283. Move Zeroes

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:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

C++(16ms):

 1 class Solution {
 2 public:
 3     void moveZeroes(vector<int>& nums) {
 4         int j = 0 ;
 5         for (int i = 0; i < nums.size();i++ ){
 6             if (nums[i] != 0 ){
 7                 nums[j] = nums[i];
 8                 j++;
 9             }
10         }
11         for (; j <nums.size() ; j++){
12             nums[j] = 0 ;
13         }
14     }
15 };

C++(19ms):

 1 class Solution {
 2 public:
 3     void moveZeroes(vector<int>& nums) {
 4         int j = 0 ;
 5         for (int i = 0; i < nums.size();i++ ){
 6             if (nums[i] != 0 ){
 7                 swap(nums[j],nums[i]);
 8                 j++;
 9             }
10         }
11     }
12 };
时间: 2024-12-10 20:37:53

283. Move Zeroes的相关文章

283. Move Zeroes(C++)

283. Move Zeroes 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,

leetCode 283. Move Zeroes 数组

283. Move Zeroes 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,

Leetcode刷题记录[python]——283 Move Zeroes

一.前言 题是上周五做的,开始思路有点问题,考虑不全,导致submit了3次才AC. 二.题283 Move Zeroes 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 call

283. Move Zeroes - LeetCode

Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZeroes(int[] nums) { int[] arr = Arrays.copyOf(nums, nums.length); int start = 0; int end = nums.length - 1; for (int i=0; i<arr.length; i++) { int tmp

刷题283. Move Zeroes

一.题目说明 题目283. Move Zeroes,给定一组数,将该组数中的0移动末尾,其他数据相对位置不变. 二.我的解答 class Solution{ public: void moveZeroes(vector<int>& nums){ if(nums.size()<2){ return; } int len = nums.size(); int i=0,j=0; //i指向更新的位置,j指向非0元素 while(i<len &&j<len){

[Algorithm] 283. Move Zeroes

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. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] Note: You must do this in-place without making a copy of the array

LeetCode: 283 Move Zeroes(easy)

题目: 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:

[LeetCode]: 283: Move Zeroes

题目: 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:

[leetcode] 283. Move Zeroes 解题报告

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