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

Commit438c7f1

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parentec1f261 commit438c7f1

4 files changed

+126
-1
lines changed

‎leetcode/116. Populating Next Right Pointers in Each Node.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,46 @@ public class Solution {
104104
}
105105
}
106106
}
107-
```
107+
```
108+
109+
###Amazon Session
110+
* Method 1: BFS
111+
```Java
112+
/*
113+
// Definition for a Node.
114+
class Node {
115+
public int val;
116+
public Node left;
117+
public Node right;
118+
public Node next;
119+
120+
public Node() {}
121+
122+
public Node(int _val,Node _left,Node _right,Node _next) {
123+
val = _val;
124+
left = _left;
125+
right = _right;
126+
next = _next;
127+
}
128+
};
129+
*/
130+
class Solution {
131+
public Node connect(Node root) {
132+
if(root == null) return root;
133+
Queue<Node> q = new LinkedList<>();
134+
q.offer(root);
135+
while(!q.isEmpty()){
136+
Queue<Node> next = new LinkedList<>();
137+
int size = q.size();
138+
for(int sz = 0; sz < size; sz++){
139+
Node cur = q.poll();
140+
cur.next = q.isEmpty() ? null: q.peek();
141+
if(cur.left != null) next.offer(cur.left);
142+
if(cur.right != null) next.offer(cur.right);
143+
}
144+
q = next;
145+
}
146+
return root;
147+
}
148+
}
149+
```

‎leetcode/252. Meeting Rooms.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,21 @@ public class MeetingRooms {
5757
}
5858
}
5959
```
60+
61+
###Amazon Session
62+
* Method 1: Sort and judge
63+
```Java
64+
class Solution {
65+
public boolean canAttendMeetings(int[][] intervals) {
66+
if(intervals == null) return false;
67+
if(intervals.length == 0) return true;
68+
Arrays.sort(intervals, (a, b)->{
69+
return a[0] == b[0] ? a[1] - b[1]: a[0] - b[0];
70+
});
71+
for(int i = 1; i < intervals.length; i++){
72+
if(intervals[i][0] < intervals[i - 1][1]) return false;
73+
}
74+
return true;
75+
}
76+
}
77+
```

‎leetcode/692. Top K Frequent Words.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,35 @@ Follow up:
6464
}
6565
}
6666
```
67+
68+
###Amazon Session
69+
* Method 1: Map + PriorityQueue
70+
```Java
71+
class Solution {
72+
private static class Node{
73+
String word;
74+
int count;
75+
public Node(String word, int count){
76+
this.word = word;
77+
this.count = count;
78+
}
79+
}
80+
public List<String> topKFrequent(String[] words, int k) {
81+
List<String> result = new ArrayList<>();
82+
Map<String, Integer> map = new HashMap<>();
83+
for(String word: words){
84+
map.put(word, map.getOrDefault(word, 0) + 1);
85+
}
86+
PriorityQueue<Node> pq = new PriorityQueue<>((a, b)->{
87+
return a.count != b.count ? b.count - a.count: a.word.compareTo(b.word);
88+
});
89+
for(Map.Entry<String, Integer> entry: map.entrySet()){
90+
pq.offer(new Node(entry.getKey(), entry.getValue()));
91+
}
92+
while(k-- > 0){
93+
result.add(pq.poll().word);
94+
}
95+
return result;
96+
}
97+
}
98+
```

‎leetcode/847. Shortest Path Visiting All Nodes.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,38 @@ Note:
6262
}
6363
```
6464

65+
###Amazon Session
66+
* Method 1: bfs + bit manipulation
67+
```Java
68+
class Solution {
69+
public int shortestPathLength(int[][] graph) {
70+
int len = graph.length;
71+
boolean[][] visited = new boolean[len][1 << len];
72+
int expect = (1 << len) - 1;
73+
Queue<int[]> q = new LinkedList<>();
74+
for(int i = 0; i < len; i++){
75+
q.offer(new int[]{i, 1 << i});
76+
}
77+
int step = -1;
78+
while(!q.isEmpty()){
79+
step++;
80+
int size = q.size();
81+
for(int sz = 0; sz < size; sz++){
82+
int[] cur = q.poll();
83+
int node = cur[0];
84+
int state = cur[1];
85+
if(state == expect) return step;
86+
if(visited[node][state]) continue;
87+
visited[node][state] = true;
88+
for(int next: graph[node]){
89+
q.offer(new int[]{next, state | (1 <<next)});
90+
}
91+
}
92+
}
93+
return step;
94+
}
95+
}
96+
```
97+
6598
###Reference
6699
1.[花花酱 LeetCode 847. Shortest Path Visiting All Nodes](http://zxi.mytechroad.com/blog/graph/leetcode-847-shortest-path-visiting-all-nodes/)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp