go: add «80. Remove Duplicates from Sorted Array II»
URL: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
3a68075b5a
commit
4204acb546
1 changed files with 18 additions and 0 deletions
18
go/remove-duplicates-from-sorted-array-ii.go
Normal file
18
go/remove-duplicates-from-sorted-array-ii.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
func removeDuplicates(nums []int) int {
|
||||
if len(nums) <= 2 {
|
||||
return len(nums)
|
||||
}
|
||||
|
||||
k := 2
|
||||
|
||||
for i := 2; i < len(nums); i++ {
|
||||
if nums[i] != nums[k-2] {
|
||||
nums[k] = nums[i]
|
||||
k++
|
||||
}
|
||||
}
|
||||
|
||||
return k
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue