全球彩票平台_全球彩票注册平台|官网下载地址

热门关键词: 全球彩票平台,全球彩票注册平台,全球彩官网下载地址

全球彩票注册平台Move Zeroes,movezeroes

Move Zeroes,movezeroes

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 void moveZeroes(int* nums, int numsSize) {
 2     int i;
 3     int count = 0;
 4     int k = 0;
 5     for(i = 0; i < numsSize; i  )
 6     {
 7         if(nums[i] == 0)
 8             count  ;
 9     }
10     for(i = 0; i < numsSize; i  )
11     {
12         if(nums[i] != 0)
13             nums[k  ] = nums[i];
14     }
15     while(count)
16     {
17         nums[k  ] = 0;
18         count--;
19     }
20 }

 

Zeroes,movezeroes 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...

LeetCode----Move Zeroes

全球彩票注册平台,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.

    分析:

    将数组中0移到数组前边去,注意要保险数组的相持成分地点不改变。

     

    代码:

     

    class Solution(object):
        def moveZeroes(self, nums):
    
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
    
            nums_l = len(nums)
            if nums_l <= 1:
                return
            z = i = 0
            while i < nums_l:
                t = nums[i]
                if t:
                    nums[i] = 0
                    nums[z] = t
                    z  = 1
                i  = 1
    

     

     

Zeroes 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 examp...

[LeetCode] 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 must do this in-place without making a copy of the array. Minimize the total number of operations.

lc 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, 0, 0].

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.

解题思路

用五个指针i,j分别指向0和非零,当j指向非零时,调换八个指针所针对的要素,并且i向前走一步;j则每趟循环无论是或不是交流都上前走一步。

analysation

刚初步用了很蠢的方法,后来用STL很自在就消除了!

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

兑当代码

C :

// Runtime: 20 ms
class Solution {
public:
    void moveZeroes(vector& nums) {
        int i = 0;
        int j = 0;
        while (j < nums.size())
        {
            if (nums[j] != 0)
            {
                swap(nums[i], nums[j]);
                i  ;
            }
            j  ;
        }
    }
};

Java:

// Runtime: 1 ms
public class Solution {
    public void moveZeroes(int[] nums) {
        int i = 0;
        int j = 0;
        while (j < nums.length) {
            if (nums[j] != 0) {
                if (j != i) {
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
                i  ;
            }
            j  ;
        }
    }
}

 

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

solution1:蠢

void moveZeroes(vector<int> & nums) {
    if (nums.size() <= 1) {
        return;
    } else {
        int sum = 0;
        for (int i = 0; i < nums.size(); i  ) {
            if (nums[i] != 0) {
                nums[sum] = nums[i];
                sum  ;
            }
        }
        for (int i = sum; i < nums.size(); i  )
        nums[i] = 0;
    }
}

给定一个数字数组,写一个方法将所有的“``0``”移动到数组尾部,同时保持其余非零元素的相对位置不变。

本文由全球彩票平台发布于全球彩票注册平台编程,转载请注明出处:全球彩票注册平台Move Zeroes,movezeroes

TAG标签: 全球彩票平台
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。