Leetcode 283 Move Zeroes
2016-12-24 22:16
309 查看
Given an array
For example, given
public class Solution {
public void moveZeroes(int[] nums) {
int j = 0;
for (int i = 0; i < nums.length; i++)
{
if (nums[i] != 0)
{
nums[j++] = nums[i];
}
}
for (;j < nums.length; j++)
{
nums[j] = 0;
}
}
}
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,
numsshould be
[1, 3, 12, 0, 0]
public class Solution {
public void moveZeroes(int[] nums) {
int j = 0;
for (int i = 0; i < nums.length; i++)
{
if (nums[i] != 0)
{
nums[j++] = nums[i];
}
}
for (;j < nums.length; j++)
{
nums[j] = 0;
}
}
}
相关文章推荐
- leetcode283~Move Zeroes
- leetcode 283:Move Zeroes
- LeetCode-283 Move Zeroes
- leetcode283---Move Zeroes(把0移到数组后面)
- Leetcode 283 Move Zeroes 字符串
- leetcode 283 Move Zeroes
- LeetCode(283)Move Zeroes
- [LeetCode-283]Move Zeroes(java)
- LeetCode[283] Move Zeroes
- [leetcode-283]Move Zeroes(java)
- LeetCode(283)Move Zeroes
- [C语言][LeetCode][283]Move Zeroes
- LeetCode 283 Move Zeroes(移动所有的零元素)
- [LeetCode283]Move Zeroes
- [LeetCode]: 283: Move Zeroes
- [LeetCode 283] Move Zeroes
- LeetCode 283:Move Zeroes
- [LeetCode-283] Move Zeroes
- 【LeetCode-283】Move Zeroes(C++)
- leetcode-283-Move Zeroes