-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Add Solution 027[Java] #42
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
solution/026.Remove Duplicates from Sorted Array/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| ## 删除排序数组中的重复项 | ||
| ### 题目描述 | ||
|
|
||
| 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 | ||
|
|
||
| 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 | ||
|
|
||
| 示例 1: | ||
|
|
||
| 给定数组 nums = [1,1,2], | ||
|
|
||
| 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 | ||
|
|
||
| 你不需要考虑数组中超出新长度后面的元素。 | ||
| 示例 2: | ||
|
|
||
| 给定 nums = [0,0,1,1,1,2,2,3,3,4], | ||
|
|
||
| 函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。 | ||
|
|
||
| 你不需要考虑数组中超出新长度后面的元素。 | ||
| 说明: | ||
|
|
||
| 为什么返回数值是整数,但输出的答案是数组呢? | ||
|
|
||
| 请注意,输入数组是以"引用"方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。 | ||
|
|
||
| 你可以想象内部操作如下: | ||
|
|
||
| // nums 是以"引用"方式传递的。也就是说,不对实参做任何拷贝 | ||
| int len = removeDuplicates(nums); | ||
|
|
||
| // 在函数里修改输入数组对于调用者是可见的。 | ||
| // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。 | ||
| ``` | ||
| for (int i = 0; i < len; i++) { | ||
| print(nums[i]); | ||
| } | ||
| ``` | ||
|
|
||
| ### 解法 | ||
| 1. 维护 i 和 j 两个指针,i 从左向右遍历数组, j 指针指向当前完成去除重复元素的最后一个值。 | ||
| 2. 通过比较 nums[i] 与 nums[j] 的值判断 i 指向的元素是否为前一个元素的重复,若是,进入步骤3,否则,重复步骤2; | ||
| 3. j 向左移动,将 nums[i] 拷贝至 nums[j] 成为新的末尾元素。 | ||
|
|
||
| ```java | ||
| class Solution { | ||
| public int removeDuplicates(int[] nums) { | ||
| if(nums == null || nums.length == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| int j = 0; | ||
| for(int i = 1; i < nums.length; i++) { | ||
| if(nums[i] != nums[j]) { | ||
| nums[++j] = nums[i]; | ||
| } | ||
| } | ||
| return j + 1; | ||
| } | ||
| } | ||
| ``` |
15 changes: 15 additions & 0 deletions
solution/026.Remove Duplicates from Sorted Array/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution { | ||
| public int removeDuplicates(int[] nums) { | ||
| if(nums == null || nums.length == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| int j = 0; | ||
| for(int i = 1; i < nums.length; i++) { | ||
| if(nums[i] != nums[j]) { | ||
| nums[++j] = nums[i]; | ||
| } | ||
| } | ||
| return j + 1; | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
solution/027.Remove Element/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| ## 移除元素 | ||
| ### 题目描述 | ||
|
|
||
| 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。 | ||
|
|
||
| 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 | ||
|
|
||
| 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。 | ||
|
|
||
| 示例 1: | ||
|
|
||
| 给定 nums = [3,2,2,3], val = 3, | ||
|
|
||
| 函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。 | ||
|
|
||
| 你不需要考虑数组中超出新长度后面的元素。 | ||
| 示例 2: | ||
|
|
||
| 给定 nums = [0,1,2,2,3,0,4,2], val = 2, | ||
|
|
||
| 函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。 | ||
|
|
||
| 注意这五个元素可为任意顺序。 | ||
|
|
||
| 你不需要考虑数组中超出新长度后面的元素。 | ||
|
|
||
| ### 解法 | ||
| 1. 维护 i 和 end 两个指针,end 指向数组尾部,i 从左向右遍历数组, | ||
| 2. 若 nums[i] == val, 则把数组尾部的值 nums[end] 拷贝至 i 的位置,然后将 end 指针向左移动;否则,i 向右移动,继续遍历数组。 | ||
| 3. 这样当两个 i 与 end 相遇时,end 左边的所以 val 元素都被 end 右边的非 val 元素替换。 | ||
|
|
||
| ```java | ||
| class Solution { | ||
| public int removeElement(int[] nums, int val) { | ||
| if(nums == null || nums.length == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| int end = nums.length - 1; | ||
| int i = 0; | ||
| while(i <= end) { | ||
| if(nums[i] == val) { | ||
| nums[i] = nums[end]; | ||
| end--; | ||
| } | ||
| else { | ||
| i++; | ||
| } | ||
| } | ||
| return end + 1; | ||
|
|
||
| } | ||
| } | ||
| ``` |
21 changes: 21 additions & 0 deletions
solution/027.Remove Element/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Solution { | ||
| public int removeElement(int[] nums, int val) { | ||
| if(nums == null || nums.length == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| int end = nums.length - 1; | ||
| int i = 0; | ||
| while(i <= end) { | ||
| if(nums[i] == val) { | ||
| nums[i] = nums[end]; | ||
| end--; | ||
| } | ||
| else { | ||
| i++; | ||
| } | ||
| } | ||
| return end + 1; | ||
|
|
||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.