zoukankan      html  css  js  c++  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 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.

     1 class Solution {
     2 public:
     3     int removeDuplicates(vector<int>& nums) {
     4         int len = nums.size();
     5         if(len == 0){
     6             return 0;
     7         }
     8         
     9         int j = 0;
    10         for(int i = 1; i < len; i++){
    11             if(nums[j] != nums[i]){
    12                 nums[++j] = nums[i];
    13             }
    14         }
    15         return j+1;
    16     }
    17 };
  • 相关阅读:
    Valid Parentheses
    3Sum
    泛型(一)
    Longest Common Prefix
    Roman to Integer
    Integer to Roman
    Container With Most Water
    知道创宇研发技能表v2.2
    anti-dns pinning 攻击
    dominator
  • 原文地址:https://www.cnblogs.com/sankexin/p/5861549.html
Copyright © 2011-2022 走看看