Skip to content

Commit b7e4855

Browse files
committed
[Function add]
1. Add leetcode solutions with amazon tag.
1 parent 0a3d891 commit b7e4855

4 files changed

+150
-2
lines changed

leetcode/149. Max Points on a Line.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,45 @@ NOTE: input types have been changed on April 15, 2019. Please reset to default c
110110
return res;
111111
}
112112
}
113-
```
113+
```
114+
115+
### Amazon Session
116+
* Method 1: HashMap + slope
117+
```Java
118+
class Solution {
119+
private static final String inf = "inf";
120+
public int maxPoints(int[][] points) {
121+
int len = points.length;
122+
if(len <= 2) return len;
123+
int res = 0;
124+
for(int i = 0; i < len; i++){
125+
int[] a = points[i];
126+
Map<String, Integer> map = new HashMap<>();
127+
int base = 1, max = 0;
128+
for(int j = i + 1; j < len; j++){
129+
int[] b = points[j];
130+
if(a[0] == b[0] && a[1] == b[1]){
131+
base++;
132+
continue;
133+
}else{
134+
String slope = "";
135+
if(a[0] == b[0]){//same straight line, slope = inf
136+
map.put(inf, map.getOrDefault(inf, 0) + 1);
137+
slope = inf;
138+
}else{
139+
int gcd = gcd(a[1] - b[1], a[0] - b[0]);
140+
slope = (a[1] - b[1]) / gcd + "_" + (a[0] - b[0]) / gcd;
141+
map.put(slope, map.getOrDefault(slope, 0) + 1);
142+
}
143+
max = Math.max(max, map.get(slope));
144+
}
145+
}
146+
res = Math.max(res, base + max);
147+
}
148+
return res;
149+
}
150+
private int gcd(int a, int b){
151+
return b == 0 ? a: gcd(b, a % b);
152+
}
153+
}
154+
```

leetcode/39. Combination Sum.md

+26
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,29 @@ class Solution {
110110
}
111111
}
112112
```
113+
114+
### Amazon Session
115+
* Method 1: recursion
116+
```Java
117+
class Solution {
118+
private List<List<Integer>> result;
119+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
120+
this.result = new ArrayList<>();
121+
dfs(candidates, 0, target, 0, new ArrayList<>());
122+
return result;
123+
}
124+
private void dfs(int[] candidates, int sum, int target, int index, List<Integer> temp){
125+
if(sum == target){
126+
List<Integer> res = new ArrayList<>();
127+
res.addAll(temp);
128+
result.add(res);
129+
}else if(sum < target){
130+
for(int i = index; i < candidates.length; i++){
131+
temp.add(candidates[i]);
132+
dfs(candidates, sum + candidates[i], target, i, temp);
133+
temp.remove(temp.size() - 1);
134+
}
135+
}
136+
}
137+
}
138+
```

leetcode/394. Decode String.md

+43
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,46 @@ class Solution {
143143
}
144144
}
145145
```
146+
147+
### Amazon Session
148+
* Method 1: Recursion: Need to pay attention to the digits since its length could be longer than 1.
149+
```Java
150+
class Solution {
151+
private int index = 0;
152+
private int len;
153+
private char[] arr;
154+
public String decodeString(String s) {
155+
if(s == null || s.length() == 0) return s;
156+
this.arr = ("1[" + s + "]").toCharArray();
157+
this.len = this.arr.length;
158+
return decode();
159+
}
160+
private String decode(){
161+
int time = 1;
162+
StringBuilder result = new StringBuilder();
163+
while(index < len){
164+
char c = arr[index];
165+
if(Character.isDigit(c)){
166+
time = c - '0';
167+
index++;
168+
while(Character.isDigit(arr[index])){
169+
time = time * 10 + arr[index++] - '0';
170+
}
171+
index ++;
172+
String sub = decode();
173+
for(int i = 0; i < time; i++){
174+
result.append(sub);
175+
}
176+
time = 1;
177+
}else if(Character.isLetter(c)){
178+
result.append(c);
179+
index++;
180+
}else if(c == ']'){
181+
index++;
182+
return result.toString();
183+
}
184+
}
185+
return result.toString();
186+
}
187+
}
188+
```

leetcode/635. Design Log Storage System.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,42 @@ Note:
6969
* obj.put(id,timestamp);
7070
* List<Integer> param_2 = obj.retrieve(s,e,gra);
7171
*/
72-
```
72+
```
73+
74+
### Amazon Session
75+
* Method 1: Map
76+
```Java
77+
class LogSystem {
78+
private Map<Integer, String> save;
79+
private Map<String, Integer> g;
80+
//2016:01:01:00:00:00
81+
//Year:Month:Day:Hour:Minute:Second
82+
public LogSystem() {
83+
this.save = new HashMap<>();
84+
this.g = new HashMap<>();
85+
g.put("Year", 4);
86+
g.put("Month", 7);
87+
g.put("Day", 10);
88+
g.put("Hour", 13);
89+
g.put("Minute", 16);
90+
g.put("Second", 19);
91+
}
92+
93+
public void put(int id, String timestamp) {
94+
save.put(id, timestamp);
95+
}
96+
97+
public List<Integer> retrieve(String s, String e, String gra) {
98+
List<Integer> result = new ArrayList<>();
99+
int index = g.get(gra);
100+
String ss = s.substring(0, index);
101+
String es = e.substring(0, index);
102+
for(Map.Entry<Integer, String> entry: save.entrySet()){
103+
// System.out.println(entry.getValue());
104+
String sub = entry.getValue().substring(0, index);
105+
if(sub.compareTo(es) <= 0 && sub.compareTo(ss) >= 0) result.add(entry.getKey());
106+
}
107+
return result;
108+
}
109+
}
110+
```

0 commit comments

Comments
 (0)