Problem:

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

1. You must do this in-place without making a copy of the array.

2. Minimize the total number of operations.

Summary:

在不复制数组的情况下,将整型数组中的所有0移至数组末尾,其他数相对位置不变。

尽可能减少操作数。

Analysis:

1. 常规方法:用两个指针,其中一个指针向后扫,每找到一个非零元素则与前面指针内容互换。

 class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size(), k = ; for (int i = ; i < len; i++) {
if (nums[i] != ) {
swap(nums[i], nums[k++]);
}
} return;
}
};

但由于交换操作过多,导致效率不高。

2. 同样两个指针,其中一个指针向后扫,每找到一个非零元素则按顺序从数组头往后放,另一个指针记录当前放置位置。

  第一个指针扫完时,将第二个指针到数组末尾全部填0即可。

 class Solution {
public:
void moveZeroes(vector<int>& nums) {
int len = nums.size(), k = ; for (int i = ; i < len; i++) {
if (nums[i] != ) {
nums[k++] = nums[i];
}
} while (k < len) {
nums[k++] = ;
} return;
}
};

LeetCode 283 Move Zeros的更多相关文章

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

    ,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...

  2. 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 ...

  3. 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 ...

  4. leetcode 283. Move Zeroes -easy

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

  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 re ...

  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 relativ ...

  7. 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 ...

  8. 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 ...

  9. 11. 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 ...

随机推荐

  1. 动态导入(import)和静态导入(import)的区别

    import static静态导入是JDK1.5中的新特性.一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com.....Cl ...

  2. ios 防止按钮快速点击造成多次响应的避免方法。

    - (void)starButtonClicked:(id)sender { //先将未到时间执行前的任务取消. [[self class] cancelPreviousPerformRequests ...

  3. HDu1003(maxn sum)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBcAAAMDCAYAAAD5XP0yAAAgAElEQVR4nOy97a8c133n2X+H3xjIC4

  4. iOS开发——高级篇——UIDynamic 物理引擎

    一.UIDynamic 1.简介什么是UIDynamicUIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象重力.弹性碰撞 ...

  5. [Unity3d][NGUI]打包NGUI预制件成Assetbundle 两种思路.

    http://www.58player.com/blog-2537-85030.html 接上文,项目中因为需要UI热更新,所以我使用了AssetBundle这个解决方案.               ...

  6. 给dos命令“.bat”文件换图标

    最近客户有个需求:给企业建立一个FTP服务器,并且给不同的部门分配不同的目录和管理权限. 这个好实现!直接安装serv-u,进行一番设置,搞定! 不过客户嫌登陆FTP操作麻烦,输入ip,输入账号什么的 ...

  7. Extjs 组件共用(单例)问题

    说明: 将store初始化在类定义时便创建, store实例将成为该类的单例 代码: 测试: 说明: 将store初始化放入initComponent函数中.  每次都将创建一个新的实例. 代码: 测 ...

  8. 修改apache上传文件大小限制

    PHP上传文件大小限制解决方法: 第一: 在php.ini里面查看如下行: upload_max_filesize = 8M    post_max_size = 10M    memory_limi ...

  9. Linux中增加软路由的两种方法/删除的方法

    第一种:   route add -net 172.16.6.0 netmask 255.255.255.0 gw 172.16.2.254 dev eth0   route del gw 172.1 ...

  10. mysql性能优化-简易版

    mysql性能优化 sql语句优化 如何发现有问题的sql? 开启mysql慢查询 show variables like 'slow_query_log' set global slow_query ...