Skip to content

Commit a3a0726

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parent 763f185 commit a3a0726

11 files changed

+627
-1
lines changed

leetcode/101. Symmetric Tree.md

+28
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,31 @@ class Solution {
108108
}
109109
}
110110
```
111+
112+
### Fourth Time
113+
* Method 1: recursion
114+
```Java
115+
/**
116+
* Definition for a binary tree node.
117+
* public class TreeNode {
118+
* int val;
119+
* TreeNode left;
120+
* TreeNode right;
121+
* TreeNode(int x) { val = x; }
122+
* }
123+
*/
124+
class Solution {
125+
public boolean isSymmetric(TreeNode root) {
126+
if(root == null) return true;
127+
return check(root.left, root.right);
128+
}
129+
private boolean check(TreeNode n1, TreeNode n2){
130+
if(n1 == null && n2 == null) return true;
131+
else if(n1 == null || n2 == null) return false;
132+
else{
133+
if(n1.val != n2.val) return false;
134+
return check(n1.left, n2.right) && check(n1.right, n2.left);
135+
}
136+
}
137+
}
138+
```

leetcode/102. Binary Tree Level Order Traversal.md

+34
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,37 @@ class Solution {
126126
}
127127
}
128128
```
129+
130+
### Fourth Time
131+
* Method 1: BFS
132+
```Java
133+
/**
134+
* Definition for a binary tree node.
135+
* public class TreeNode {
136+
* int val;
137+
* TreeNode left;
138+
* TreeNode right;
139+
* TreeNode(int x) { val = x; }
140+
* }
141+
*/
142+
class Solution {
143+
public List<List<Integer>> levelOrder(TreeNode root) {
144+
List<List<Integer>> result = new ArrayList<>();
145+
if(root == null) return result;
146+
Queue<TreeNode> q = new LinkedList<>();
147+
q.offer(root);
148+
while(!q.isEmpty()){
149+
List<Integer> temp = new ArrayList<>();
150+
int size = q.size();
151+
for(int i = 0; i < size; i++){
152+
TreeNode node = q.poll();
153+
temp.add(node.val);
154+
if(node.left != null) q.offer(node.left);
155+
if(node.right != null) q.offer(node.right);
156+
}
157+
result.add(temp);
158+
}
159+
return result;
160+
}
161+
}
162+
```

leetcode/129. Sum Root to Leaf Numbers.md

+31
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,35 @@ class Solution {
141141
if(node.right != null) dfs(node.right, cur);
142142
}
143143
}
144+
```
145+
146+
### Fourth Time
147+
* Method 1: dfs
148+
```Java
149+
/**
150+
* Definition for a binary tree node.
151+
* public class TreeNode {
152+
* int val;
153+
* TreeNode left;
154+
* TreeNode right;
155+
* TreeNode(int x) { val = x; }
156+
* }
157+
*/
158+
class Solution {
159+
private int sum = 0;
160+
public int sumNumbers(TreeNode root) {
161+
if(root == null) return 0;
162+
getNumber(root, 0);
163+
return this.sum;
164+
}
165+
private void getNumber(TreeNode node, int num){
166+
int cur = num * 10 + node.val;
167+
if(node.left == null && node.right == null){
168+
sum += cur;
169+
return;
170+
}
171+
if(node.left != null) getNumber(node.left, cur);
172+
if(node.right != null) getNumber(node.right, cur);
173+
}
174+
}
144175
```

leetcode/165. Compare Version Numbers.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,31 @@ class Solution {
9393
return 0;
9494
}
9595
}
96-
```
96+
```
97+
98+
### Third Time
99+
* Method 1: String
100+
```Java
101+
class Solution {
102+
public int compareVersion(String version1, String version2) {
103+
String[] v1 = version1.split("\\.");
104+
String[] v2 = version2.split("\\.");
105+
int count1 = 0, count2 = 0;
106+
while(count1 < v1.length || count2 < v2.length){
107+
if(count1 < v1.length && count2 < v2.length){
108+
int v1Num = Integer.parseInt(v1[count1++]);
109+
int v2Num = Integer.parseInt(v2[count2++]);
110+
if(v1Num < v2Num) return -1;
111+
else if(v1Num > v2Num) return 1;
112+
}else if(count1 < v1.length){
113+
int v1Num = Integer.parseInt(v1[count1++]);
114+
if(v1Num > 0) return 1;
115+
}else{
116+
int v2Num = Integer.parseInt(v2[count2++]);
117+
if(v2Num > 0) return -1;
118+
}
119+
}
120+
return 0;
121+
}
122+
}
123+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## 186. Reverse Words in a String II
2+
3+
### Question
4+
Given an input string , reverse the string word by word.
5+
6+
```
7+
Example:
8+
9+
Input: ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
10+
Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]
11+
```
12+
13+
Note:
14+
* A word is defined as a sequence of non-space characters.
15+
* The input string does not contain leading or trailing spaces.
16+
* The words are always separated by a single space.
17+
18+
Follow up: Could you do it in-place without allocating extra space?
19+
20+
21+
### Solution
22+
* Method 1: String operation
23+
```Java
24+
class Solution {
25+
public void reverseWords(char[] str) {
26+
String s = new String(str);
27+
String[] tokens = s.split(" ");
28+
int count = 0;
29+
for(int i = tokens.length - 1; i >= 0; i--){
30+
for(char c : tokens[i].toCharArray()){
31+
str[count++] = c;
32+
}
33+
if(count < str.length)
34+
str[count++] = ' ';
35+
}
36+
}
37+
}
38+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## 270. Closest Binary Search Tree Value
2+
3+
### Question
4+
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
5+
6+
Note:
7+
* Given target value is a floating point.
8+
* You are guaranteed to have only one unique value in the BST that is closest to the target.
9+
10+
```
11+
Example:
12+
13+
Input: root = [4,2,5,1,3], target = 3.714286
14+
15+
4
16+
/ \
17+
2 5
18+
/ \
19+
1 3
20+
21+
Output: 4
22+
```
23+
24+
25+
### Solution
26+
* Method 1: recursion O(nlogn)
27+
```Java
28+
/**
29+
* Definition for a binary tree node.
30+
* public class TreeNode {
31+
* int val;
32+
* TreeNode left;
33+
* TreeNode right;
34+
* TreeNode(int x) { val = x; }
35+
* }
36+
*/
37+
class Solution {
38+
private double diff = Double.MAX_VALUE;
39+
private int res;
40+
public int closestValue(TreeNode root, double target) {
41+
dfs(root, target);
42+
return this.res;
43+
}
44+
private void dfs(TreeNode node, double target){
45+
if(node == null) return;
46+
else{
47+
double diff = Math.abs(node.val - target);
48+
if(diff < this.diff){
49+
this.diff = diff;
50+
this.res = node.val;
51+
}
52+
dfs(target < node.val ? node.left: node.right, target);
53+
}
54+
}
55+
}
56+
```
57+
58+
* Method 2: Itegeration
59+
```Java
60+
class Solution {
61+
public int maxSubArrayLen(int[] nums, int k) {
62+
Map<Integer, Integer> map = new HashMap<>();
63+
for(int i = 1; i < nums.length; i++){
64+
nums[i] += nums[i - 1];
65+
}
66+
int max = 0;
67+
map.put(0, -1);
68+
for(int i = 0; i < nums.length; i++){
69+
if(map.containsKey(nums[i] - k)){
70+
max = Math.max(max, i - map.get(nums[i] - k));
71+
}
72+
if(!map.containsKey(nums[i])){
73+
map.put(nums[i], i);
74+
}
75+
}
76+
return max;
77+
}
78+
}
79+
```

0 commit comments

Comments
 (0)