刷题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){
				if(nums[j]==0){
					j++;
				}else{
					nums[i] = nums[j];
					i++;
					j++;
				}
			}
			for(int t = i;t<len;t++){
				nums[t] = 0;
			}
		}
};

性能如下:

Runtime: 12 ms, faster than 97.09% of C++ online submissions for Move Zeroes.
Memory Usage: 9.5 MB, less than 87.50% of C++ online submissions for Move Zeroes.

三、优化措施

从左到右遍历数组,统计0的个数,num[i]为整数,则将其移动到nums[i-count]

class Solution{
	public:
		void moveZeroes(vector<int>& nums){
			int count = 0;
			for(int i=0;i<nums.size();i++){
				if(nums[i]==0){
					count++;
				}else if(count>0){
					nums[i-count] = nums[i];
					nums[i] = 0;
				}
			}
		}
};

性能

Runtime: 12 ms, faster than 97.09% of C++ online submissions for Move Zeroes.
Memory Usage: 9.5 MB, less than 84.72% of C++ online submissions for Move Zeroes.

原文地址:https://www.cnblogs.com/siweihz/p/12295137.html

时间: 2024-10-11 01:30:30

刷题283. Move Zeroes的相关文章

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(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,

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

[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

[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

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: