GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaInterview/java/src/LeetCode/MergeIntervals_56.java at master · damaohongtu/JavaInterview

Expand file tree

/

MergeIntervals_56.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
52 lines (47 loc) · 1.39 KB
/

MergeIntervals_56.java

Copy path

File metadata and controls

52 lines (47 loc) · 1.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package LeetCode;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
/**
* @Classname MergeIntervals_56
* @Description 将数组重叠的部分合并,返回去除交叉后的二维数组
* 思路:使用LinkedHasMap
* @Date 19-5-21 上午8:55
* @Created by mao<[email protected]>
*/
public class MergeIntervals_56 {
public int[][] merge(int[][] intervals) {
int len=intervals.length;
if(len==0){
return new int[0][2];
}
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0]-o2[0];
}
});
LinkedHashMap<Integer,Integer> map=new LinkedHashMap<>();
map.put(intervals[0][0],intervals[0][1]);
int key=intervals[0][0];
for(int i=1;i<len;i++){
if(intervals[i][0]>map.get(key)){
map.put(intervals[i][0],intervals[i][1]);
key=intervals[i][0];
}else {
if(intervals[i][1]>map.get(key)){
map.put(key,intervals[i][1]);
}
}
}
int l=map.keySet().size();
int[][] ans=new int[l][2];
int j=0;
for(Integer i:map.keySet()){
ans[j][0]=i;
ans[j][1]=map.get(i);
j++;
}
return ans;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.