[LeetCode]--283. Move Zeroes
来源:互联网 发布:dll编程书籍 编辑:程序博客网 时间:2024/04/23 20:22
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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
我想着先不是零的都移到前面,然后后面的补零。
public void moveZeroes(int[] nums) { int j = 0; for (int i = 0; i < nums.length; i++) if (nums[i] != 0) nums[j++] = nums[i]; while (j < nums.length) nums[j++] = 0; }
另外一种,边移边补零。
public void moveZeroes(int[] nums) { int i = 0; int j = 0; while (j < nums.length) { if (nums[j] != 0) { if (j != i) { nums[i++] = nums[j]; nums[j] = 0; } else ++i; } ++j; } }
0 0
- 【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
- #leetcode#283. Move Zeroes
- LeetCode *** 283. Move Zeroes
- LeetCode 283. Move Zeroes
- LeetCode-283.Move Zeroes
- Leetcode 283. Move Zeroes
- leetcode 283. Move Zeroes
- HDU ACM 11 2052 Picture
- 分析与设计(A&D)简介(1)
- 软件评测师——计算机网络1
- UVa 12096 The SetStack Computer
- PHP操作Excel表格浅析
- [LeetCode]--283. Move Zeroes
- UITableView: 刷新视图、插入行、更新行的两种方式
- 最短路
- 【codevs 4906】删数问题
- 用Python 实现的九种排序算法
- Python 字典setdefault()
- HTML进度条动画
- CSS 深入理解absolute
- Java web 写一个注册登录界面,Sql 10