26. Remove Duplicates from Sorted Array

原创 2016年06月01日 18:36:23

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 in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode_Easy心得:26. Remove Duplicates from Sorted Array(C语言)

26. Remove Duplicates from Sorted Array

LeetCode笔记:26. Remove Duplicates from Sorted Array

固定内存下删除有序数组中重复的数字

26. Remove Duplicates from Sorted Array(java)

26. Remove Duplicates from Sorted Array(java)

LeetCode 26: Remove Duplicates from Sorted Array

LeedCode 26: Remove Duplicates from Sorted Array的三种解题思路

Leetcode NO.26 Remove Duplicates from Sorted Array

本题题目要求如下: Given a sorted array, remove the duplicates in place such that each element appear only ...

LeetCode 26. Remove Duplicates from Sorted Array

题目: Given a sorted array, remove the duplicates in place such that each element appear only onc...

LeetCode OJ 26 Remove Duplicates from Sorted Array

毕设做累了,无聊刷个题压压惊。今天是第一次做leetcode的题目,做题顺序按照分类来做。 该题分类为线性表,题目难度为简单。 Given a sorted array, remove the d...

26. Remove Duplicates from Sorted Array [easy] (Python)

题目链接https://leetcode.com/problems/remove-duplicates-from-sorted-array/题目原文 Given a sorted array, r...

LeetCode 26_Remove Duplicates from Sorted Array

又有一段时间没有更新了,时间是非常的不够用啊,今天抽点儿时间,多刷几道吧。 这次是leetcode第26题,题目难度是属于easy的那种。并没有涉及什么高深算法,属于刚学C时练手的题目。这种题目对于...

LeetCode(26)Remove Duplicates From Sorted Array

题目如下 Given a sorted array, remove the duplicates in place such that each element appear only once an...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)