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. Mac Pro 安装 最新版的 SVN 1.9.4

    系统自带的 SVN 版本为 1.7.2 $ svn --version svn, version 1.7.22 (r1694152) 有点老,安装下最新版本 brew install svn 由于老版 ...

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

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

  3. mongodb的sql例子(简单版)

    插入数据 db.person.insert({"name":"zfx","age":21}) 查找所有数据 db.person.find() ...

  4. 第五章第二例题关于Vector(LRJ)

    vector(动态数组)(粘) 一.概述 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector是一个容器,它能够存放各种类型的对象,简 ...

  5. unity3d 安卓IOS推送

    https://github.com/jpush/jpush-unity3d-plugin

  6. windows7 + cocos2d-x 3.2 +vs2012 速度真的很慢

    每次编译要大半天,简直伤不起,这样效率如何上的去???有谁有办法?

  7. linux kernel 平台总线实例分析

    linux 平台总线的实现有三大块  , platform bus , platform device , platform drvice 平台类型结构体: /** * struct bus_type ...

  8. PHP输出控制(Output Control)函数

    ob_start 此函数将打开输出缓冲.当输出缓冲激活后,脚本将不会输出内容(除http标头外),相反需要输出的内容被存储在内部缓冲区中. 内部缓冲区的内容可以用 ob_get_contents() ...

  9. 4.1---二叉树是否平衡(CC150)

    //也就是把高度在递归过程中给一并算了.public class Balance { public static boolean checkBalance(TreeNode root, int[] d ...

  10. django foreign key 自动加_id问题

    解决:http://stackoverflow.com/questions/8223519/preventing-django-from-appending-id-to-a-foreign-key-f ...