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 ...
随机推荐
- Linux问题:开启网关
1 开启网关 1.1 问题描述 虚拟机每次重启后,都需要利用 ifup eth0 来手动开启网关,否则获取到的IP地址为回环127.0.0.1 1.2 解决办法 修改ifcfg-eth0中 ONBOO ...
- SpringBoot26 利用 Ribbon + RestTemplate 调用远程服务资源
1 RestTemplate扫盲 借助 RestTemplate,Spring应用能够方便地使用REST资源 2 准备 创建三个springCloud项目 >Eureaka : 服务注册中心 ...
- ROS naviagtion analysis: move_base
博客转载自:https://blog.csdn.net/u013158492/article/details/50483123 这是navigation的第一篇文章,主要通过分析ROS代码级实现,了解 ...
- groupie
def add_group(group): c = group.c.astype('float') group['d'] = c/c.sum() return group df = pd ...
- 在slam_gmapping中使用Log数据创建地图
本文介绍使用机器人记录的tf变换和激光扫描数据来建立2D地图.并在ROS的图形化模拟环境rviz中通过重新回放记录的数据作为机器人真实传感器采集的输入,来观测地图动态创建过程. 1.ROS gmapp ...
- ByteUnit
JDK里面有TimeUnit,看spark源码有个ByteUnit.这个类还是挺不错的. public enum ByteUnit { BYTE (1), KiB (1024L), MiB ((lon ...
- JUnit 两日游
从一个简单的Junit demo到一个用户名与密码的验证 学习Junit,首先肯定是要环境的搭建与配置. 第一步,安装JDK,配置环境变量 第二步,安装eclipse.OS X环境下,eclipse并 ...
- Hyper-V和vmware在虚拟机中安装xen总结
1. Hyper-V 在hyper-v中安装了ubuntu13.04,运行很好,使用起来的效果感觉比vmware要舒服.安装变异xen的内核也没有问题,可以正常的安装,update-grub之后也可以 ...
- (转)C# HTML解析示例---星星引发的血案
原文地址:http://www.cnblogs.com/wurang/archive/2013/06/14/3119023.html [前言] 从CSDN转投cnBlog也有一段时间了,发现cnBlo ...
- [LintCode笔记了解一下]39.恢复旋转排序数组
思路: 1.需要O(n)的事件复杂度,所以多次循环不考虑 2.四步翻转法 -第一步,找到数组里最小的那个数字,因为是旋转排序数组,所以只要找到某个位置arr[i]>arr[i+1]的话,就找到了 ...