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

解法一:

时间复杂度O(n)

空间复杂度O(1)

class Solution {
public:
void moveZeroes(vector<int>& nums) {
int k = ; //nums中,[0,...k)的元素均为非0元素
//遍历到第i个元素后,保证[0,...i)中所有非0元素
//都按照顺序排列在[0,...k)中
for(int i=;i<nums.size();i++){
if(nums[i]){
nums[k++] = nums[i];
}
}
//将nums剩余的位置放置为0
for(int i=k;i<nums.size();i++)
nums[i] = ;
}
};

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

class Solution {
public:
void moveZeroes(vector<int>& nums) {
int k = ; //nums中,[0,...k)的元素均为非0元素
//遍历到第i个元素后,保证[0,...i)中所有非0元素
//都按照顺序排列在[0,...k)中
//同时,[k,...i]为0
for(int i=;i<nums.size();i++){
if(nums[i]){
if(i!=k)
swap(nums[k++] , nums[i]);
else
k++;
}
}
}
};

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

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

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int k = ;
int i = ;
while(i<nums.size()){
if(nums[i] == val)
nums.erase(nums.begin()+i); //删除下标为i的元素
else
i++;
}
return nums.size();
}
};

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

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

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

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]。

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return ;
int k = ;
for (int i = ; i < nums.size(); ++i)
{
if (nums[i] != nums[i - ])
{
nums[k++] = nums[i]; k++的作用是最终的k是数组的长度
}
}
return k;
}
};

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

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()<=) return nums.size();
int k=; //k记录最多重复两次的数组下标
for(int i=;i<nums.size();i++){
if(nums[i]!=nums[k] || (nums[i]==nums[k] && nums[k]!=nums[k-]) )
nums[++k] = nums[i];
}
return k+;
}
};

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. mask rcnn训练自己的数据集参考文章(推荐)

    最近用Mask_RCNN训练模型,下面几篇文章提供了不少帮助,汇总出来,方便以后查找,并向几位博主老师表示感谢 https://blog.csdn.net/qq_29462849/article/de ...

  2. while 循环和do while循环

    while循环是先检测条件符合不符合,符合才执行循环体内容,不符合就跳过while循环. 就和一个房间有两个门,一个前门,一个后门,while循环是当你进入前门的时候有人会检查你的身份,只有身份符合条 ...

  3. fiddler抓包时显示Tunnel to......443

    打开手机浏览器,输入http://192.168.0.65:8888/FiddlerRoot.cer

  4. Creating and Using Static Libraries for iPhone using Xcode 4.3

    Recently, after developing a collection of applications for iPhone that were intended to be used as ...

  5. 观察者(Observer)模式 * 委托事件

    观察者(Observer)模式:定义了一种一对多的依赖关系.让多个观察者对象同时监听某一个主题对象.   这个主题对象发生变化时会通知所有观察者对象,使他们字段更新自己 /* * 抽象主题(Subje ...

  6. 【SQL】- 基础知识梳理(一) - 数据库

    一.引言 知识分享这个事情在公司会议上被提出过几次,可一直因各种事情耽搁下来,“我不如地狱,谁入地狱”,怀着这样一种心态,写下了 数据库系列知识分享. 本文将一步步通过循序渐进的方式带你去了解数据库. ...

  7. SpringMVC+Hibernate 项目开发之一(Maven环境搭建)

    Maven环境搭建网上一大堆文章,直接引用leiOOlei同学的了:http://www.cnblogs.com/leiOOlei/p/3359561.html Maven版本:apache-mave ...

  8. windows环境下安装ZooKeeper

    $.说明 ZooKeeper: ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件. 它是一个为分布式 ...

  9. h2数据库 安装部署

    1.下载linux下的包,即全平台,网址:http://www.h2database.com/html/download.html 选择Platform-Independent Zip 2.把这个包上 ...

  10. Flutter的使用教学笔记

    QQ交流群 Flutter 程序开发群:766307130 教程 官方实战 使用Flutter 构建精美的页面 云在千峰 博主一直是从事 Android 开发的,所以主要从 Android 技术角度来 ...