Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb7e4855

Browse files
committed
[Function add]
1. Add leetcode solutions with amazon tag.
1 parent0a3d891 commitb7e4855

4 files changed

+150
-2
lines changed

‎leetcode/149. Max Points on a Line.md

Lines changed: 42 additions & 1 deletion
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+
###AmazonSession
116+
*Method1:HashMap+ slope
117+
```Java
118+
classSolution {
119+
privatestaticfinalString inf="inf";
120+
publicintmaxPoints(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=newHashMap<>();
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+
privateintgcd(inta,intb){
151+
return b==0? a: gcd(b, a% b);
152+
}
153+
}
154+
```

‎leetcode/39. Combination Sum.md

Lines changed: 26 additions & 0 deletions
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

Lines changed: 43 additions & 0 deletions
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

Lines changed: 39 additions & 1 deletion
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+
###AmazonSession
75+
*Method1:Map
76+
```Java
77+
classLogSystem {
78+
privateMap<Integer,String> save;
79+
privateMap<String,Integer> g;
80+
//2016:01:01:00:00:00
81+
//Year:Month:Day:Hour:Minute:Second
82+
publicLogSystem() {
83+
this.save=newHashMap<>();
84+
this.g=newHashMap<>();
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+
publicvoidput(intid,Stringtimestamp) {
94+
save.put(id, timestamp);
95+
}
96+
97+
publicList<Integer>retrieve(Strings,Stringe,Stringgra) {
98+
List<Integer> result=newArrayList<>();
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)

[8]ページ先頭

©2009-2025 Movatter.jp