Skip to content

Commit 763f185

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions with tag amazon
1 parent 1bc4533 commit 763f185

36 files changed

+387
-6
lines changed

leetcode/146. LRU Cache.md

100644100755
File mode changed.

leetcode/160. Intersection of Two Linked Lists.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,37 @@ public class Solution {
9494
return (curA == curB) ? curA : null;
9595
}
9696
}
97-
```
97+
```
98+
99+
### Third Time
100+
* Method 1: List
101+
```Java
102+
/**
103+
* Definition for singly-linked list.
104+
* public class ListNode {
105+
* int val;
106+
* ListNode next;
107+
* ListNode(int x) {
108+
* val = x;
109+
* next = null;
110+
* }
111+
* }
112+
*/
113+
public class Solution {
114+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
115+
if(headA == null || headB == null) return null;
116+
ListNode cur1 = headA, cur2 = headB;
117+
int reach = 0;
118+
while(cur1 != cur2){
119+
cur1 = cur1.next;
120+
if(cur1 == null){
121+
cur1 = headB;
122+
if(++reach == 2) return null;
123+
}
124+
cur2 = cur2.next;
125+
if(cur2 == null) cur2 = headA;
126+
}
127+
return cur1;
128+
}
129+
}
130+
```

leetcode/2. Add Two Numbers.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,43 @@ class Solution {
136136
return dummy.next;
137137
}
138138
}
139-
```
139+
```
140+
141+
### Third Time
142+
* Method 1: List
143+
```Java
144+
/**
145+
* Definition for singly-linked list.
146+
* public class ListNode {
147+
* int val;
148+
* ListNode next;
149+
* ListNode(int x) { val = x; }
150+
* }
151+
*/
152+
class Solution {
153+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
154+
int carry = 0;
155+
ListNode dummy = new ListNode(0), cur = dummy;
156+
while(l1 != null || l2 != null){
157+
int sum = carry;
158+
if(l1 != null && l2 != null){
159+
sum += l1.val + l2.val;
160+
l1 = l1.next;
161+
l2 = l2.next;
162+
}else if(l1 != null){
163+
sum += l1.val;
164+
l1 = l1.next;
165+
}else{
166+
sum += l2.val;
167+
l2 = l2.next;
168+
}
169+
carry = sum / 10;
170+
cur.next = new ListNode(sum % 10);
171+
cur = cur.next;
172+
}
173+
if(carry == 1)
174+
cur.next = new ListNode(1);
175+
return dummy.next;
176+
}
177+
}
178+
```

leetcode/218. The Skyline Problem.md

100644100755
File mode changed.

leetcode/23. Merge k Sorted Lists.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,51 @@ class Solution {
176176
return dummy.next;
177177
}
178178
}
179-
```
179+
```
180+
181+
### Fourth Time
182+
* Method 1: Divede and Conquer
183+
```Java
184+
/**
185+
* Definition for singly-linked list.
186+
* public class ListNode {
187+
* int val;
188+
* ListNode next;
189+
* ListNode(int x) { val = x; }
190+
* }
191+
*/
192+
class Solution {
193+
private ListNode[] lists;
194+
public ListNode mergeKLists(ListNode[] lists) {
195+
if(lists == null || lists.length == 0) return null;
196+
else if(lists.length == 1) return lists[0];
197+
this.lists = lists;
198+
return sort(0, lists.length - 1);
199+
}
200+
private ListNode sort(int low, int high){
201+
if(low == high) return this.lists[low];
202+
int mid = low + (high - low) / 2;
203+
ListNode l1 = sort(low, mid);
204+
ListNode l2 = sort(mid + 1, high);
205+
return merge(l1, l2);
206+
}
207+
private ListNode merge(ListNode l1, ListNode l2){
208+
ListNode dummy = new ListNode(0), cur = dummy;
209+
while(l1 != null || l2 != null){
210+
if(l1 != null && l2 != null){
211+
cur.next = l1.val < l2.val ? l1: l2;
212+
if(l1.val < l2.val) l1 = l1.next;
213+
else l2 = l2.next;
214+
}else if(l1 != null){
215+
cur.next = l1;
216+
l1 = l1.next;
217+
}else{
218+
cur.next = l2;
219+
l2 = l2.next;
220+
}
221+
cur = cur.next;
222+
}
223+
return dummy.next;
224+
}
225+
}
226+
```

