class Solution53 {
public int maxSubArray(int[] nums) {
int pre = 0, maxAns = nums[0];
//对nums进行遍历
for (int x : nums) {
// 取当前元素和之前连续相加元素和的最大值
pre = Math.max(pre + x, x);
// 取此时最大和的连续子数组和之前的最大值
maxAns = Math.max(maxAns, pre);
}
return maxAns;
}
}
class Solution217 {
public boolean containsDuplicate(int[] nums) {
//对nums进行排序,升序
Arrays.sort(nums);
int n = nums.length;
for (int i = 0; i < n - 1; i++) {
//判断至少出现两次
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
}