Leetcode-通过分治法求最大子序和
初始代码,返回的是错误的,思路有点问题,待反思
public static int maxSubArray(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
List list = new ArrayList<Integer>();
int first = 0;
int last = nums.length - 1;
int sum = 0;
while (first <= last) {
last--;
while (first <= last) {
for (int j = first; j <= last; j++) {
sum += nums[j];
}
list.add(sum);
first++;
}
}
Collections.sort(list);
return (int) list.get(list.size()-1);
}
起初是想着用扫描法将其所有的情况求出来然后遍历,扫描处的循环语句出现了问题,待反思。
多少事,从来急,天地转,光阴迫。一万年太久,只争朝夕

浙公网安备 33010602011771号