Skip to content

Commit 9bc6361

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parent 618427f commit 9bc6361

7 files changed

+309
-2
lines changed

leetcode/155. Min Stack.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,41 @@ class MinStack {
175175
* int param_3 = obj.top();
176176
* int param_4 = obj.getMin();
177177
*/
178-
```
178+
```
179+
180+
### Third Time
181+
* Method 1: stack
182+
```Java
183+
class MinStack {
184+
private Stack<Integer> numStack;
185+
private Stack<Integer> minStack;
186+
/** initialize your data structure here. */
187+
public MinStack() {
188+
this.numStack = new Stack<>();
189+
this.minStack = new Stack<>();
190+
minStack.push(Integer.MAX_VALUE);
191+
}
192+
public void push(int x) {
193+
numStack.push(x);
194+
minStack.push(Math.min(x, minStack.peek()));
195+
}
196+
public void pop() {
197+
numStack.pop();
198+
minStack.pop();
199+
}
200+
public int top() {
201+
return numStack.peek();
202+
}
203+
public int getMin() {
204+
return minStack.peek();
205+
}
206+
}
207+
/**
208+
* Your MinStack object will be instantiated and called as such:
209+
* MinStack obj = new MinStack();
210+
* obj.push(x);
211+
* obj.pop();
212+
* int param_3 = obj.top();
213+
* int param_4 = obj.getMin();
214+
*/
215+
```

leetcode/295.Find Median from Data Stream.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,45 @@ class MedianFinder {
9292
* obj.addNum(num);
9393
* double param_2 = obj.findMedian();
9494
*/
95-
```
95+
```
96+
97+
### Third Time
98+
* Method 1: PriorityQueue
99+
```Java
100+
class MedianFinder {
101+
PriorityQueue<Integer> pq1;
102+
PriorityQueue<Integer> pq2;
103+
/** initialize your data structure here. */
104+
public MedianFinder() {
105+
pq2 = new PriorityQueue<>();
106+
pq1 = new PriorityQueue<>(new Comparator<Integer>(){
107+
@Override
108+
public int compare(Integer a, Integer b){
109+
return b - a;
110+
}
111+
});
112+
}
113+
114+
public void addNum(int num) {
115+
pq1.offer(num);
116+
pq2.offer(pq1.poll());
117+
if(pq1.size() < pq2.size())
118+
pq1.offer(pq2.poll());
119+
}
120+
121+
public double findMedian() {
122+
if(pq1.size() == pq2.size()){
123+
return (double)(pq1.peek() + pq2.peek()) / 2;
124+
}else{
125+
return (double) pq1.peek();
126+
}
127+
}
128+
}
129+
130+
/**
131+
* Your MedianFinder object will be instantiated and called as such:
132+
* MedianFinder obj = new MedianFinder();
133+
* obj.addNum(num);
134+
* double param_2 = obj.findMedian();
135+
*/
136+
```

leetcode/449. Serialize and Deserialize BST.md

+58
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,61 @@ Note: Do not use class member/global/static variables to store states. Your seri
116116
// Codec codec = new Codec();
117117
// codec.deserialize(codec.serialize(root));
118118
```
119+
120+
### Second Time
121+
* Method 1: recursion
122+
```Java
123+
/**
124+
* Definition for a binary tree node.
125+
* public class TreeNode {
126+
* int val;
127+
* TreeNode left;
128+
* TreeNode right;
129+
* TreeNode(int x) { val = x; }
130+
* }
131+
*/
132+
public class Codec {
133+
private static final char NULL = '#';
134+
private static final char split = ',';
135+
// Encodes a tree to a single string.
136+
public String serialize(TreeNode root) {
137+
StringBuilder sb = new StringBuilder();
138+
serialize(root, sb);
139+
return sb.toString();
140+
}
141+
private void serialize(TreeNode node, StringBuilder sb){
142+
if(node == null){
143+
sb.append(NULL).append(split);
144+
return;
145+
}else{
146+
sb.append(node.val).append(split);
147+
serialize(node.left, sb);
148+
serialize(node.right, sb);
149+
}
150+
151+
}
152+
// Decodes your encoded data to tree.
153+
public TreeNode deserialize(String data) {
154+
String[] tokens = data.split(",");
155+
Queue<String> q = new LinkedList<>();
156+
for(String token : tokens){
157+
q.offer(token);
158+
}
159+
return deserialize(q);
160+
}
161+
private TreeNode deserialize(Queue<String> q){
162+
String val = q.poll();
163+
if(val.equals("" + NULL)) return null;
164+
else{
165+
TreeNode node = new TreeNode(Integer.parseInt(val));
166+
node.left = deserialize(q);
167+
node.right = deserialize(q);
168+
return node;
169+
}
170+
}
171+
}
172+
173+
// Your Codec object will be instantiated and called as such:
174+
// Codec codec = new Codec();
175+
// codec.deserialize(codec.serialize(root));
176+
```

leetcode/686. Repeated String Match.md

Whitespace-only changes.

leetcode/76. Minimum Window Substring.md

+42
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,45 @@ class Solution {
133133
}
134134
}
135135
```
136+
137+
### Third Time
138+
* Method 1: Sliding Window
139+
```Java
140+
class Solution {
141+
public String minWindow(String s, String t) {
142+
if(s.length() < t.length()) return "";
143+
int slow = 0, matchNum = 0, minLen = Integer.MAX_VALUE, index = 0;
144+
Map<Character, Integer> wordDict = constructMap(t);
145+
for(int fast = 0; fast < s.length(); fast++){
146+
char c = s.charAt(fast);
147+
if(!wordDict.containsKey(c)) continue;
148+
int freq = wordDict.get(c);
149+
wordDict.put(c, freq - 1);
150+
if(freq == 1){
151+
matchNum++;
152+
}
153+
while(matchNum == wordDict.size()){
154+
if(fast - slow + 1 < minLen){
155+
minLen = fast - slow + 1;
156+
index = slow;
157+
}
158+
char left = s.charAt(slow++);
159+
if(!wordDict.containsKey(left)) continue;
160+
int count = wordDict.get(left);
161+
wordDict.put(left, count + 1);
162+
if(count == 0){ //0->1
163+
matchNum--;
164+
}
165+
}
166+
}
167+
return minLen == Integer.MAX_VALUE ? "": s.substring(index, index + minLen);
168+
}
169+
private Map<Character, Integer> constructMap(String t){
170+
Map<Character, Integer> map = new HashMap<>();
171+
for(char c : t.toCharArray()){
172+
map.put(c, map.getOrDefault(c, 0) + 1);
173+
}
174+
return map;
175+
}
176+
}
177+
```
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## 811. Subdomain Visit Count
2+
3+
### Question
4+
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.
5+
6+
Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".
7+
8+
We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.
9+
10+
```
11+
Example 1:
12+
Input:
13+
["9001 discuss.leetcode.com"]
14+
Output:
15+
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
16+
Explanation:
17+
We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.
18+
19+
Example 2:
20+
Input:
21+
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
22+
Output:
23+
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
24+
Explanation:
25+
We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
26+
```
27+
28+
Notes:
29+
* The length of cpdomains will not exceed 100.
30+
* The length of each domain name will not exceed 100.
31+
* Each address will have either 1 or 2 "." characters.
32+
* The input count in any count-paired domain will not exceed 10000.
33+
* The answer output can be returned in any order.
34+
35+
### Thinking:
36+
* Method 1: HashTable
37+
```Java
38+
class Solution {
39+
public List<String> subdomainVisits(String[] cpdomains) {
40+
Map<String, Integer> map = new HashMap<>();
41+
for(String domain : cpdomains){
42+
String[] tokens = domain.split(" ");
43+
int count = Integer.parseInt(tokens[0]);
44+
String d = tokens[1];
45+
for(int i = 0; i < d.length(); i++){
46+
char c = d.charAt(i);
47+
if(c != '.') continue;
48+
String key = d.substring(i + 1, d.length());
49+
map.put(key, map.getOrDefault(key, 0) + count);
50+
}
51+
map.put(d, map.getOrDefault(d, 0) + count);
52+
}
53+
List<String> result = new ArrayList<>();
54+
for(Map.Entry<String, Integer> entry: map.entrySet()){
55+
result.add(entry.getValue() + " " + entry.getKey());
56+
}
57+
return result;
58+
}
59+
}
60+
```
61+

leetcode/937. Reorder Log Files.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## 937. Reorder Log Files
2+
3+
### Question
4+
You have an array of logs. Each log is a space delimited string of words.
5+
6+
For each log, the first word in each log is an alphanumeric identifier. Then, either:
7+
8+
1. Each word after the identifier will consist only of lowercase letters, or;
9+
2. Each word after the identifier will consist only of digits.
10+
11+
We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.
12+
13+
Reorder the logs so that all of the letter-logs come before any digit-log. The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. The digit-logs should be put in their original order.
14+
15+
Return the final order of the logs.
16+
17+
```
18+
Example 1:
19+
20+
Input: ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
21+
Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
22+
```
23+
24+
Note:
25+
* 0 <= logs.length <= 100
26+
* 3 <= logs[i].length <= 100
27+
* logs[i] is guaranteed to have an identifier, and a word after the identifier.
28+
29+
### Thinking:
30+
* Method 1: Comparator + Sort
31+
```Java
32+
class Solution {
33+
public String[] reorderLogFiles(String[] logs) {
34+
int len = logs.length;
35+
if(len == 0) return logs;
36+
String[] result = new String[len];
37+
List<String> numList = new ArrayList<>();
38+
List<String> cList = new ArrayList<>();
39+
for(String log : logs){
40+
if(isNumber(log)) numList.add(log);
41+
else cList.add(log);
42+
}
43+
Collections.sort(cList, new Comparator<String>(){
44+
@Override
45+
public int compare(String a, String b){
46+
int index1 = a.indexOf(" ");
47+
int index2 = b.indexOf(" ");
48+
int cmp = a.substring(index1 + 1).compareTo(b.substring(index2 + 1));
49+
if(cmp != 0) return cmp;
50+
return a.substring(0, index1).compareTo(b.substring(0, index2));
51+
}
52+
});
53+
int count = 0;
54+
for(String s: cList){
55+
result[count++] = s;
56+
}
57+
for(String s : numList){
58+
result[count++] = s;
59+
}
60+
return result;
61+
}
62+
private boolean isNumber(String s){
63+
int index = s.indexOf(" ");
64+
return Character.isDigit(s.charAt(index + 1));
65+
}
66+
}
67+
```
68+

0 commit comments

Comments
 (0)