File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ 탐욕법을 이용하여, 연산하는 방식
3+ */
4+ class Solution {
5+ public int [][] merge (int [][] intervals ) {
6+
7+ Arrays .sort (intervals , (a ,b ) -> a [0 ] - b [0 ]);
8+
9+ List <int []> result = new ArrayList <>();
10+ int startNum = intervals [0 ][0 ];
11+ int endNum = intervals [0 ][1 ];
12+ for (int i = 1 ; i < intervals .length ; i ++) {
13+ if (endNum >= intervals [i ][0 ]) {
14+ endNum = Math .max (endNum , intervals [i ][1 ]);
15+ continue ;
16+ }
17+
18+ result .add (new int []{startNum , endNum });
19+ startNum = intervals [i ][0 ];
20+ endNum = intervals [i ][1 ];
21+ }
22+ result .add (new int []{startNum , endNum });
23+
24+ return result .toArray (new int [result .size ()][]);
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ /**
2+ 최댓값을 기준으로 배열의 합과 1~N까지의 합을 비교하여 확인하는 방식
3+ */
4+ class Solution {
5+ public int missingNumber (int [] nums ) {
6+ int max = 0 ;
7+ int sum = 0 ;
8+ boolean existsZero = false ;
9+ for (int i = 0 ; i < nums .length ; i ++) {
10+ max = Math .max (nums [i ], max );
11+ sum += nums [i ];
12+ if (nums [i ] == 0 ){
13+ existsZero = true ;
14+ }
15+ }
16+
17+ int result = (max * (max + 1 )) / 2 - sum ;
18+
19+ if (!existsZero ) {
20+ result = 0 ;
21+ } else if (result == 0 ) {
22+ result = max + 1 ;
23+ }
24+
25+ return result ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+
2+ class Solution {
3+ public void reorderList (ListNode head ) {
4+ if (head .next == null ) return ;
5+
6+ ListNode prev = null ;
7+ while (true ) {
8+ ListNode tail = head ;
9+ while (tail .next != null ) {
10+ prev = tail ;
11+ tail = tail .next ;
12+ }
13+
14+ if (prev == head ) break ;
15+
16+ // tail 이동
17+ ListNode headNext = head .next ;
18+ prev .next = null ;
19+ head .next = tail ;
20+ tail .next = headNext ;
21+
22+ head = headNext ;
23+ }
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments