Skip to content

Commit 5fc32ef

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parent c475a62 commit 5fc32ef

File tree

3 files changed

+204
-2
lines changed

3 files changed

+204
-2
lines changed

leetcode/12. Integer to Roman.md

+73-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
11
## 12. Integer to Roman
2+
3+
### Question:
4+
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
5+
6+
Symbol Value
7+
I 1
8+
V 5
9+
X 10
10+
L 50
11+
C 100
12+
D 500
13+
M 1000
14+
15+
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
16+
17+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
18+
* I can be placed before V (5) and X (10) to make 4 and 9.
19+
* X can be placed before L (50) and C (100) to make 40 and 90.
20+
* C can be placed before D (500) and M (1000) to make 400 and 900.
21+
22+
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
23+
24+
```
25+
Example 1:
26+
27+
Input: 3
28+
Output: "III"
29+
30+
Example 2:
31+
32+
Input: 4
33+
Output: "IV"
34+
35+
Example 3:
36+
37+
Input: 9
38+
Output: "IX"
39+
40+
Example 4:
41+
42+
Input: 58
43+
Output: "LVIII"
44+
Explanation: L = 50, V = 5, III = 3.
45+
46+
Example 5:
47+
48+
Input: 1994
49+
Output: "MCMXCIV"
50+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
51+
```
52+
253
### Thinking:
354
* Method1:
455
Compare and append.
@@ -32,4 +83,25 @@ M 1000
3283
return sb.toString();
3384
}
3485
}
35-
```
86+
```
87+
88+
### Amazon Session
89+
* Method 1: String
90+
```Java
91+
class Solution {
92+
private static final String[] roman = {"M", "CM","D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
93+
private static final int[] n = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
94+
public String intToRoman(int num) {
95+
StringBuilder sb = new StringBuilder();
96+
int index = 0;
97+
while(num != 0){
98+
while(num >= n[index]){
99+
sb.append(roman[index]);
100+
num -= n[index];
101+
}
102+
index++;
103+
}
104+
return sb.toString();
105+
}
106+
}
107+
```

leetcode/560. Subarray Sum Equals K.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,25 @@ Note:
5959
return res;
6060
}
6161
}
62-
```
62+
```
63+
64+
### Amazon session
65+
* Method 1: Prefix Sum + HashMap, use a map to record number of one sum occurs.
66+
```Java
67+
class Solution {
68+
public int subarraySum(int[] nums, int k) {
69+
int res = 0;
70+
Map<Integer, Integer> map = new HashMap<>();
71+
map.put(0, 1);
72+
int sum = 0;
73+
for(int i = 0; i < nums.length; i++){
74+
sum += nums[i];
75+
Integer freq = map.get(sum - k); // sum - target = k
76+
if(freq != null) res += freq;
77+
map.put(sum, map.getOrDefault(sum, 0) + 1);
78+
}
79+
return res;
80+
}
81+
}
82+
```
83+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
## 662. Maximum Width of Binary Tree
2+
3+
### Question
4+
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.
5+
6+
The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.
7+
8+
```
9+
Example 1:
10+
11+
Input:
12+
13+
1
14+
/ \
15+
3 2
16+
/ \ \
17+
5 3 9
18+
19+
Output: 4
20+
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
21+
22+
Example 2:
23+
24+
Input:
25+
26+
1
27+
/
28+
3
29+
/ \
30+
5 3
31+
32+
Output: 2
33+
Explanation: The maximum width existing in the third level with the length 2 (5,3).
34+
35+
Example 3:
36+
37+
Input:
38+
39+
1
40+
/ \
41+
3 2
42+
/
43+
5
44+
45+
Output: 2
46+
Explanation: The maximum width existing in the second level with the length 2 (3,2).
47+
48+
Example 4:
49+
50+
Input:
51+
52+
1
53+
/ \
54+
3 2
55+
/ \
56+
5 9
57+
/ \
58+
6 7
59+
Output: 8
60+
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
61+
```
62+
63+
Note: Answer will in the range of 32-bit signed integer.
64+
65+
### Solution
66+
* Method 1: Binary Full Tree
67+
1. node.left = node * 2 - 1
68+
2. node.right = node * 2;
69+
```Java
70+
/**
71+
* Definition for a binary tree node.
72+
* public class TreeNode {
73+
* int val;
74+
* TreeNode left;
75+
* TreeNode right;
76+
* TreeNode(int x) { val = x; }
77+
* }
78+
*/
79+
class Solution {
80+
private static final class Node{
81+
TreeNode node;
82+
int index;
83+
public Node(TreeNode node, int index){
84+
this.node = node;
85+
this.index = index;
86+
}
87+
}
88+
public int widthOfBinaryTree(TreeNode root) {
89+
if(root == null) return 0;
90+
Queue<Node> q = new LinkedList<>();
91+
int res = 0;
92+
q.offer(new Node(root, 1));
93+
while(!q.isEmpty()){
94+
int size = q.size();
95+
int left = Integer.MAX_VALUE, right = Integer.MIN_VALUE;
96+
for(int sz = 0; sz < size; sz++){
97+
Node node = q.poll();
98+
int index = node.index;
99+
left = Math.min(index, left);
100+
right = Math.max(right, index);
101+
if(node.node.left != null) q.offer(new Node(node.node.left, 2 * index - 1));
102+
if(node.node.right != null) q.offer(new Node(node.node.right, 2 * index));
103+
}
104+
res = Math.max(res, right - left + 1);
105+
}
106+
return res;
107+
}
108+
}
109+
```

0 commit comments

Comments
 (0)