Skip to content

Commit cca1701

Browse files
committed
feat: update golang solution to lcci problems: No. 02.06, 03.01
1 parent ca21982 commit cca1701

File tree

6 files changed

+319
-8
lines changed

6 files changed

+319
-8
lines changed

lcci/02.06.Palindrome Linked List/README.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
<p><strong>示例 1:</strong></p>
1313

1414
<pre><strong>输入: </strong>1-&gt;2
15-
<strong>输出:</strong> false
15+
<strong>输出:</strong> false
1616
</pre>
1717

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

2020
<pre><strong>输入: </strong>1-&gt;2-&gt;2-&gt;1
21-
<strong>输出:</strong> true
21+
<strong>输出:</strong> true
2222
</pre>
2323

2424
<p>&nbsp;</p>
@@ -248,6 +248,51 @@ function isPalindrome(head: ListNode | null): boolean {
248248
};
249249
```
250250

251+
### **Go**
252+
253+
```go
254+
func isPalindrome(head *ListNode) bool {
255+
if head == nil {
256+
return true
257+
}
258+
m := mid(head)
259+
temp := reverse(m.Next)
260+
m.Next = nil
261+
p, q := head, temp
262+
res := true
263+
for p != nil && q != nil {
264+
if p.Val != q.Val {
265+
res = false
266+
break
267+
}
268+
p = p.Next
269+
q = q.Next
270+
}
271+
m.Next = reverse(temp)
272+
return res
273+
}
274+
275+
func mid(head *ListNode) *ListNode {
276+
slow, fast := head, head.Next
277+
for fast != nil && fast.Next != nil {
278+
slow = slow.Next
279+
fast = fast.Next.Next
280+
}
281+
return slow
282+
}
283+
284+
func reverse(head *ListNode) *ListNode {
285+
var prev *ListNode = nil
286+
for head != nil {
287+
temp := head.Next
288+
head.Next = prev
289+
prev = head
290+
head = temp
291+
}
292+
return prev
293+
}
294+
```
295+
251296
### **...**
252297

253298
```

lcci/02.06.Palindrome Linked List/README_EN.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<strong>Input: </strong>1-&gt;2
1616

17-
<strong>Output: </strong> false
17+
<strong>Output: </strong> false
1818

1919
</pre>
2020

@@ -24,7 +24,7 @@
2424

2525
<strong>Input: </strong>1-&gt;2-&gt;2-&gt;1
2626

27-
<strong>Output: </strong> true
27+
<strong>Output: </strong> true
2828

2929
</pre>
3030

@@ -248,6 +248,51 @@ function isPalindrome(head: ListNode | null): boolean {
248248
};
249249
```
250250

251+
### **Go**
252+
253+
```go
254+
func isPalindrome(head *ListNode) bool {
255+
if head == nil {
256+
return true
257+
}
258+
m := mid(head)
259+
temp := reverse(m.Next)
260+
m.Next = nil
261+
p, q := head, temp
262+
res := true
263+
for p != nil && q != nil {
264+
if p.Val != q.Val {
265+
res = false
266+
break
267+
}
268+
p = p.Next
269+
q = q.Next
270+
}
271+
m.Next = reverse(temp)
272+
return res
273+
}
274+
275+
func mid(head *ListNode) *ListNode {
276+
slow, fast := head, head.Next
277+
for fast != nil && fast.Next != nil {
278+
slow = slow.Next
279+
fast = fast.Next.Next
280+
}
281+
return slow
282+
}
283+
284+
func reverse(head *ListNode) *ListNode {
285+
var prev *ListNode = nil
286+
for head != nil {
287+
temp := head.Next
288+
head.Next = prev
289+
prev = head
290+
head = temp
291+
}
292+
return prev
293+
}
294+
```
295+
251296
### **...**
252297

253298
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
func isPalindrome(head *ListNode) bool {
2+
if head == nil {
3+
return true
4+
}
5+
m := mid(head)
6+
temp := reverse(m.Next)
7+
m.Next = nil
8+
p, q := head, temp
9+
res := true
10+
for p != nil && q != nil {
11+
if p.Val != q.Val {
12+
res = false
13+
break
14+
}
15+
p = p.Next
16+
q = q.Next
17+
}
18+
m.Next = reverse(temp)
19+
return res
20+
}
21+
22+
func mid(head *ListNode) *ListNode {
23+
slow, fast := head, head.Next
24+
for fast != nil && fast.Next != nil {
25+
slow = slow.Next
26+
fast = fast.Next.Next
27+
}
28+
return slow
29+
}
30+
31+
func reverse(head *ListNode) *ListNode {
32+
var prev *ListNode = nil
33+
for head != nil {
34+
temp := head.Next
35+
head.Next = prev
36+
prev = head
37+
head = temp
38+
}
39+
return prev
40+
}

lcci/03.01.Three in One/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,68 @@ class TripleInOne {
119119
*/
120120
```
121121

