[Leetcode] 283. Move Zeroes 解题报告
来源:互联网 发布:怎么在淘宝上注册店 编辑:程序博客网 时间:2024/06/02 02:35
题目:
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]
.
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
思路:
好简洁的代码呀!注意理解k的含义。
代码:
class Solution {public: void moveZeroes(vector<int>& nums) { for(int i = 0, k = 0; i < nums.size(); ++i) { if(nums[i] != 0) { swap(nums[i], nums[k++]); } } }};
阅读全文
0 0
- [leetcode] 283. Move Zeroes 解题报告
- 【LeetCode】283.Move Zeroes解题报告
- [Leetcode] 283. Move Zeroes 解题报告
- 【LeetCode】Move Zeroes 解题报告
- 景岁的Leetcode解题报告:283. Move Zeroes(Python)
- LeetCode 283. Move Zeros 解题报告
- 【LeetCode】283. Move Zeroes
- Leetcode 283. Move Zeroes
- LeetCode #283. Move Zeroes
- 283. Move Zeroes LeetCode
- LeetCode 283. Move Zeroes
- LeetCode 283. Move Zeroes
- LeetCode 283. Move Zeroes
- 【LeetCode】283. Move Zeroes
- leetcode 283. Move Zeroes
- leetcode 283. Move Zeroes
- LeetCode 283. Move Zeroes
- leetcode 283. Move Zeroes
- Python安装rar解压包(for Arcgis篇)
- ArcGIS Engine进行编辑时,提示错误:Objects in this class cannot be updated outside an edit session [line_2]
- eclipse中合并svn代码 -- 将分支branch上的代码合并到主干trunk上
- JS日期格式化,拿到时间段日期
- xml基础知识(1)
- [Leetcode] 283. Move Zeroes 解题报告
- c\c++ 输入输出
- Hadoop的分布式架构改进与应用
- xmpp的XmppConnection类
- C++ 标准函数库 —— 文件的输入输出 (Input/Output with files)
- 垂直居中各种条件小的适用情况
- hdu 1397 Goldbach's Conjecture
- 通过ICSharpCode.SharpZipLib类库对zip文件进行压缩与解压
- C++ 引用变量