亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

[LintCode] Move Zeroes

Mr_houzi / 785人閱讀

Problem

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.

Notice

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

Example

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

Solution A too-clever method
class Solution {
    public void moveZeroes(int[] nums) {
        int i = 0, j = 0;
        while (i < nums.length && j < nums.length) {
            if (nums[i] != 0) {
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                j++;
            }
            i++;
        }
    }
}
A just-so-so method
public class Solution {
    public void moveZeroes(int[] nums) {
        int i = 0, j = i+1;
        while (j < nums.length) {
            if (nums[i] != 0) {
                i++;
                j++;
            }
            else {
                if (nums[j] == 0) j++;
                else {
                    swap(nums, i, j);
                    i++;
                }
            }
        }
    }
    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
    

Update 2018-8

class Solution {
    public void moveZeroes(int[] nums) {
        if (nums == null || nums.length < 2) return;
        int i = 0, j = 1;
        while (j < nums.length) {
            if (nums[i] != 0) {
                i++;
                j++;
            } else {
                if (nums[j] == 0) {
                    j++;
                } else {
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                    i++;
                }
            }
        }
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/65999.html

相關(guān)文章

  • [LintCode/LeetCode/CC] Set Matrix Zeroes

    摘要:把矩陣所有零點(diǎn)的行和列都置零,要求不要額外的空間。對(duì)于首行和首列的零點(diǎn),進(jìn)行額外的標(biāo)記即可。這道題我自己做了四遍,下面幾個(gè)問(wèn)題需要格外注意標(biāo)記首行和首列時(shí),從到遍歷時(shí),若有零點(diǎn),則首列標(biāo)記為從到遍歷,若有零點(diǎn),則首行標(biāo)記為。 Problem Given a m x n matrix, if an element is 0, set its entire row and column t...

    zhangwang 評(píng)論0 收藏0
  • Leetcode PHP題解--D68 283. Move Zeroes

    摘要:題目鏈接題目分析給定一個(gè)整數(shù)數(shù)組,將值為的元素移動(dòng)到數(shù)組末尾,而不改動(dòng)其他元素出現(xiàn)的順序。再在去后的元素末尾填充到計(jì)算出的數(shù)組長(zhǎng)度。最終代碼若覺(jué)得本文章對(duì)你有用,歡迎用愛(ài)發(fā)電資助。 D68 283. Move Zeroes 題目鏈接 283. Move Zeroes 題目分析 給定一個(gè)整數(shù)數(shù)組,將值為0的元素移動(dòng)到數(shù)組末尾,而不改動(dòng)其他元素出現(xiàn)的順序。 思路 計(jì)算總共有多少個(gè)元素。 再...

    xiongzenghui 評(píng)論0 收藏0
  • [Leetcode] Move Zeroes 移動(dòng)零

    摘要:雙指針壓縮法復(fù)雜度時(shí)間空間思路實(shí)際上就是將所有的非數(shù)向前盡可能的壓縮,最后把沒(méi)壓縮的那部分全置就行了。比如,先壓縮成,剩余的為全置為。過(guò)程中需要一個(gè)指針記錄壓縮到的位置。 Move Zeroes Given an array nums, write a function to move all 0s to the end of it while maintaining the rel...

    baiy 評(píng)論0 收藏0
  • LeetCode 283:移動(dòng)零 Move Zeroes

    摘要:給定一個(gè)數(shù)組,編寫(xiě)一個(gè)函數(shù)將所有移動(dòng)到數(shù)組的末尾,同時(shí)保持非零元素的相對(duì)順序。盡量減少操作次數(shù)。換個(gè)思路,把非數(shù)字前移,不去管數(shù)字。這樣遍歷完之后,數(shù)組索引從到之間的數(shù)值即為所求得保持非零元素的相對(duì)順序,而之后的數(shù)值只需要全部賦值即可。 給定一個(gè)數(shù)組 nums,編寫(xiě)一個(gè)函數(shù)將所有 0 移動(dòng)到數(shù)組的末尾,同時(shí)保持非零元素的相對(duì)順序。 Given an array nums, write ...

    tianyu 評(píng)論0 收藏0
  • LeetCode 283:移動(dòng)零 Move Zeroes

    摘要:給定一個(gè)數(shù)組,編寫(xiě)一個(gè)函數(shù)將所有移動(dòng)到數(shù)組的末尾,同時(shí)保持非零元素的相對(duì)順序。盡量減少操作次數(shù)。換個(gè)思路,把非數(shù)字前移,不去管數(shù)字。這樣遍歷完之后,數(shù)組索引從到之間的數(shù)值即為所求得保持非零元素的相對(duì)順序,而之后的數(shù)值只需要全部賦值即可。 給定一個(gè)數(shù)組 nums,編寫(xiě)一個(gè)函數(shù)將所有 0 移動(dòng)到數(shù)組的末尾,同時(shí)保持非零元素的相對(duì)順序。 Given an array nums, write ...

    seanlook 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<