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

Commitc564493

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parentdc513d7 commitc564493

3 files changed

+99
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
##556. Next Greater Element III
2+
3+
###Question
4+
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
5+
6+
```
7+
Example 1:
8+
Input: 12
9+
Output: 21
10+
11+
Example 2:
12+
Input: 21
13+
Output: -1
14+
```
15+
16+
###Solution
17+
* Method 1: Math
18+
1. Find the position to swap: traversal from right to left, find the first index whose value is smaller than its right index value. If no such index, return -1, which means current value is the maximum value can be represented by current numbers.
19+
2. Swap the number in that index with the first number bigger than current one, traversal from right to the left.
20+
3. Sort the values at right side of that index in ascending order.
21+
```Java
22+
classSolution {
23+
publicintnextGreaterElement(intn) {
24+
String s= n+"";
25+
char[] arr= s.toCharArray();
26+
int len= s.length(), i= len-2;
27+
for(; i>=0; i--){
28+
if(arr[i]< arr[i+1])break;
29+
}
30+
if(i<0)return-1;
31+
for(int j= len-1; j> i; j--){
32+
if(arr[j]> arr[i]){
33+
char temp= arr[i];
34+
arr[i]= arr[j];
35+
arr[j]= temp;
36+
break;
37+
}
38+
}
39+
char[] sub=Arrays.copyOfRange(arr, i+1, len);
40+
Arrays.sort(sub);
41+
for(int j= i+1; j< len; j++){
42+
arr[j]= sub[j- i-1];
43+
}
44+
long res=Long.parseLong(newString(arr));
45+
return res>Integer.MAX_VALUE?-1: (int)res;
46+
}
47+
}
48+
```

‎leetcode/636. Exclusive Time of Functions.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,33 @@ Note:
6565
return result;
6666
}
6767
}
68+
```
69+
70+
###AmazonSession
71+
*Method1:Stack
72+
```Java
73+
classSolution {
74+
publicint[]exclusiveTime(intn,List<String>logs) {
75+
int[] res=newint[n];
76+
int currentTime=0;
77+
Stack<Integer> stack=newStack<>();
78+
for(String log: logs){
79+
String[] tokens= log.split(":");
80+
int id=Integer.parseInt(tokens[0]);
81+
int time=Integer.parseInt(tokens[2]);
82+
if(tokens[1].equals("start")){
83+
if(!stack.isEmpty()){//current is begin, we need to update the previous id.
84+
int preId= stack.peek();
85+
res[preId]+= time- currentTime-1;
86+
}
87+
stack.push(id);
88+
}else{
89+
int preId= stack.pop();
90+
res[preId]+= time- currentTime+1;
91+
}
92+
currentTime= time;
93+
}
94+
return res;
95+
}
96+
}
6897
```

‎leetcode/653. Two Sum IV - Input is a BST.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,25 @@ Output: False
6161
}
6262
}
6363
}
64-
```
64+
```
65+
66+
###AmazonSession
67+
*Method1: dfs
68+
```Java
69+
classSolution {
70+
privateSet<Integer> set;
71+
privateint target;
72+
publicbooleanfindTarget(TreeNoderoot,intk) {
73+
if(root==null)returnfalse;
74+
this.set=newHashSet<>();
75+
this.target= k;
76+
return dfs(root);
77+
}
78+
privatebooleandfs(TreeNodenode){
79+
if(node==null)returnfalse;
80+
if(set.contains(this.target- node.val))returntrue;
81+
set.add(node.val);
82+
return dfs(node.left)|| dfs(node.right);
83+
}
84+
}
85+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp