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

Diff for: leetcode/146. LRU Cache.md

100644100755
File mode changed.

Diff for: leetcode/160. Intersection of Two Linked Lists.md

+34-1
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+
```

Diff for: leetcode/2. Add Two Numbers.md

+40-1
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+
```

Diff for: leetcode/218. The Skyline Problem.md

100644100755
File mode changed.

Diff for: leetcode/23. Merge k Sorted Lists.md

+48-1
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+
```

Diff for: leetcode/238. Product of Array Except Self.md

+20
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+
```

Diff for: leetcode/273. Integer to English Words.md

+71
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+
```

Diff for: leetcode/287. Find the Duplicate Number.md

+20
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+
```

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

100644100755
File mode changed.

Diff for: leetcode/387. First Unique Character in a String.md

100644100755
File mode changed.

Diff for: leetcode/445. Add Two Numbers II.md

100644100755
File mode changed.

Diff for: leetcode/450. Delete Node in a BST.md

100644100755
File mode changed.

Diff for: leetcode/5. Longest Palindromic Substring.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Output: "bb"
1818
### Thinking:
1919
* Method1: Iterate center of two points.
2020
** 1.Center is a character, length of palindromic string is odd.
21-
** 2.Center is between two characters, length of palindromic is even.
21+
** 2.Center is between two characters, length of palindromic is even.
2222
```Java
2323
class Solution {
2424
public String longestPalindrome(String s) {
@@ -115,4 +115,60 @@ class Solution {
115115
return s.substring(++low, high);
116116
}
117117
}
118-
```
118+
```
119+
120+
### Third Time
121+
* Method 1: O(n^3)
122+
```Java
123+
class Solution {
124+
public String longestPalindrome(String s) {
125+
String res = "";
126+
if(s == null || s.length() == 0) return res;
127+
for(int i = 0; i < s.length(); i++){
128+
for(int j = i + res.length(); j <= s.length(); j++){
129+
String sub = s.substring(i, j);
130+
if(isPalindrome(sub)){
131+
res = sub;
132+
}
133+
}
134+
}
135+
return res;
136+
}
137+
private boolean isPalindrome(String s){ // O(n)
138+
int low = 0, high = s.length() - 1;
139+
char[] arr = s.toCharArray();
140+
while(low < high){
141+
if(arr[low++] != arr[high--]) return false;
142+
}
143+
return true;
144+
}
145+
}
146+
```
147+
148+
* Method 2: O(n^2)
149+
```Java
150+
class Solution {
151+
private int low, max;
152+
private char[] arr;
153+
public String longestPalindrome(String s) {
154+
if(s == null || s.length() == 0) return s;
155+
else if(s.length() == 1) return s;
156+
arr = s.toCharArray();
157+
for(int i = 0; i < arr.length - 1; i++){
158+
expand(i, i);
159+
expand(i, i + 1);
160+
}
161+
return s.substring(low, low + max);
162+
}
163+
private void expand(int low, int high){
164+
while(low >= 0 && high < arr.length && arr[low] == arr[high]){
165+
low--;
166+
high++;
167+
}
168+
if(this.max < high - low - 1){
169+
this.low = low + 1;
170+
this.max = high - 1 - low;
171+
}
172+
}
173+
}
174+
```

Diff for: leetcode/501. Find Mode in Binary Search Tree.md

100644100755
File mode changed.

Diff for: leetcode/530. Minimum Absolute Difference in BST.md

100644100755
File mode changed.

Diff for: leetcode/535. Encode and Decode TinyURL.md

100644100755
File mode changed.

Diff for: leetcode/617. Merge Two Binary Trees.md

100644100755
File mode changed.

Diff for: leetcode/668. Kth Smallest Number in Multiplication Table.md

100644100755
File mode changed.

Diff for: leetcode/700. Search in a Binary Search Tree.md

100644100755
File mode changed.

Diff for: leetcode/701. Insert into a Binary Search Tree.md

100644100755
File mode changed.

Diff for: leetcode/707. Design Linked List.md

100644100755
File mode changed.

Diff for: leetcode/719. Find K-th Smallest Pair Distance.md

100644100755
File mode changed.

Diff for: leetcode/726. Number of Atoms.md

100644100755
File mode changed.

Diff for: leetcode/763. Partition Labels.md

100644100755
File mode changed.

Diff for: leetcode/771. Jewels and Stones.md

100644100755
File mode changed.

Diff for: leetcode/778. Swim in Rising Water.md

100644100755
File mode changed.

Diff for: leetcode/786. K-th Smallest Prime Fraction.md

100644100755
File mode changed.

Diff for: leetcode/819. Most Common Word.md

100644100755
File mode changed.

Diff for: leetcode/856. Score of Parentheses.md

100644100755
File mode changed.

Diff for: leetcode/875. Koko Eating Bananas.md

100644100755
File mode changed.

Diff for: leetcode/904. Fruit Into Baskets.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## 904. Fruit Into Baskets
2+
3+
### Question
4+
In a row of trees, the i-th tree produces fruit with type tree[i].
5+
6+
You start at any tree of your choice, then repeatedly perform the following steps:
7+
8+
Add one piece of fruit from this tree to your baskets. If you cannot, stop.
9+
Move to the next tree to the right of the current tree. If there is no tree to the right, stop.
10+
Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop.
11+
12+
You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each.
13+
14+
What is the total amount of fruit you can collect with this procedure?
15+
16+
17+
```
18+
Example 1:
19+
20+
Input: [1,2,1]
21+
Output: 3
22+
Explanation: We can collect [1,2,1].
23+
Example 2:
24+
25+
Input: [0,1,2,2]
26+
Output: 3
27+
Explanation: We can collect [1,2,2].
28+
If we started at the first tree, we would only collect [0, 1].
29+
Example 3:
30+
31+
Input: [1,2,3,2,2]
32+
Output: 4
33+
Explanation: We can collect [2,3,2,2].
34+
If we started at the first tree, we would only collect [1, 2].
35+
Example 4:
36+
37+
Input: [3,3,3,1,2,1,1,2,3,3,4]
38+
Output: 5
39+
Explanation: We can collect [1,2,1,1,2].
40+
If we started at the first tree or the eighth tree, we would only collect 4 fruits.
41+
```
42+
43+
Note:
44+
* 1 <= tree.length <= 40000
45+
* 0 <= tree[i] < tree.length
46+
47+
### Solution
48+
* Method 1: HashMap + Two pointers
49+
```Java
50+
class Solution {
51+
public int totalFruit(int[] tree) {
52+
if(tree.length < 2) return tree.length;
53+
Map<Integer, Integer> map = new HashMap<>();
54+
int start = 0, max = 0;
55+
for(int i = 0; i < tree.length; i++){
56+
map.put(tree[i], map.getOrDefault(tree[i], 0) + 1);
57+
while(map.size() > 2){
58+
map.put(tree[start], map.get(tree[start]) - 1);
59+
if(map.get(tree[start]) == 0) map.remove(tree[start]);
60+
start++;
61+
}
62+
max = Math.max(max, i - start + 1);
63+
}
64+
return max;
65+
}
66+
}
67+
```

Diff for: leetcode/929. Unique Email Addresses.md

100644100755
File mode changed.

Diff for: leetcode/973. K Closest Points to Origin.md

100644100755
File mode changed.

Diff for: leetcode/977. Squares of a Sorted Array.md

100644100755
File mode changed.

0 commit comments

Comments
 (0)