Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

80 remove duplicates 2 solved #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from remove-duplicates-from-sorted-array2
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ continually updating 😃.
* [1. Two Sum](./src/0001_two_sum/twosum.go)   *`lookup table;`*  *`hash table`*
* [26. Remove Duplicates from Sorted Array](./src/0026_remove_duplicates_from_sorted_array/rdfsa.go)   *`double index;`*  *`array`*
* [27. Remove Element](src/0027_remove_element/remove_element.go)   *`double index;`*  *`array`*
* [80. Remove Duplicates from Sorted Array II](./src/0080_remove_duplicates_from_sorted_array2/rdfsa2.go)   *`double index;`*  *`array`*
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
Expand Down
46 changes: 46 additions & 0 deletions src/0080_remove_duplicates_from_sorted_array2/rdfsa2.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
80. Remove Duplicates from Sorted Array II
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

Given a sorted array nums, remove the duplicates in-place such that
duplicates appeared at most twice and return the new length.

Do not allocate extra space for another array, you must do this
by modifying the input array in-place with O(1) extra memory.
*/
// time: 2018年12月21日

package rdfsa2

// double index
// time complexity: O(n)
// space complexity: O(1)
func removeDuplicates(nums []int) int {
n := len(nums)
if 0 == n || 1 == n {
return n
}

var (
res = 2
i = 2
index = nextDifferentCharacterIndex(nums, i, 2) // 寻找下一个满足条件的下标
)

for index < n {
res++
nums[i] = nums[index]
i++
index = nextDifferentCharacterIndex(nums, i, index+1)
}
return res
}

func nextDifferentCharacterIndex(nums []int, j int, p int) int {
for ; p < len(nums); p++ {
if nums[j-1] == nums[j-2] && nums[p] != nums[j-1] || nums[j-1] != nums[j-2] {
break
}
}
return p
}
22 changes: 22 additions & 0 deletions src/0080_remove_duplicates_from_sorted_array2/rdfsa2_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package rdfsa2

import "testing"

func TestRemoveDuplicates(t *testing.T) {
testCases := [][]int{
{1, 1, 1, 2, 2, 3},
{1, 2, 2},
{0, 0, 1, 1, 1, 1, 2, 3, 3},
{},
{1},
{2, 2},
{2, 2, 2},
}
expected := []int{5, 3, 7, 0, 1, 2, 2}

for index, data := range testCases {
if res := removeDuplicates(data); res != expected[index] {
t.Errorf("expected %d, got %d", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
|0069|[Sqrt(x)](0069_sqrtx/sqrtx.go)|Easy|*`binary search`*|
|0070|[Climbing Stairs](./0070_climbing_stairs/climbing_stairs.go)|Easy|*`dynamic programming`*|
|0076|[Minimum Window Substring](./0076_minimum_window_substring/minimum_window_substring.go)|Hard|*`sliding window`*|
|0080|[80. Remove Duplicates from Sorted Array II](0080_remove_duplicates_from_sorted_array2/rdfsa2.go)|Medium|*`double index`*|
|0094|[Binary Tree Inorder Traversal](./0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)|Medium|*`binary tree`*|
|0100|[Same Tree](./0100_same_tree/same_tree.go)|Easy|*`binary tree`*|
|0101|[Symmetric Tree](./0101_symmetric_tree/symmetric_tree.go)|Easy|*`stack;`* *`recursion; `* *`iterative`*|
Expand Down

AltStyle によって変換されたページ (->オリジナル) /