# LeetCode 第二十六题 删除排序数组中的重复项

## Java

    public static int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0||nums.length==1)
return nums.length;
int current_location = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[current_location] == nums[current_location - 1]) {
for (int j = current_location; j < nums.length - 1; j++) {
nums[j] = nums[j + 1];
} else {
current_location++;
}
}
return current_location;
}

## C++

class Solution
{
public:
int removeDuplicates(vector<int>&nums)
{
if(nums.size()==0||nums.size()==1)return nums.size();
int current_location=1;
for(int i=1; i<nums.size(); i++)
{
if(nums[current_location]==nums[current_location-1])
{
for(int j=current_location; j<nums.size()-1; j++)
{
nums[j]=nums[j+1];
}
}
else
{
current_location++;
}
}
return current_location;
}
};

## Python

class Solution(object):
def removeDuplicates(self, nums):
if len(nums) == 0 or len(nums) == 1:
return len(nums)
current_position = 1
for i in range(len(nums)):
if nums[current_position] == nums[current_position - 1]:
for j in range(current_position, len(nums) - 1):
nums[j] = nums[j + 1]
else:
current_position += 1
if current_position >= len(nums):
break
return current_position


public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120