1. 程式人生 > >move zeroes --java 解法 同leetcode 283

move zeroes --java 解法 同leetcode 283

題目是我做一個公司的筆試題目出現的,後來發現leetcode 上也有。
參考:leetcode 283

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.

題目描述

給定一個數組,將陣列中的0元素全部移動到陣列的尾部,並保持原陣列的非0元素的在陣列中出現的順序。
例如 輸入nums = [0, 1, 0, 3, 12], 輸出[1, 3, 12, 0, 0]。

要求

1.只能對陣列進行原地操作,不能額外借用陣列或者複製陣列;
2.時間複雜度越小越好。

思路

在不允許使用輔助陣列的情況下,只能對陣列本身進行操作。根據題意,由於我們要找出陣列中的0元素和非0元素,顯然,我們需要兩個指標來分別標記他們,然後交換兩指標上的值來達到目的。
接下來的三種方法,無論哪一種思路,實際上都是用到了兩種指標來標記0元素和非0元素的目的。

方法一

順序掃描陣列,用一個指標iPointer從0開始,可以理解為標記陣列中非0元素的個數,遇到非0元素時,順序的直接將其移動到陣列的前面即iPointer所指位置,陣列掃描完畢時,iPointer指向陣列中的最後一個非0元素。然後將iPointer後面的部分的陣列元素全部補0即可。這種思路的程式碼也最容易理解。

