class Solution {
public int maxSubArray(int[] nums) {
int result = Integer.MIN_VALUE;
int sum = 0;
for (int i=0;i<nums.length;i++){
sum += nums[i];
result = Math.max(result,sum);
if (sum<0){
sum = 0;
}
}
return result;
}
}
class Solution {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals,(a,b)->{
return Integer.compare(a[0],b[0]);//按照首元素排序
});
int[][] result = new int[intervals.length][2];
List<int[]> record = new ArrayList<>();//记录合并之后的区间
record.add(intervals[0]);
for (int i=1;i<intervals.length;i++){
if (intervals[i][0]<=record.getLast()[1]){
record.getLast()[1] = Math.max(record.getLast()[1],intervals[i][1]);
}else{
record.add(intervals[i]);
}
}
return record.toArray(new int[record.size()][2]);
}
}
class Solution {
public void rotate(int[] nums, int k) {
k = k%nums.length; //这一步容易忘记
revers(nums,0,nums.length-1);
revers(nums,0,k-1);
revers(nums,k,nums.length-1);
}
private void revers(int[] nums, int start, int end){
while(start<end){
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}