leetcode/238. Product of Array Except Self.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,23 @@ class Solution {
6363
}
6464
}
6565
```
66+
67+
### Third Time
68+
* Method 1: O(N) time complexity and O(1) space complexity
69+
```Java
70+
class Solution {
71+
public int[] productExceptSelf(int[] nums) {
72+
int[] left = new int[nums.length];
73+
left[0] = 1;
74+
for(int i = 1; i < nums.length; i++){
75+
left[i] = left[i - 1] * nums[i - 1];
76+
}
77+
int right = 1;
78+
for(int i = nums.length - 2; i >= 0; i--){
79+
right *= nums[i + 1];
80+
left[i] *= right;
81+
}
82+
return left;
83+
}
84+
}
85+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## 273. Integer to English Words
2+
3+
### Question
4+
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
5+
6+
```
7+
Example 1:
8+
9+
Input: 123
10+
Output: "One Hundred Twenty Three"
11+
Example 2:
12+
13+
Input: 12345
14+
Output: "Twelve Thousand Three Hundred Forty Five"
15+
Example 3:
16+
17+
Input: 1234567
18+
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
19+
Example 4:
20+
21+
Input: 1234567891
22+
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
23+
```
24+
25+
### Solution
26+
* Method 1:
27+
```Java
28+
class Solution {
29+
private static String[] bigUnit = new String[]{"", "Thousand", "Million", "Billion"};
30+
private static String[] numbers = new String[]{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
31+
private static String[] tens = new String[]{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
32+
private static String[] overTens = new String[]{"", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
33+
public String numberToWords(int num) {
34+
if(num == 0) return "Zero";
35+
String result = "";
36+
int bigUnitIndex = 0;
37+
while(num > 0){
38+
String temp = parseThree(num % 1000);
39+
if(temp.length() != 0){
40+
result = temp
41+
+ (bigUnitIndex == 0 ? "": " ")
42+
+ bigUnit[bigUnitIndex]
43+
+ (result.length() == 0 ? "": " ") + result;
44+
}
45+
bigUnitIndex++;
46+
num /= 1000;
47+
}
48+
return result;
49+
}
50+
private String parseThree(int num){
51+
String res = "";
52+
if(num % 100 > 10 && num % 100 < 20){
53+
res = overTens[num % 100 - 10];
54+
num /= 100;
55+
}else{
56+
if(num % 10 != 0){
57+
res = numbers[num % 10];
58+
}
59+
num /= 10;
60+
if(num % 10 != 0){
61+
res = tens[num % 10] + (res.length() == 0 ? "":" ") + res;
62+
}
63+
num /= 10;
64+
}
65+
if(num % 10 != 0){
66+
res = numbers[num % 10] + " Hundred" + (res.length() == 0 ? "": " ") + res;
67+
}
68+
return res;
69+
}
70+
}
71+
```

leetcode/287. Find the Duplicate Number.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,23 @@ class Solution {
7373
}
7474
}
7575
```
76+
77+
### Third Time
78+
* Method 1: Cyclic list
79+
```Java
80+
class Solution {
81+
public int findDuplicate(int[] nums) {
82+
int slow = 0, fast = 0;
83+
do{
84+
slow = nums[slow];
85+
fast = nums[nums[fast]];
86+
}while(nums[slow] != nums[fast]);
87+
slow = 0;
88+
while(nums[slow] != nums[fast]){
89+
slow = nums[slow];
90+
fast = nums[fast];
91+
}
92+
return nums[slow];
93+
}
94+
}
95+
```

leetcode/380. Insert Delete GetRandom O(1).md

100644100755
File mode changed.

leetcode/387. First Unique Character in a String.md

100644100755
File mode changed.

0 commit comments

Comments
 (0)