File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed
0026.Remove Duplicates from Sorted Array Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments