Skip to content

Commit c69bac4

Browse files
authored
Merge pull request #183 from lightfish-zhang/master
add Solution 0026, 0075, 0141 [golang]
2 parents 566e270 + 4681cbc commit c69bac4

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @lc app=leetcode.cn id=26 lang=golang
3+
* Accepted
4+
* 161/161 cases passed (144 ms)
5+
* Your runtime beats 36.91 % of golang submissions
6+
* Your memory usage beats 40.4 % of golang submissions (8.2 MB)
7+
*/
8+
9+
func removeDuplicates(nums []int) int {
10+
if nums == nil || len(nums) == 0 {
11+
return 0
12+
}
13+
j := 0
14+
for i := 1; i < len(nums); i++ {
15+
if nums[j] != nums[i] {
16+
j++
17+
nums[j] = nums[i]
18+
}
19+
}
20+
return j + 1
21+
}

solution/0075.Sort Colors/Solution.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @lc app=leetcode.cn id=75 lang=golang
3+
* 87/87 cases passed (4 ms), memory usage 2.4 MB
4+
*/
5+
func sortColors(nums []int) {
6+
finish0 := -1 // 元素1结束的位置
7+
start2 := len(nums) // 元素2开始的位置
8+
for i := 0; i < start2; {
9+
if nums[i] == 0 {
10+
finish0++
11+
nums[i], nums[finish0] = nums[finish0], 0
12+
i++
13+
} else if nums[i] == 2 {
14+
start2--
15+
nums[i], nums[start2] = nums[start2], 2
16+
} else {
17+
i++
18+
}
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @lc app=leetcode.cn id=141 lang=golang
3+
* 17/17 cases passed (12 ms), memory usage 4 MB
4+
*/
5+
func hasCycle(head *ListNode) bool {
6+
if head == nil || head.Next == nil {
7+
return false
8+
}
9+
slow, fast := head, head.Next
10+
for {
11+
if fast == nil || fast.Next == nil {
12+
return false
13+
}
14+
if slow == fast {
15+
return true
16+
}
17+
slow, fast = slow.Next, fast.Next.Next
18+
}
19+
return false
20+
}

0 commit comments

Comments
 (0)