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 = arr[i];
        if (tmp == 0) {
            nums[end--] = 0;
        } else {
            nums[start++] = tmp;
        }
    }
}

不用数组复制

public void moveZeroes(int[] nums) {
    int start = 0;
    int zeroCount = 0;
    for (int i=0; i<nums.length; i++) {
        int tmp = nums[i];
        if (tmp == 0) {
            zeroCount++;
        } else {
            nums[start++] = tmp;
        }
    }
    for (int i = 0; i < zeroCount; i++) {
        nums[nums.length - 1 - i] = 0;
    }
}

原文地址:https://www.cnblogs.com/okokabcd/p/9427924.html

时间: 2024-10-12 22:49:17

283. Move Zeroes - LeetCode的相关文章

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

刷题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){

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 解题报告

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

翻译 给定一个数字数组.写一个方法将全部的"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