Skip to content

Commit 3874470

Browse files
committed
feat: add solutions to lcci problems: No. 03.05, 04.03
1 parent 968ccbb commit 3874470

File tree

8 files changed

+352
-11
lines changed

8 files changed

+352
-11
lines changed

lcci/03.05.Sort of Stacks/README.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<p><strong>示例2:</strong></p>
2020

21-
<pre><strong> 输入</strong>:
21+
<pre><strong> 输入</strong>:
2222
[&quot;SortedStack&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;isEmpty&quot;]
2323
[[], [], [], [1], [], []]
2424
<strong> 输出</strong>:
@@ -115,6 +115,48 @@ class SortedStack {
115115
*/
116116
```
117117

118+
### **Go**
119+
120+
```go
121+
type SortedStack struct {
122+
data []int
123+
}
124+
125+
func Constructor() SortedStack {
126+
return SortedStack{make([]int, 0)}
127+
}
128+
129+
func (s *SortedStack) Push(val int) {
130+
temp := make([]int, 0)
131+
for !s.IsEmpty() && s.Peek() < val {
132+
temp = append(temp, s.Peek())
133+
s.Pop()
134+
}
135+
s.data = append(s.data, val)
136+
for len(temp) > 0 {
137+
s.data = append(s.data, temp[len(temp)-1])
138+
temp = temp[:len(temp)-1]
139+
}
140+
}
141+
142+
func (s *SortedStack) Pop() {
143+
if !s.IsEmpty() {
144+
s.data = s.data[:len(s.data)-1]
145+
}
146+
}
147+
148+
func (s *SortedStack) Peek() int {
149+
if !s.IsEmpty() {
150+
return s.data[len(s.data)-1]
151+
}
152+
return -1
153+
}
154+
155+
func (s *SortedStack) IsEmpty() bool {
156+
return len(s.data) == 0
157+
}
158+
```
159+
118160
### **...**
119161

120162
```

lcci/03.05.Sort of Stacks/README_EN.md

+46-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
<pre>
1212

13-
<strong> Input</strong>:
13+
<strong> Input</strong>:
1414

1515
[&quot;SortedStack&quot;, &quot;push&quot;, &quot;push&quot;, &quot;peek&quot;, &quot;pop&quot;, &quot;peek&quot;]
1616

1717
[[], [1], [2], [], [], []]
1818

19-
<strong> Output</strong>:
19+
<strong> Output</strong>:
2020

2121
[null,null,null,1,null,2]
2222

@@ -26,13 +26,13 @@
2626

2727
<pre>
2828

29-
<strong> Input</strong>:
29+
<strong> Input</strong>:
3030

3131
[&quot;SortedStack&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;isEmpty&quot;]
3232

3333
[[], [], [], [1], [], []]
3434

35-
<strong> Output</strong>:
35+
<strong> Output</strong>:
3636

3737
[null,null,null,null,null,true]
3838

@@ -120,6 +120,48 @@ class SortedStack {
120120
*/
121121
```
122122

123+
### **Go**
124+
125+
```go
126+
type SortedStack struct {
127+
data []int
128+
}
129+
130+
func Constructor() SortedStack {
131+
return SortedStack{make([]int, 0)}
132+
}
133+
134+
func (s *SortedStack) Push(val int) {
135+
temp := make([]int, 0)
136+
for !s.IsEmpty() && s.Peek() < val {
137+
temp = append(temp, s.Peek())
138+
s.Pop()
139+
}
140+
s.data = append(s.data, val)
141+
for len(temp) > 0 {
142+
s.data = append(s.data, temp[len(temp)-1])
143+
temp = temp[:len(temp)-1]
144+
}
145+
}
146+
147+
func (s *SortedStack) Pop() {
148+
if !s.IsEmpty() {
149+
s.data = s.data[:len(s.data)-1]
150+
}
151+
}
152+
153+
func (s *SortedStack) Peek() int {
154+
if !s.IsEmpty() {
155+
return s.data[len(s.data)-1]
156+
}
157+
return -1
158+
}
159+
160+
func (s *SortedStack) IsEmpty() bool {
161+
return len(s.data) == 0
162+
}
163+
```
164+
123165
### **...**
124166

125167
```

lcci/03.05.Sort of Stacks/Solution.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
type SortedStack struct {
2+
data []int
3+
}
4+
5+
func Constructor() SortedStack {
6+
return SortedStack{make([]int, 0)}
7+
}
8+
9+
func (s *SortedStack) Push(val int) {
10+
temp := make([]int, 0)
11+
for !s.IsEmpty() && s.Peek() < val {
12+
temp = append(temp, s.Peek())
13+
s.Pop()
14+
}
15+
s.data = append(s.data, val)
16+
for len(temp) > 0 {
17+
s.data = append(s.data, temp[len(temp)-1])
18+
temp = temp[:len(temp)-1]
19+
}
20+
}
21+
22+
func (s *SortedStack) Pop() {
23+
if !s.IsEmpty() {
24+
s.data = s.data[:len(s.data)-1]
25+
}
26+
}
27+
28+
func (s *SortedStack) Peek() int {
29+
if !s.IsEmpty() {
30+
return s.data[len(s.data)-1]
31+
}
32+
return -1
33+
}
34+
35+
func (s *SortedStack) IsEmpty() bool {
36+
return len(s.data) == 0
37+
}

lcci/04.03.List of Depth/README.md

+78-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
<pre><strong>输入:</strong>[1,2,3,4,5,null,7,8]
1515

1616
1
17-
/ \
17+
/ \
1818
2 3
19-
/ \ \
19+
/ \ \
2020
4 5 7
2121
/
2222
8
@@ -28,22 +28,97 @@
2828

2929
<!-- 这里可写通用的实现逻辑 -->
3030

31+
层序遍历
32+
3133
<!-- tabs:start -->
3234

3335
### **Python3**
3436

3537
<!-- 这里可写当前语言的特殊实现逻辑 -->
3638

3739
```python
38-
40+
class Solution:
41+
def listOfDepth(self, tree: TreeNode) -> List[ListNode]:
42+
q = [tree]
43+
ans = []
44+
while q:
45+
n = len(q)
46+
head = ListNode(-1)
47+
tail = head
48+
for i in range(n):
49+
front = q.pop(0)
50+
node = ListNode(front.val)
51+
tail.next = node
52+
tail = node
53+
if front.left:
54+
q.append(front.left)
55+
if front.right:
56+
q.append(front.right)
57+
ans.append(head.next)
58+
return ans
3959
```
4060

4161
### **Java**
4262

4363
<!-- 这里可写当前语言的特殊实现逻辑 -->
4464

4565
```java
66+
class Solution {
67+
public ListNode[] listOfDepth(TreeNode tree) {
68+
Queue<TreeNode> queue = new LinkedList<>();
69+
queue.offer(tree);
70+
List<ListNode> ans = new ArrayList<>();
71+
while (!queue.isEmpty()) {
72+
int n = queue.size();
73+
ListNode head = new ListNode(-1);
74+
ListNode tail = head;
75+
for (int i = 0; i < n; i++) {
76+
TreeNode front = queue.poll();
77+
ListNode node = new ListNode(front.val);
78+
tail.next = node;
79+
tail = node;
80+
if (front.left != null) {
81+
queue.offer(front.left);
82+
}
83+
if (front.right != null) {
84+
queue.offer(front.right);
85+
}
86+
}
87+
ans.add(head.next);
88+
}
89+
return ans.toArray(new ListNode[0]);
90+
}
91+
}
92+
```
4693

94+
### **Go**
95+
96+
```go
97+
func listOfDepth(tree *TreeNode) []*ListNode {
98+
queue := make([]*TreeNode, 0)
99+
queue = append(queue, tree)
100+
ans := make([]*ListNode, 0)
101+
for len(queue) > 0 {
102+
n := len(queue)
103+
head := new(ListNode)
104+
tail := head
105+
for i := 0; i < n; i++ {
106+
front := queue[0]
107+
queue = queue[1:]
108+
node := &ListNode{Val: front.Val}
109+
tail.Next = node
110+
tail = node
111+
if front.Left != nil {
112+
queue = append(queue, front.Left)
113+
}
114+
if front.Right != nil {
115+
queue = append(queue, front.Right)
116+
}
117+
}
118+
ans = append(ans, head.Next)
119+
}
120+
return ans
121+
}
47122
```
48123

49124
### **...**

lcci/04.03.List of Depth/README_EN.md

+78-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
1
2020

21-
/ \
21+
/ \
2222

2323
2 3
2424

25-
/ \ \
25+
/ \ \
2626

2727
4 5 7
2828

@@ -38,18 +38,93 @@
3838

3939
## Solutions
4040

41+
Level order traversal.
42+
4143
<!-- tabs:start -->
4244

4345
### **Python3**
4446

4547
```python
46-
48+
class Solution:
49+
def listOfDepth(self, tree: TreeNode) -> List[ListNode]:
50+
q = [tree]
51+
ans = []
52+
while q:
53+
n = len(q)
54+
head = ListNode(-1)
55+
tail = head
56+
for i in range(n):
57+
front = q.pop(0)
58+
node = ListNode(front.val)
59+
tail.next = node
60+
tail = node
61+
if front.left:
62+
q.append(front.left)
63+
if front.right:
64+
q.append(front.right)
65+
ans.append(head.next)
66+
return ans
4767
```
4868

4969
### **Java**
5070

5171
```java
72+
class Solution {
73+
public ListNode[] listOfDepth(TreeNode tree) {
74+
Queue<TreeNode> queue = new LinkedList<>();
75+
queue.offer(tree);
76+
List<ListNode> ans = new ArrayList<>();
77+
while (!queue.isEmpty()) {
78+
int n = queue.size();
79+
ListNode head = new ListNode(-1);
80+
ListNode tail = head;
81+
for (int i = 0; i < n; i++) {
82+
TreeNode front = queue.poll();
83+
ListNode node = new ListNode(front.val);
84+
tail.next = node;
85+
tail = node;
86+
if (front.left != null) {
87+
queue.offer(front.left);
88+
}
89+
if (front.right != null) {
90+
queue.offer(front.right);
91+
}
92+
}
93+
ans.add(head.next);
94+
}
95+
return ans.toArray(new ListNode[0]);
96+
}
97+
}
98+
```
5299

100+
### **Go**
101+
102+
```go
103+
func listOfDepth(tree *TreeNode) []*ListNode {
104+
queue := make([]*TreeNode, 0)
105+
queue = append(queue, tree)
106+
ans := make([]*ListNode, 0)
107+
for len(queue) > 0 {
108+
n := len(queue)
109+
head := new(ListNode)
110+
tail := head
111+
for i := 0; i < n; i++ {
112+
front := queue[0]
113+
queue = queue[1:]
114+
node := &ListNode{Val: front.Val}
115+
tail.Next = node
116+
tail = node
117+
if front.Left != nil {
118+
queue = append(queue, front.Left)
119+
}
120+
if front.Right != nil {
121+
queue = append(queue, front.Right)
122+
}
123+
}
124+
ans = append(ans, head.Next)
125+
}
126+
return ans
127+
}
53128
```
54129

55130
### **...**

0 commit comments

Comments
 (0)