Skip to content

Commit 411c90d

Browse files
committed
[Function add]
1. Add leetcode solutions with tag list.
1 parent 9b3ed41 commit 411c90d

7 files changed

+468
-3
lines changed

leetcode/141. Linked List Cycle.md

+29
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,32 @@ public class Solution {
8989
}
9090
}
9191
```
92+
93+
### Third Time
94+
* Method 1: 2 pointer
95+
```Java
96+
/**
97+
* Definition for singly-linked list.
98+
* class ListNode {
99+
* int val;
100+
* ListNode next;
101+
* ListNode(int x) {
102+
* val = x;
103+
* next = null;
104+
* }
105+
* }
106+
*/
107+
public class Solution {
108+
public boolean hasCycle(ListNode head) {
109+
if(head == null) return false;
110+
ListNode slow = head, fast = head;
111+
if(fast.next == null) return false;
112+
do{
113+
slow = slow.next;
114+
fast = fast.next.next;
115+
}while(slow != fast && slow != null && fast != null && fast.next != null);
116+
if(slow == fast) return true;
117+
return false;
118+
}
119+
}
120+
```

leetcode/142. Linked List Cycle II.md

+33
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,36 @@ public class Solution {
9696
}
9797
}
9898
```
99+
100+
### Third Time
101+
* Method 1: two pointer
102+
```Java
103+
/**
104+
* Definition for singly-linked list.
105+
* class ListNode {
106+
* int val;
107+
* ListNode next;
108+
* ListNode(int x) {
109+
* val = x;
110+
* next = null;
111+
* }
112+
* }
113+
*/
114+
public class Solution {
115+
public ListNode detectCycle(ListNode head) {
116+
if(head == null || head.next == null) return null;
117+
ListNode slow = head, fast = head;
118+
do{
119+
slow = slow.next;
120+
fast = fast.next.next;
121+
}while(slow != fast && slow != null && fast != null && fast.next != null);
122+
if(slow != fast) return null;
123+
slow = head;
124+
while(slow != fast){
125+
slow = slow.next;
126+
fast = fast.next;
127+
}
128+
return slow;
129+
}
130+
}
131+
```

leetcode/2. Add Two Numbers.md

+82-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,85 @@ class Solution {
5555
return res.next;
5656
}
5757
}
58-
```
58+
```
59+
60+
### Second Time
61+
* Method 1:
62+
```Java
63+
/**
64+
* Definition for singly-linked list.
65+
* public class ListNode {
66+
* int val;
67+
* ListNode next;
68+
* ListNode(int x) { val = x; }
69+
* }
70+
*/
71+
class Solution {
72+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
73+
int carry = 0;
74+
ListNode cur1 = l1, cur2 = l2;
75+
ListNode dummy = new ListNode(0);
76+
ListNode cur = dummy;
77+
ListNode temp = null;
78+
while(cur1 != null && cur2 != null){
79+
int sum = cur1.val + cur2.val + carry;
80+
temp = new ListNode(sum % 10);
81+
carry = sum / 10;
82+
cur.next = temp;
83+
cur = cur.next;
84+
cur1 = cur1.next;
85+
cur2 = cur2.next;
86+
}
87+
ListNode node = cur1 != null ? cur1: cur2;
88+
while(node != null){
89+
temp = new ListNode((node.val + carry) % 10);
90+
carry = (node.val + carry) / 10;
91+
cur.next = temp;
92+
cur = cur.next;
93+
node = node.next;
94+
}
95+
if(carry == 1){
96+
cur.next = new ListNode(1);
97+
}
98+
return dummy.next;
99+
}
100+
}
101+
```
102+
103+
* Method 2: Optimization of Method 1.
104+
```Java
105+
/**
106+
* Definition for singly-linked list.
107+
* public class ListNode {
108+
* int val;
109+
* ListNode next;
110+
* ListNode(int x) { val = x; }
111+
* }
112+
*/
113+
class Solution {
114+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
115+
ListNode dummy = new ListNode(0);
116+
ListNode cur = dummy;
117+
int carry = 0;
118+
while(l1 != null || l2 != null){
119+
int sum = 0;
120+
if(l1 != null && l2 != null){
121+
sum = l1.val + l2.val + carry;
122+
l1 = l1.next;
123+
l2 = l2.next;
124+
}else if(l1 != null){
125+
sum = l1.val + carry;
126+
l1 = l1.next;
127+
}else{
128+
sum = l2.val + carry;
129+
l2 = l2.next;
130+
}
131+
cur.next = new ListNode(sum % 10);
132+
carry = sum / 10;
133+
cur = cur.next;
134+
}
135+
if(carry == 1) cur.next = new ListNode(1);
136+
return dummy.next;
137+
}
138+
}
139+
```

leetcode/206. Reverse Linked List.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,59 @@ class Solution {
125125
}
126126
}
127127
}
128-
```
128+
```
129+
130+
### Third Time
131+
* Method 1: Loop
132+
```Java
133+
/**
134+
* Definition for singly-linked list.
135+
* public class ListNode {
136+
* int val;
137+
* ListNode next;
138+
* ListNode(int x) { val = x; }
139+
* }
140+
*/
141+
class Solution {
142+
public ListNode reverseList(ListNode head) {
143+
ListNode pre = null, cur = head, next = null;
144+
while(cur != null){
145+
next = cur.next;
146+
cur.next = pre;
147+
pre = cur;
148+
cur = next;
149+
}
150+
return pre;
151+
}
152+
}
153+
```
154+
155+
* Method 2: recursion
156+
```Java
157+
/**
158+
* Definition for singly-linked list.
159+
* public class ListNode {
160+
* int val;
161+
* ListNode next;
162+
* ListNode(int x) { val = x; }
163+
* }
164+
*/
165+
class Solution {
166+
private ListNode head = null;
167+
public ListNode reverseList(ListNode head) {
168+
if(head == null) return head;
169+
getNext(null, head);
170+
return this.head;
171+
}
172+
private void getNext(ListNode pre, ListNode cur){
173+
ListNode next = cur.next;
174+
if(cur.next == null){
175+
this.head = cur;
176+
cur.next = pre;
177+
return;
178+
}
179+
cur.next = pre;
180+
getNext(cur, next);
181+
}
182+
}
183+
```

leetcode/218. The Skyline Problem.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## 218. The Skyline Problem
2+
3+
### Question
4+
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
5+
Buildings Skyline Contour
6+
![Imgur](https://i.imgur.com/KnMSfkr.png)
7+
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
8+
9+
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
10+
11+
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
12+
13+
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
14+
15+
Notes:
16+
1. The number of buildings in any input list is guaranteed to be in the range [0, 10000].
17+
2. The input list is already sorted in ascending order by the left x position Li.
18+
3. The output list must be sorted by the x position.
19+
4. There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]
20+
21+
### Solution
22+
* Method 1: PriorityQueue
23+
![Imgur](https://i.imgur.com/Fo2NqRe.png)
24+
```Java
25+
class Solution {
26+
public List<int[]> getSkyline(int[][] buildings) {
27+
List<int[]> result = new ArrayList<>();
28+
List<int[]> building = new ArrayList<>();
29+
for(int[] b : buildings){
30+
building.add(new int[]{b[0], -b[2]});
31+
building.add(new int[]{b[1], b[2]});
32+
}
33+
Collections.sort(building, new Comparator<int[]>(){
34+
@Override
35+
public int compare(int[] a, int[] b){
36+
if(a[0] != b[0]) return a[0] - b[0];
37+
return a[1] - b[1];
38+
}
39+
});
40+
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
41+
@Override
42+
public int compare(Integer h1, Integer h2){
43+
return h2 - h1;
44+
}
45+
});
46+
pq.offer(0);
47+
int prev = 0;
48+
for(int[] h : building){
49+
if(h[1] < 0) pq.offer(-h[1]);
50+
else pq.remove(h[1]);
51+
int cur = pq.peek();
52+
if(cur != prev){
53+
result.add(new int[]{h[0], cur});
54+
prev = cur;
55+
}
56+
}
57+
return result;
58+
}
59+
}
60+
```
61+
62+
### Reference
63+
1. [花花酱 LeetCode 218. The Skyline Problem](http://zxi.mytechroad.com/blog/tree/leetcode-218-the-skyline-problem/)

leetcode/24. Swap Nodes in Pairs.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,33 @@ class Solution {
6262
return dummy.next;
6363
}
6464
}
65-
```
65+
```
66+
67+
### Third Time
68+
* Method 1:
69+
```Java
70+
/**
71+
* Definition for singly-linked list.
72+
* public class ListNode {
73+
* int val;
74+
* ListNode next;
75+
* ListNode(int x) { val = x; }
76+
* }
77+
*/
78+
class Solution {
79+
public ListNode swapPairs(ListNode head) {
80+
ListNode dummy = new ListNode(0);
81+
dummy.next = head;
82+
ListNode cur = dummy;
83+
while(cur != null && cur.next != null && cur.next.next != null){
84+
ListNode temp = cur.next;
85+
ListNode next = temp.next.next;
86+
cur.next = temp.next;
87+
cur.next.next = temp;
88+
temp.next = next;
89+
cur = cur.next.next;
90+
}
91+
return dummy.next;
92+
}
93+
}
94+
```

0 commit comments

Comments
 (0)