zoukankan      html  css  js  c++  java
  • Remove Duplicates from Sorted Array II

    Question:

    Follow up for "Remove Duplicates":
    What if duplicates are allowed at most twice?

    For example,
    Given sorted array nums = [1,1,1,2,2,3],

    Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn't matter what you leave beyond the new length.

    Solution:

     1 class Solution {
     2 public:
     3     int removeDuplicates(vector<int>& nums) {
     4             int n=nums.size();
     5     if(n<=2) return n;
     6     int i=2;
     7     for(int j=2;j<n;j++)
     8     {
     9         if(nums[j]!=nums[i-2])
    10         {
    11             nums[i]=nums[j];
    12             i++;
    13 
    14         }
    15     }
    16     return i;
    17         
    18     }
    19 };

  • 相关阅读:
    谷歌机器学习
    Pycharm使用conda安装的环境
    HAN模型理解2
    HAN模型理解1
    RCNN
    深度CNN
    多通道CNN
    TextCNN
    词向量2
    词向量1.md
  • 原文地址:https://www.cnblogs.com/riden/p/4631347.html
Copyright © 2011-2022 走看看