[刷题] LeetCode 283 Move Zeros

要求:

将所有的0,移动到vector的后面
比如; [1,3,0,12,5] -> [1,3,12,5,0]

第一版程序,时间、空间复杂度都是O(n)

 1 #include<iostream>
 2 #include<vector>
 3
 4 using namespace std;
 5
 6 class Solution{
 7     public:
 8         void moveZeros(vector<int>& nums){
 9             vector<int> nonZeroElements;
10             for( int i = 0 ; i < nums.size() ; i ++ )
11                 if( nums[i] )
12                     nonZeroElements.push_back(nums[i]);
13             for( int i = 0 ; i < nonZeroElements.size() ; i ++ )
14                 nums[i] = nonZeroElements[i];
15             for( int i = nonZeroElements.size() ; i < nums.size() ; i ++ )
16                 nums[i] = 0;
17         }
18 };
19
20 int main(){
21     int arr[] = {0, 1, 0, 3, 12};
22     vector<int> vec(arr, arr + sizeof(arr)/sizeof(int) );
23     Solution().moveZeros( vec );
24     for( int i = 0 ; i < vec.size() ; i ++ )
25         cout<<vec[i]<<" ";
26     cout<<endl;
27 }

优化后,空间复杂度O(1)

 1 class Solution{
 2     public:
 3         // 索引
 4         void moveZeros(vector<int>& nums){
 5             int k = 0;
 6             // 遍历到第i个元素后,保证[0...i)中所有非0元素
 7             // 均按顺序排列在[0...k)中
 8             for( int i = 0 ; i < nums.size() ; i ++ )
 9                 if( nums[i] )
10                     nums[k++] = nums[i];
11             // 将[k...n]赋值为0
12             for( int i = k ; i < nums.size() ; i ++ )
13                 nums[i] = 0;
14         }
15 }

利用交换,提高效率

1 void moveZeros1(vector<int>& nums){
2     int k = 0;
3         for( int i = 0 ; i < nums.size() ; i ++ )
4             if( nums[i] )
5                 swap( nums[k++] , nums[i] );
6 } 

增加判断,提高非零元素较多时的效率

 1 void moveZeros1(vector<int>& nums){
 2     int k = 0;
 3         for( int i = 0 ; i < nums.size() ; i ++ )
 4             if( nums[i] )
 5                 // 指向同一个元素时不交换
 6                 if( i != k )
 7                     swap( nums[k++] , nums[i] );
 8                     else // i == k
 9                         k++;
10 }

>> 1 3 2 12 0 0

原文地址:https://www.cnblogs.com/cxc1357/p/12327378.html

时间: 2024-10-21 09:45:46

[刷题] LeetCode 283 Move Zeros的相关文章

LeetCode(283): Move Zeros

Move Zeros: 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].

【LeetCode】283. Move Zeros

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

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] 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

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

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(移动全部的零元素)

翻译 给定一个数字数组.写一个方法将全部的"0"移动到数组尾部.同一时候保持其余非零元素的相对位置不变. 比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之后,nums应该变为[1, 3, 12, 0, 0]. 备注: 你必须就地完毕,不得复制该数组. 最小化总共的操作数. Given an array nums, write a function to move all 0's to the end of it while maintaining the re

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 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]. python