feat: 双指针
This commit is contained in:
24
26.remove-duplicates-from-sorted-array.java
Normal file
24
26.remove-duplicates-from-sorted-array.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* @lc app=leetcode id=26 lang=java
|
||||
*
|
||||
* [26] Remove Duplicates from Sorted Array
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int removeDuplicates(int[] nums) {
|
||||
if(nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int i = 0;
|
||||
for(int j = 0; j< nums.length; j++) {
|
||||
if(nums[j] != nums[i]) {
|
||||
i++;
|
||||
nums[i] = nums[j];
|
||||
}
|
||||
}
|
||||
return i+1;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user