26. Remove Duplicates from Sorted Array java solutions

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.

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public int removeDuplicates(int[] nums) {
 3         if(nums.length == 0 || nums.length == 1) return nums.length;
 4         int len = 1;
 5         for(int i = 1;i<nums.length;i++){
 6             if(nums[i] != nums[i-1]) nums[len++] = nums[i];
 7         }
 8         return len;
 9     }
10 }
时间: 2024-08-27 06:35:52

26. Remove Duplicates from Sorted Array java solutions的相关文章

26. Remove Duplicates from Sorted Array【leetcode】,数组,array,java,算法

26. Remove Duplicates from Sorted Array 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 mem

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] 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 consta

&lt;LeetCode OJ&gt; 26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array My Submissions Question Total Accepted: 104150 Total Submissions: 322188 Difficulty: Easy Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

26. Remove Duplicates from Sorted Array &amp;&amp; 80. Remove Duplicates from Sorted Array II

26. Remove Duplicates from Sorted Array 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 mem

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

leetCode 26. Remove Duplicates from Sorted Array 数组

26. Remove Duplicates from Sorted Array 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 mem

leetcode 26. Remove Duplicates from Sorted Array 、80. Remove Duplicates from Sorted Array II

两个题类似,第一个题是不允许有重复的数字,第二个题是允许每个数字最多重复两个,两个题目都要求在原数组上进行操作,并返回生成数组的长度,即空间复杂度为O(1). 两个题都是使用双指针,第一个指针指向生成新的数组的最后一个位置,第二个指针指向当前进行判断的位置. 唯一不同的是, 第二个题需要设置一个变量来控制重复的个数是否超过2 26. Remove Duplicates from Sorted Array class Solution { public: int removeDuplicates(

[Leetcode][Python]26: Remove Duplicates from Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 26: Remove Duplicates from Sorted Arrayhttps://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear

No.26 Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array 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.