leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;


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;的更多相关文章
- 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 ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- 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 ...
- 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 ...
随机推荐
- ObjectMapper对象的使用 Object2JSON
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...
- c语言学习笔记 for循环的结构
其实感觉for循环没有while循环那么直白好理解. for(i=0;i<n;i++) { dosth(); } i=0是i的初始值. i<n是循环进行的条件. i++是每次循环要做的事情 ...
- ensemble 的2篇入门 文章
python 篇: http://machinelearningmastery.com/ensemble-machine-learning-algorithms-python-scikit-learn ...
- SpringMVC——拦截器
Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口 preHandle():这个方法在业务处理 ...
- 手打的table
突然觉得,如果我不上传源码和写篇博客,对不起花在这个破网页2个小时的时间,完全手打,浏览器调效果. 源码如下: a.html: <!DOCTYPE html PUBLIC "-//W3 ...
- LightOJ 1284 Lights inside 3D Grid (数学期望)
题意:在一个三维的空间,每个点都有一盏灯,开始全是关的.现在每次随机选两个点,把两个点之间的全部点,开关都按一遍,问k次过后开着的灯的期望数量: 析:很容易知道,如果一盏灯被按了奇数次,那么它肯定是开 ...
- java IO 对象流 反序列化和序列化
例: 重点:需要序列化的对象必须实现Serializable接口 //需要序列化的对象 public class User implements Serializable { private Stri ...
- MongoDB整理笔记のMapReduce
MongDB的MapReduce相当于MySQL中的“group by”,所以在MongoDB上使用Map/Reduce进行并行“统计”很容易. 使用MapReduce要实现两个函数Map函数和Red ...
- Eclipse平台下配置Go语言开发环境(Win7)
<Go语言编程>中写到:“从功能和易用性等方面考虑, Eclipse+GoEclipse.LiteIDE这两个环境在所有IDE里面是表现最好的”,所以笔者打算采用Eclipse+GoEcl ...
- asp.net mvc 中通过url字符串获取controller和action
在项目中遇到需要通过url字符串获取controller和action的情况,百度了 一下找到了一个可以用的方法 ,在这里分享和记录一下 这个方法是在博客园的博问里看到的 原文地址是http://q. ...