• num 26


    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

    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            vector<int>::size_type it1;
            vector<int>::size_type it2;
            if(nums.size() < 2) return nums.size();
            for(it1=1,it2=0;it1<nums.size();it1++)
            {
                if(nums[it2]!=nums[it1])
                {
                    ;
                    nums[++it2]=nums[it1];
                    
                }
            }
            return it2+1;
        }
    };
    

     若使用STL库的函数,可直接写为return distance(A, unique(A, A + n));

    unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除。他是c++中的函数,所以头文件要加#include<iostream.h>,具体用法如下:

        int num[100];

       unique(num,mun+n)返回的是num去重后的尾地址,之所以说比不真正把重复的元素删除,其实是,该函数把重复的元素一到后面去了,然后依然保存到了原数组中,然后返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。

    distance则用于求出迭代器之间的距离。

  • 相关阅读:
    php mysql 查询
    正则表达式 常用匹配模式
    正则 去除html标签
    PHP 操作MySQL
    MySQL 的中文乱码问题终结
    [转]BP人工神经网络的介绍与实现
    [转]BP人工神经网络的C++实现
    [转]高效BP(反向传播算法)
    [转]反向传播算法BP (弄懂为什么是反向而不是正向)
    [转]BP神经网络梯度下降算法
  • 原文地址:https://www.cnblogs.com/xds1224/p/5140545.html
Copyright © 2020-2023  润新知