- 两个数组最长公共子数组
1 | public static void main(String[] args) { |
- 两个数组最长公共子序列长度
1 | public static void main(String[] args) { |
- 连续子数组之和最大值
1
2
3
4
5
6
7
8
9
10
11
12public int maxSubArray(int[] nums) {
if(nums.length==0){
return 0;
}
int sum=0;
int res=Integer.MIN_VALUE;
for(int i=0;i<nums.length;i++){
sum=Math.max(sum+nums[i],nums[i]);
res=Math.max(res,sum);
}
return res;
} - 连续子数组乘积最大值
1 | int m=nums.length; |
- 最长上升子序列
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public int lengthOfLIS(int[] nums) {
int n=nums.length;
if(n==0){
return 0;
}
int[] dp=new int[n];
Arrays.fill(dp,1);
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(nums[i]>nums[j]){
dp[i]=Math.max(dp[i],dp[j]+1);
}
}
}
int res=0;
for(int i=0;i<n;i++){
res=Math.max(res,dp[i]);
}
return res;
}