好啦,今天國慶日,我不想逼迫自己咩,其實是我今天寫不出來,寫不出的題號是56. Merge Intervals,我一直覺得差一點點了,然後家裡沒有書桌可以用真的是很崩潰ㄟ,我晚上如果有時間再試試看寫不寫得出來吧,等等吃燒烤去
好啦,今天國慶日,我不想逼迫自己咩,其實是我今天寫不出來,寫不出的題號是56. Merge Intervals,我一直覺得差一點點了,然後家裡沒有書桌可以用真的是很崩潰ㄟ,我晚上如果有時間再試試看寫不寫得出來吧,等等吃燒烤去
目前的程式碼
class Solution {
public int[][] merge(int[][] intervals) {
int[] temp = new int[2];
Arrays.fill(temp, 0);
List<List<Integer>> rtemp = new ArrayList<List<Integer>>();
List<List<Integer>> rtemp2 = new ArrayList<List<Integer>>();
int i,j,k=0,a=-1,b=-1;
for ( i = 0; i < intervals.length; i++) {
Arrays.sort(intervals[i]);
}
for ( i = 0; i < intervals.length; i++) {
System.out.println(intervals[i][0] +" " +intervals[i][1]);
}
for(i=0;i<intervals.length;i++){
if(rtemp2.contains(Arrays.asList(intervals[i][0],intervals[i][1]))){
}else{
rtemp2.add(Arrays.asList(intervals[i][0],intervals[i][1]));
}
}
temp[0] = rtemp2.get(0).get(0);
temp[1] = rtemp2.get(0).get(1);
for(i=1;i<rtemp2.size();i++){
a = rtemp2.get(i).get(0);
b = rtemp2.get(i).get(1);
if(a>temp[1]){
rtemp.add(Arrays.asList(temp[0],temp[1]));
temp[0] = rtemp2.get(i).get(0);
temp[1] = rtemp2.get(i).get(1);
}else if(a<=temp[1]&&b>=temp[1]){
temp[1]= b;
}
}
if(!rtemp.contains(Arrays.asList(temp[0],temp[1]))){
rtemp.add(Arrays.asList(temp[0],temp[1]));
}
//rtemp.add(Arrays.asList(temp[0],temp[1]));
int[][] result = new int[rtemp.size()][2];
for(i=0;i<rtemp.size();i++){
for(j=0;j<2;j++){
result[i][0] = rtemp.get(i).get(0);
result[i][1] = rtemp.get(i).get(1);
}
}
return result;
}
}
sort的部分還沒處理好,之後再處理囉
二更)
我竟然解決啦
題目:56 標題:Merge Intervals 難度:Medium
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
Constraints:
1 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti <= endi <= 104
成功的程式碼
class Solution {
public int[][] merge(int[][] intervals) {
int[] temp = new int[2];
Arrays.fill(temp, 0);
List<List<Integer>> rtemp = new ArrayList<List<Integer>>();
List<List<Integer>> rtemp2 = new ArrayList<List<Integer>>();
int i,j,k=0,a=-1,b=-1;
for ( i = 0; i < intervals.length; i++) {
Arrays.sort(intervals, (x, y) -> x[0] - y[0]);
}
for ( i = 0; i < intervals.length; i++) {
System.out.println(intervals[i][0] +" " +intervals[i][1]);
}
for(i=0;i<intervals.length;i++){
if(rtemp2.contains(Arrays.asList(intervals[i][0],intervals[i][1]))){
}else{
rtemp2.add(Arrays.asList(intervals[i][0],intervals[i][1]));
}
}
temp[0] = rtemp2.get(0).get(0);
temp[1] = rtemp2.get(0).get(1);
for(i=1;i<rtemp2.size();i++){
a = rtemp2.get(i).get(0);
b = rtemp2.get(i).get(1);
if(a>temp[1]){
rtemp.add(Arrays.asList(temp[0],temp[1]));
temp[0] = rtemp2.get(i).get(0);
temp[1] = rtemp2.get(i).get(1);
}else if(a<=temp[1]&&b>=temp[1]){
temp[1]= b;
}
}
if(!rtemp.contains(Arrays.asList(temp[0],temp[1]))){
rtemp.add(Arrays.asList(temp[0],temp[1]));
}
int[][] result = new int[rtemp.size()][2];
for(i=0;i<rtemp.size();i++){
for(j=0;j<2;j++){
result[i][0] = rtemp.get(i).get(0);
result[i][1] = rtemp.get(i).get(1);
}
}
return result;
}
}
DAY25心得
明天得再寫一題了QQ