当前位置:网站首页>Continuous maximum sum and judgement palindrome
Continuous maximum sum and judgement palindrome
2022-07-25 05:22:00 【InfoQ】
️ Continuous maximum sum ️
Topic details
3
-1 2 1
3
Their thinking
arrni- State definition
- Determine the initial state
- Determine the state transfer equation
iif[n]Source code
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Transition definition : Definition f[i] Said to i The maximum sequence sum of the elements as the ending subscript
// The initial state is determined : When i=0 when , The largest sum of elements is f[0] = arr[0]
// State transition equation f[i] = Max(f[i - 1] + arr[i], arr[i])
int[] f = new int[n];
f[0] = arr[0];
//ans Used to find the sum of maximal sub continuous sequences
int ans = arr[0];
for (int i = 1; i < n; i++) {
int curval = f[i - 1] + arr[i];
f[i] = curval > arr[i] ? curval : arr[i];
// If currently i The maximum sequence sum ratio at the end ans Big , Update , Otherwise, it will not be updated
ans = f[i] > ans ? f[i] : ans;
}
System.out.println(ans);
}
}
summary
️ Palindrome ️
Topic details
aba
b
2
Their thinking
Astr1Bstr2str2str11Source code
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
int ans = 0;
int n = str1.length();
for (int i = 0; i <= n; i++) {
StringBuilder sb = new StringBuilder();
sb.append(str1.substring(0, i));
sb.append(str2);
sb.append(str1.substring(i, n));
String rs1 = sb.toString();
String rs2 = sb.reverse().toString();
if (rs1.equals(rs2)) {
ans++;
}
}
System.out.println(ans);
}
}
summary
str2str1边栏推荐
- Odoo14 | about the abnormal display of statusbar keyword after use and Its Solutions
- 微服务 - Hystrix 熔断器
- Three schemes for finclip to realize wechat authorized login
- Sword finger offer II 014. anagrams in strings
- Nexttick principle analysis
- Win11 how to view the file explorer tab
- Introduction to kubernetes
- 自己实现is_class
- STL notes (III): input / output stream
- rhcsa暑假第三天
猜你喜欢

Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies

ThreadLocal

Forwarding and sharing function of wechat applet

epoll的实现原理

1310_一个printf的实现分析

Leetcode 15: sum of three numbers

Single sign on (one sign on, available everywhere)

微服务 - 远程调用(Feign组件)

What content does the software test plan include and how to write it. Share test plan template

85 distributed project construction
随机推荐
Exchange 2010 SSL certificate installation document
Implement is by yourself_ class
Openfegin remote call lost request header problem
[live review] AI customer service "changes according to the situation", and man-machine dialogue can be easier
The global shipment of glory 8x series exceeded 10million units, and the glory V20 exceeded 1.5 million units!
STL notes (VI): container vector
Anshi semiconductor management paid a visit to Wentai technology and Gree Electric appliance
Single sign on (one sign on, available everywhere)
Go language function
systemVerilog中automatic用法
background
2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f
STL notes (II): template and operator overloading
STL notes (VIII): container - List
systemverilog中function和task区别
STL notes (IV): String
Render Optimization: repaint and reflow
DOM在Ahooks中的处理过程
Solve the problem that uni app applet obtains routes and route parameters
H5 new feature domcontentloaded event - custom attribute -async attribute -history interface method - local storage -postmessage