56. 合并区间
class Solution {
public int[][] merge(int[][] intervals) {
List<int[] > res = new LinkedList<>();
Arrays.sort(intervals, (a,b) -> Integer.compare(a[0],b[0]));
for(int i = 1;i < intervals.length; i++){
if(intervals[i][0] > intervals[i-1][1]){
res.add(intervals[i-1]);
}else{
intervals[i][0] = intervals[i-1][0];
//[1,4][2,3]这种情况需要注意上一个元素的有边界是否比当前元素右边界更大。
intervals[i][1] = Math.max(intervals[i][1],intervals[i-1][1]);
}
}
res.add(intervals[intervals.length - 1]);
return res.toArray(new int[res.size()][]);
}
}
738.单调递增的数字
class Solution {
public int monotoneIncreasingDigits(int n) {
String num = String.valueOf(n);
char[] ch = num.toCharArray();
int flag = ch.length;
for(int i = ch.length - 1; i > 0; i--){
if(ch[i]<ch[i-1]){
ch[i-1] = (char)(ch[i-1] - 1);
flag = i;
}
}
for(int j = flag; j < ch.length; j++){
ch[j] = '9';
}
return Integer.parseInt(String.valueOf(ch));
}
}