File tree 8 files changed +122
-0
lines changed
0000-0099/0080.Remove Duplicates from Sorted Array II
0100-0199/0169.Majority Element
0200-0299/0283.Move Zeroes
8 files changed +122
-0
lines changed Original file line number Diff line number Diff line change @@ -168,6 +168,21 @@ var removeDuplicates = function (nums) {
168
168
};
169
169
```
170
170
171
+ ### ** Go**
172
+
173
+ ``` go
174
+ func removeDuplicates (nums []int ) int {
175
+ i := 0
176
+ for _ , num := range nums {
177
+ if i < 2 || num != nums[i-2 ] {
178
+ nums[i] = num
179
+ i++
180
+ }
181
+ }
182
+ return i
183
+ }
184
+ ```
185
+
171
186
### ** ...**
172
187
173
188
```
Original file line number Diff line number Diff line change @@ -148,6 +148,21 @@ var removeDuplicates = function (nums) {
148
148
};
149
149
```
150
150
151
+ ### ** Go**
152
+
153
+ ``` go
154
+ func removeDuplicates (nums []int ) int {
155
+ i := 0
156
+ for _ , num := range nums {
157
+ if i < 2 || num != nums[i-2 ] {
158
+ nums[i] = num
159
+ i++
160
+ }
161
+ }
162
+ return i
163
+ }
164
+ ```
165
+
151
166
### ** ...**
152
167
153
168
```
Original file line number Diff line number Diff line change
1
+ func removeDuplicates (nums []int ) int {
2
+ i := 0
3
+ for _ , num := range nums {
4
+ if i < 2 || num != nums [i - 2 ] {
5
+ nums [i ] = num
6
+ i ++
7
+ }
8
+ }
9
+ return i
10
+ }
Original file line number Diff line number Diff line change @@ -145,6 +145,27 @@ public class Solution {
145
145
}
146
146
```
147
147
148
+ ### ** Go**
149
+
150
+ ``` go
151
+ func majorityElement (nums []int ) int {
152
+ var cnt , major int
153
+ for _ , num := range nums {
154
+ if cnt == 0 {
155
+ major = num
156
+ cnt = 1
157
+ } else {
158
+ if major == num {
159
+ cnt++
160
+ } else {
161
+ cnt--
162
+ }
163
+ }
164
+ }
165
+ return major
166
+ }
167
+ ```
168
+
148
169
### ** ...**
149
170
150
171
```
Original file line number Diff line number Diff line change @@ -131,6 +131,27 @@ public class Solution {
131
131
}
132
132
```
133
133
134
+ ### ** Go**
135
+
136
+ ``` go
137
+ func majorityElement (nums []int ) int {
138
+ var cnt , major int
139
+ for _ , num := range nums {
140
+ if cnt == 0 {
141
+ major = num
142
+ cnt = 1
143
+ } else {
144
+ if major == num {
145
+ cnt++
146
+ } else {
147
+ cnt--
148
+ }
149
+ }
150
+ }
151
+ return major
152
+ }
153
+ ```
154
+
134
155
### ** ...**
135
156
136
157
```
Original file line number Diff line number Diff line change @@ -97,6 +97,21 @@ var moveZeroes = function (nums) {
97
97
};
98
98
```
99
99
100
+ ### ** Go**
101
+
102
+ ``` go
103
+ func moveZeroes (nums []int ) {
104
+ n := len (nums)
105
+ left := 0
106
+ for right := 0 ; right < n; right++ {
107
+ if nums[right] != 0 {
108
+ nums[left], nums[right] = nums[right], nums[left]
109
+ left++
110
+ }
111
+ }
112
+ }
113
+ ```
114
+
100
115
### ** ...**
101
116
102
117
```
Original file line number Diff line number Diff line change @@ -96,6 +96,21 @@ var moveZeroes = function (nums) {
96
96
};
97
97
```
98
98
99
+ ### ** Go**
100
+
101
+ ``` go
102
+ func moveZeroes (nums []int ) {
103
+ n := len (nums)
104
+ left := 0
105
+ for right := 0 ; right < n; right++ {
106
+ if nums[right] != 0 {
107
+ nums[left], nums[right] = nums[right], nums[left]
108
+ left++
109
+ }
110
+ }
111
+ }
112
+ ```
113
+
99
114
### ** ...**
100
115
101
116
```
Original file line number Diff line number Diff line change
1
+ func moveZeroes (nums []int ) {
2
+ n := len (nums )
3
+ left := 0
4
+ for right := 0 ; right < n ; right ++ {
5
+ if nums [right ] != 0 {
6
+ nums [left ], nums [right ] = nums [right ], nums [left ]
7
+ left ++
8
+ }
9
+ }
10
+ }
You can’t perform that action at this time.
0 commit comments