Skip to content

Commit b7b2a8e

Browse files
committedJun 28, 2021
feat: update golang solution to lcci problems: No. 01.09~02.02
1 parent 71124a2 commit b7b2a8e

File tree

9 files changed

+120
-0
lines changed

9 files changed

+120
-0
lines changed
 

‎lcci/01.09.String Rotation/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class Solution {
6060
}
6161
```
6262

63+
### **Go**
64+
65+
```go
66+
func isFlipedString(s1 string, s2 string) bool {
67+
return len(s1) == len(s2) && strings.Contains(s1+s1, s2)
68+
}
69+
```
70+
6371
### **...**
6472

6573
```

‎lcci/01.09.String Rotation/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ class Solution {
5656
}
5757
```
5858

59+
### **Go**
60+
61+
```go
62+
func isFlipedString(s1 string, s2 string) bool {
63+
return len(s1) == len(s2) && strings.Contains(s1+s1, s2)
64+
}
65+
```
66+
5967
### **...**
6068

6169
```
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func isFlipedString(s1 string, s2 string) bool {
2+
return len(s1) == len(s2) && strings.Contains(s1+s1, s2)
3+
}

‎lcci/02.01.Remove Duplicate Node/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,27 @@ public:
164164
};
165165
```
166166
167+
### **Go**
168+
169+
```go
170+
func removeDuplicateNodes(head *ListNode) *ListNode {
171+
if head == nil {
172+
return nil
173+
}
174+
vis := map[int]bool{head.Val: true}
175+
p := head
176+
for p.Next != nil {
177+
if vis[p.Next.Val] {
178+
p.Next = p.Next.Next
179+
} else {
180+
vis[p.Next.Val] = true
181+
p = p.Next
182+
}
183+
}
184+
return head
185+
}
186+
```
187+
167188
### **...**
168189

169190
```

‎lcci/02.01.Remove Duplicate Node/README_EN.md

+21
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,27 @@ public:
164164
};
165165
```
166166
167+
### **Go**
168+
169+
```go
170+
func removeDuplicateNodes(head *ListNode) *ListNode {
171+
if head == nil {
172+
return nil
173+
}
174+
vis := map[int]bool{head.Val: true}
175+
p := head
176+
for p.Next != nil {
177+
if vis[p.Next.Val] {
178+
p.Next = p.Next.Next
179+
} else {
180+
vis[p.Next.Val] = true
181+
p = p.Next
182+
}
183+
}
184+
return head
185+
}
186+
```
187+
167188
### **...**
168189

169190
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func removeDuplicateNodes(head *ListNode) *ListNode {
2+
if head == nil {
3+
return nil
4+
}
5+
vis := map[int]bool{head.Val: true}
6+
p := head
7+
for p.Next != nil {
8+
if vis[p.Next.Val] {
9+
p.Next = p.Next.Next
10+
} else {
11+
vis[p.Next.Val] = true
12+
p = p.Next
13+
}
14+
}
15+
return head
16+
}

‎lcci/02.02.Kth Node From End of List/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ public:
133133
};
134134
```
135135
136+
### **Go**
137+
138+
```go
139+
func kthToLast(head *ListNode, k int) int {
140+
slow, fast := head, head
141+
for i := 0; i < k; i++ {
142+
fast = fast.Next
143+
}
144+
for fast != nil {
145+
slow = slow.Next
146+
fast = fast.Next
147+
}
148+
return slow.Val
149+
}
150+
```
151+
136152
### **...**
137153

138154
```

‎lcci/02.02.Kth Node From End of List/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ public:
125125
};
126126
```
127127
128+
### **Go**
129+
130+
```go
131+
func kthToLast(head *ListNode, k int) int {
132+
slow, fast := head, head
133+
for i := 0; i < k; i++ {
134+
fast = fast.Next
135+
}
136+
for fast != nil {
137+
slow = slow.Next
138+
fast = fast.Next
139+
}
140+
return slow.Val
141+
}
142+
```
143+
128144
### **...**
129145

130146
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func kthToLast(head *ListNode, k int) int {
2+
slow, fast := head, head
3+
for i := 0; i < k; i++ {
4+
fast = fast.Next
5+
}
6+
for fast != nil {
7+
slow = slow.Next
8+
fast = fast.Next
9+
}
10+
return slow.Val
11+
}

0 commit comments

Comments
 (0)