class Solution {
    public void moveZeroes(int[] nums) {
        //方案一
        int iPointer = 0;
        int len = nums.length;
        for(int i=0; i<len;++i)
        {
            if
(nums[i] != 0) nums[iPointer++] = nums[i];//直接將遇到的非0元素 } if(iPointer != 0) { for(; iPointer<len; iPointer++) nums[iPointer] = 0; } } }
方法二

實際上算是方案一的改進版,既然每次找到了非0元素,幹嘛不多用一個指標,標記一下0元素,讓其兩者直接交換,而無須在陣列掃描完畢後再對陣列剩餘部分置0了。
流程:iPointer=0用來從頭掃描陣列,jPointer=0用來標記0元素。兩者都從頭開始移動指標。IPointer碰到非0元素時停下,將其跟jPointer指向的0元素交換,交換完,j向前移動一個位置,由於iPointer掃描陣列時是遇到非0元素才停下,交換完後,jPointer移動一個指標指向的肯定是0元素,實際上在交換元素的過程中,i,j指標之間的元素一定都是0

class Solution {
    public void moveZeroes(int[] nums) {

        if(nums == null || nums.length == 0)
        {
            return;
        }

        int len = nums.length;
        for(int i=0, j=0; i < len; ++i)
        {
            if(nums[i] != 0 )//i指標實際上依次指向非0元素時,才有交換
            {
                /*nums[j] = nums[i];//交換完,將j指標指向0元素
                nums[i] = 0;
                ++j;*///這樣做的問題就在於當陣列第一個元素或者都是非0元素時就會出現問題

                /*nums[i] = nums[i] + nums[j];//或者用異或a=a^b;b=a^b;a=a^b;
                nums[j] = nums[i] - nums[j];
                nums[i] = nums[i] - nums[j];*///這個只適合交換陣列中不同位置上的數字,如果i=j時就會改變nums[i]的值

                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                j++;//i,j指標交換完,將j指標指向0元素
            }
        }       
    }
}
方法三

實際上思路跟方法二完全一樣。

class Solution {
    public void moveZeroes(int[] nums) {

        /**
         *思路:維持兩個指標:一個指向非0元素(iPointer),一個指向0元素(jPointer)
         * 一開始i,j指標同時都從0開始往後移動;
         * 先移動i指標遇到非0元素停下,j指標遇到0元素停下;
         * 交換i,j指標上的值,繼續移動;
         * 我們會發現:i,j指標之間都是0元素
         */
        int len = nums.length;
        int iPointer = 0;
        int jPointer = 0;
       while (iPointer < len)
        {
            if(nums[iPointer] != 0)//在遇到0元素時,只有i指標移動
            {
                if (iPointer != jPointer) {
                    nums[jPointer] = nums[iPointer];
                    nums[iPointer] = 0;
                    jPointer++;
                } else {
                    jPointer++;//在遇到非0元素時,i,j指標同時移動
                }

            }
            iPointer++;//在遇到0元素時,只有i指標移動////在遇到非0元素時,i,j指標同時移動
        }        
    }
}

通過比較三種方法,實質上都是通過雙指標來完成原地操作的,之所以寫第三種方法,是我們能更直觀的看到雙指標的操作。以上三種方法都在leetcode上Accepted的。雙指標的思路解決辦法感覺還是很重要的。

相關推薦

move zeroes --java 解法 leetcode 283

題目是我做一個公司的筆試題目出現的,後來發現leetcode 上也有。 參考:leetcode 283 Given an array nums, write a function to move all 0’s to the end of it while

LeetCode演算法題-Move ZeroesJava實現-三種解法

這是悅樂書的第201次更新,第211篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第67題(順位題號是283)。給定一個數組nums,寫一個函式將所有0移動到它的末尾,同時保持非零元素的相對順序。例如: 輸入:[0,1,0,3,12] 輸出:[1,3,12,0,0]

LeetCode: 283 Move Zeroes(easy)

com opera tail edits stat cas end cal note 題目: Given an array nums, write a function to move all 0‘s to the end of it while maintaining t

Leetcode 283. Move Zeroes

emp ber you elements public pla tco nbsp for Given an array nums, write a function to move all 0‘s to the end of it while maintaining the

LeetCode 283 Move Zeroes

blog style left 移動 交換 size 容易 lee 也不會 是道容易題,可以想到很多解法,但有些解法容易出小錯誤: 1)交換法: 用一個指針p保存零元素起始位置。遍歷數組,當發現是非零元素,則和前面的零元素交換,同時p自增一。 這個方法有幾個地方容易想錯,首

283. Move Zeroes - LeetCode

圖片 class http rt+ lse lee img cloud tco Question 283. Move Zeroes Solution 題目大意:將0移到最後 思路: 1. 數組復制 2. 不用數組復制 Java實現: 數組復制 public void mo

[LeetCode&Python] Problem 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:

LeetCode 題解之 283. Move Zeroes

283. Move Zeroes 題目描述和難度 題目描述: 給定一個數組 nums,編寫一個函式將所有 0 移動到陣列的末尾,同時保持非零元素的相對順序。 示例: 輸入: [0,1,0,3

[Leetcode] 283. Move Zeroes

== write 一次 light The inpu relative 循環 ray Given an array nums, write a function to move all 0‘s to the end of it while maintaining the r

LeetCode283Move 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. Examp

leetcodeMove Zeroes283

題目: 給定一個數組 nums,編寫一個函式將所有 0 移動到陣列的末尾,同時保持非零元素的相對順序。 示例: 輸入: [0,1,0,3,12] 輸出: [1,3,12,0,0] 說明: 必須在原陣列上操作,不能拷貝額外的陣列。 儘量減少操作次數。 Python程

【python3】leetcode 283. Move Zeroes (easy)

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

python leetcode 283. Move Zeroes

class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, mod

LeetCode283. Move Zeroes

283.Move Zeroes Description: Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative

Leetcode 283. Move Zeroes 移動陣列中的零 (陣列,模擬)

題目描述 已知陣列nums,寫一個函式將nums中的0移動到陣列後面,同時保持非零元素的相對位置不變。比如已知nums=[0,1,0,3,12],呼叫你寫的函式後nums應該是[1,3,12,0,0

LeetCode 283 Move Zeroes(移動所有的零元素)

翻譯 給定一個數字陣列,寫一個方法將所有的“0”移動到陣列尾部,同時保持其餘非零元素的相對位置不變。 例如,給定nums = [0, 1, 0, 3, 12],在呼叫你的函式之後,nums應該變為[1, 3, 12, 0, 0]。 備註: 你必須就地完

LeetCode-283.Move Zeroes

arr != amp out void etc input ava for Given an array nums, write a function to move all 0‘s to the end of it while maintaining the relati

Leetcode-283. Move Zeroes

給定 輸出 != 排列 clas 遍歷 空間 pub problem 地址:https://leetcode.com/problems/move-zeroes/ 項目描述: Given an array nums, write a function to move al

283. Move Zeroes【easy】

leading blog ast 空間 only pub ssa ase ear 283. Move Zeroes【easy】 Given an array nums, write a function to move all 0‘s to the end of it w

283. Move Zeroes

num ets div idg 負責 != public nim write 283. Move Zeroes Given an array nums, write a function to move all 0‘s to the end of it while main