Remove Duplicates from Sorted Array

来源:互联网 发布:阿里云动易cms安装教程 编辑:程序博客网 时间:2024/06/14 04:35

英文题目

Given a sorted array, remove the duplicates in-place such that each element appear only once 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.

中文题目

给定一个排好序的数组,去除其中重复的数字组成新的数组,返回新数组的长度,注意复杂度的问题

代码

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

更多文章

原创粉丝点击