122+
### **Go**
123+
124+
```go
125+
type TripleInOne struct {
126+
data []int
127+
offset [3]int
128+
stackSize int
129+
}
130+
131+
func Constructor(stackSize int) TripleInOne {
132+
total := stackSize * 3
133+
data := make([]int, total)
134+
offset := [3]int{}
135+
for i := 0; i < 3; i++ {
136+
offset[i] = i * stackSize
137+
}
138+
return TripleInOne{
139+
data: data,
140+
offset: offset,
141+
stackSize: stackSize,
142+
}
143+
}
144+
145+
func (this *TripleInOne) Push(stackNum int, value int) {
146+
i := this.offset[stackNum]
147+
if i < (stackNum+1)*this.stackSize {
148+
this.data[i] = value
149+
this.offset[stackNum]++
150+
}
151+
}
152+
153+
func (this *TripleInOne) Pop(stackNum int) int {
154+
i := this.offset[stackNum]
155+
if i == stackNum*this.stackSize {
156+
return -1
157+
}
158+
this.offset[stackNum]--
159+
return this.data[i-1]
160+
}
161+
162+
func (this *TripleInOne) Peek(stackNum int) int {
163+
i := this.offset[stackNum]
164+
if i == stackNum*this.stackSize {
165+
return -1
166+
}
167+
return this.data[i-1]
168+
}
169+
170+
func (this *TripleInOne) IsEmpty(stackNum int) bool {
171+
return this.offset[stackNum] == stackNum*this.stackSize
172+
}
173+
174+
/**
175+
* Your TripleInOne object will be instantiated and called as such:
176+
* obj := Constructor(stackSize);
177+
* obj.Push(stackNum,value);
178+
* param_2 := obj.Pop(stackNum);
179+
* param_3 := obj.Peek(stackNum);
180+
* param_4 := obj.IsEmpty(stackNum);
181+
*/
182+
```
183+
122184
### **...**
123185

124186
```

lcci/03.01.Three in One/README_EN.md

+66-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
<pre>
1616

17-
<strong> Input</strong>:
17+
<strong> Input</strong>:
1818

1919
[&quot;TripleInOne&quot;, &quot;push&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;isEmpty&quot;]
2020

2121
[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
2222

23-
<strong> Output</strong>:
23+
<strong> Output</strong>:
2424

2525
[null, null, null, 1, -1, -1, true]
2626

@@ -32,13 +32,13 @@
3232

3333
<pre>
3434

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

3737
[&quot;TripleInOne&quot;, &quot;push&quot;, &quot;push&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;peek&quot;]
3838

3939
[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
4040

41-
<strong> Output</strong>:
41+
<strong> Output</strong>:
4242

4343
[null, null, null, null, 2, 1, -1, -1]
4444

@@ -125,6 +125,68 @@ class TripleInOne {
125125
*/
126126
```
127127

128+
### **Go**
129+
130+
```go
131+
type TripleInOne struct {
132+
data []int
133+
offset [3]int
134+
stackSize int
135+
}
136+
137+
func Constructor(stackSize int) TripleInOne {
138+
total := stackSize * 3
139+
data := make([]int, total)
140+
offset := [3]int{}
141+
for i := 0; i < 3; i++ {
142+
offset[i] = i * stackSize
143+
}
144+
return TripleInOne{
145+
data: data,
146+
offset: offset,
147+
stackSize: stackSize,
148+
}
149+
}
150+
151+
func (this *TripleInOne) Push(stackNum int, value int) {
152+
i := this.offset[stackNum]
153+
if i < (stackNum+1)*this.stackSize {
154+
this.data[i] = value
155+
this.offset[stackNum]++
156+
}
157+
}
158+
159+
func (this *TripleInOne) Pop(stackNum int) int {
160+
i := this.offset[stackNum]
161+
if i == stackNum*this.stackSize {
162+
return -1
163+
}
164+
this.offset[stackNum]--
165+
return this.data[i-1]
166+
}
167+
168+
func (this *TripleInOne) Peek(stackNum int) int {
169+
i := this.offset[stackNum]
170+
if i == stackNum*this.stackSize {
171+
return -1
172+
}
173+
return this.data[i-1]
174+
}
175+
176+
func (this *TripleInOne) IsEmpty(stackNum int) bool {
177+
return this.offset[stackNum] == stackNum*this.stackSize
178+
}
179+
180+
/**
181+
* Your TripleInOne object will be instantiated and called as such:
182+
* obj := Constructor(stackSize);
183+
* obj.Push(stackNum,value);
184+
* param_2 := obj.Pop(stackNum);
185+
* param_3 := obj.Peek(stackNum);
186+
* param_4 := obj.IsEmpty(stackNum);
187+
*/
188+
```
189+
128190
### **...**
129191

130192
```

0 commit comments

Comments
 (0)