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. R 如何 隐藏坐标轴

    x = c(7,5,8)dim(x)<-3names(x)<-c("apple","banana", "cherry")plot ...

  2. Django框架 之 Ajax

    Django框架 之 Ajax 浏览目录 AJAX准备知识 AJAX与XML的比较 AJAX简介 jQuery实现的ajax AJAX参数 AJAX请求如何设置csrf_token 序列化 一.AJA ...

  3. 一步一步带你构建第一个 Laravel 项目

    参考链接:https://laravel-news.com/your-first-laravel-application 简介 按照以下的步骤,你会创建一个简易的链接分享网站. 安装 Laravel ...

  4. javascript总结17:javascript 函数简介

    1 释义:函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 2 格式:通过 function  关键字. function test(){ alert("您好"); } ...

  5. 【Head First Java 读书笔记】(三)primitive主数据类型和引用

    认识变量 变量有两种:primitive数数据类型和引用. 声明变量 Java注重变量.它不会让你将浮点数类型变量放进整数类型的变量中,除非你先跟编译器确认过数字可以损失掉精确度. 为了要让类型安全能 ...

  6. C# 取得内网IP、外网IP、客户端IP方法

    前言 在 Windows Form Application 里对于取得 IP Address 有内网.外网两种 IP Address ,如果只需要取得内网 IP Address ,可以透过使用 IPH ...

  7. [Lua快速了解一下]Lua的控制语句

    -Lua中没有++或者--的骚操作 -while loop sum = num = do sum = sum + num num = num + end print("sum =" ...

  8. c# 读取txt文档和写入文档的方法

    StreamReader sr = new StreamReader(path); //path是要读取的文件的完整路径 String str_read = sr.ReadToEnd(); //从开始 ...

  9. C# 属性与字段

    属性和字段的区别: 属性是逻辑字段,是字段的扩展,并不占用实际的内存:而字段占用内存空间. 属性可以被其他类访问:而非public的字段不能被直接访问. 属性可以对接受的数据在范围上做限定:而字段不能 ...

  10. kali linux之服务扫描

    识别开放端口上运行的应用.识别目标操作系统,提高攻击效率 banner捕获(软件开发商,软件名称,服务类型,版本号-----直接发现已知的漏洞和弱点) 服务识别 操作系统识别 snmp分析(简单网络管 ...