1. int arr[] = {,,,,};
  2. //把数组的值赋给vector
  3. vector<int> vec(arr, arr+sizeof(arr)/sizeof(int));

解法一:

时间复杂度O(n)

空间复杂度O(1)

  1. class Solution {
  2. public:
  3. void moveZeroes(vector<int>& nums) {
  4. int k = ; //nums中,[0,...k)的元素均为非0元素
  5. //遍历到第i个元素后,保证[0,...i)中所有非0元素
  6. //都按照顺序排列在[0,...k)中
  7. for(int i=;i<nums.size();i++){
  8. if(nums[i]){
  9. nums[k++] = nums[i];
  10. }
  11. }
  12. //将nums剩余的位置放置为0
  13. for(int i=k;i<nums.size();i++)
  14. nums[i] = ;
  15. }
  16. };

解法二:将非0元素与0元素交换位置,其中k指向非零元素的位置,且为了不让两个0元素之间相互交换位置,则增加一个判断条件( i != k)

  1. class Solution {
  2. public:
  3. void moveZeroes(vector<int>& nums) {
  4. int k = ; //nums中,[0,...k)的元素均为非0元素
  5. //遍历到第i个元素后,保证[0,...i)中所有非0元素
  6. //都按照顺序排列在[0,...k)中
  7. //同时,[k,...i]为0
  8. for(int i=;i<nums.size();i++){
  9. if(nums[i]){
  10. if(i!=k)
  11. swap(nums[k++] , nums[i]);
  12. else
  13. k++;
  14. }
  15. }
  16. }
  17. };

我用了一个比较简便的解法,使用了vector的erase()函数直接删除等于val的元素(相当于下标自动加了一,即表示的是删除元素的下一个元素),剩余的元素个数可以直接由size()得到。

需注意的:不能使用remove()的原因是:它是将等于val的元素放到vector的尾部,返回新的end()值(非val部分的end),但并不减少vector的size。

  1. class Solution {
  2. public:
  3. int removeElement(vector<int>& nums, int val) {
  4. int k = ;
  5. int i = ;
  6. while(i<nums.size()){
  7. if(nums[i] == val)
  8. nums.erase(nums.begin()+i); //删除下标为i的元素
  9. else
  10. i++;
  11. }
  12. return nums.size();
  13. }
  14. };

所以要考虑上述三个问题。

解法一:快慢指针。用两个指针,慢指针来记录不重复元素的个数,快指针遍历整个数组,若慢指针指向的元素不等于快指针指向的元素,则赋值。

注意:判断当数组的长度为0时返回0,否则容易出现野指针报错。

  1. class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            if(nums.empty())
                return 0;
            int count=0;   //记录不重复元素的个数
            int j=1;   //遍历整个vector
            while(j<nums.size()){
                if(nums[count] != nums[j]){
                    count++;
                    nums[count] = nums[j];
                }
                j++;
            }
            return count+1;
        }
    };

解法二:(24s)

和解法二的思路相似,执行时间较多,比较相邻两个元素是否相同,若相同则i++;若不相同则将nums[i]赋给nums[k]。

  1. class Solution {
  2. public:
  3. int removeDuplicates(vector<int>& nums) {
  4. if (nums.empty()) return ;
  5. int k = ;
  6. for (int i = ; i < nums.size(); ++i)
  7. {
  8. if (nums[i] != nums[i - ])
  9. {
  10. nums[k++] = nums[i]; k++的作用是最终的k是数组的长度
  11. }
  12. }
  13. return k;
  14. }
  15. };

思路:k记录最多重复两次的数组下标,若( 下标为i的元素不等于k) 或者(i等于k 但是 k和k-1不相等)  则把下标为i的元素赋给k+1的元素

  1. class Solution {
  2. public:
  3. int removeDuplicates(vector<int>& nums) {
  4. if(nums.size()<=) return nums.size();
  5. int k=; //k记录最多重复两次的数组下标
  6. for(int i=;i<nums.size();i++){
  7. if(nums[i]!=nums[k] || (nums[i]==nums[k] && nums[k]!=nums[k-]) )
  8. nums[++k] = nums[i];
  9. }
  10. return k+;
  11. }
  12. };

leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;的更多相关文章

  1. LeetCode 283 Move Zeros

    Problem: Given an array nums, write a function to move all 0's to the end of it while maintaining th ...

  2. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  3. LN : leetcode 283 Move Zeroes

    lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...

  4. [LeetCode] 283. Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  5. LeetCode 283. Move Zeroes (移动零)

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  6. LeetCode 283 Move Zeroes 解题报告

    题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...

  7. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  8. Java [Leetcode 283]Move Zeroes

    题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  9. Leetcode 283 Move Zeroes python

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

随机推荐

  1. CentOS 安装mongodb3.0 二进制包

    1.下载mongodb因为64位系统CentOS,所以下载64位的安装包: wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0 ...

  2. 屌爆的xamarin,一人单挑google/apple/windows

    一个IDE就把3大手机平台全包了: android:自带模拟器xamarin player,速度堪比genymotion. ios:需要一台mac机辅助,一旦配好后可全程脱离,连ios模拟器都给镜像到 ...

  3. Apache logresolve命令

    一.简介 logresolve是一个解析Apache访问日志中IP地址的后处理程序. 二.语法 logresolve [ -s filename ] [ -c ] < access_log &g ...

  4. Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange

    DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...

  5. 7.python实现高效端口扫描器之nmap模块

    对于端口扫描,使用的最多的就是nmap这个工具,不想python已经强大到,提供了nmap这个扫描端口的模块. 本片文章主要介绍nmap模块的两个常用类: PortScanner()类,实现一个nma ...

  6. Java web 中的HttpServletRequest对象

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  7. document.domain 跨域问题

    document.domain用来得到当前网页的域名. 比如在地址栏里输入:javascript:alert(document.domain); //www.315ta.com我们也可以给docume ...

  8. LibreOJ 6279 数列分块入门 3(分块+排序)

    题解:自然是先分一波块,把同一个块中的所有数字压到一个vector中,将每一个vector进行排序.然后对于每一次区间加,不完整的块加好后暴力重构,完整的块直接修改标记.查询时不完整的块暴力找最接近x ...

  9. delphi窗体启动外部exe

    uses Winapi.Windows; WinExec(PAnsiChar(Application.ExeName), sw_normal);   // PAnsiChar : string to ...

  10. Canvas保存为图片

    public static void GenerateCanvas(string imgSaveName, int canvasWidth, int canvasHeight, string imgD ...