상세 컨텐츠

본문 제목

Array 101 Move Zeroes in Go

Go/Leet Code

by Gopythor 2022. 3. 10. 20:20

본문

728x90
반응형

Accepted Solutions Runtime Distribution
Accepted Solutions Memory Distribution

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

Example 1:

Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]

Example 2:

Input: nums = [0]
Output: [0]

Constraints:

  • 1 <= nums.length <= 104
  • -231 <= nums[i] <= 231 - 1

Follow up: Could you minimize the total number of operations done?

My code

func moveZeroes(nums []int) {
    if len(nums) <= 1 {
        return
    }
    wp := 0
    for j, v := range nums {
        if j == wp && v != 0 {
            wp++
        } else if v != 0 {
            nums[wp], nums[j] = v, 0
            wp++
        }
    }
}
  • I implemented edge cage escaper
  • wp is for writing pointer.
  • I used for loop with index, value.
  • First If condition will check j and wp are same with checking value.
  • If same array pointer, increase wp only not to make any process.
  • if not with different pointer, v will be allocated to wp array and j array will have 0.
  • And increase wp.

sample 11 ms submission

func moveZeroes(nums []int) {
    lastzero := 0
    for i := 0; i < len(nums); i++ {
        if nums[i] != 0 {
            nums[lastzero] = nums[i]
            lastzero++
        }
    }
    for i := lastzero; i < len(nums); i++ {
        nums[i] = 0
    }
}
  • This code is using lastzero as writer pointer.
  • nums array will be checked until end array.
  • After roof ended for scan, lastzero will be used as filling 0.
  • simple code.

sample 12 ms submission

func moveZeroes(nums []int)  {
    index:=0
    for i:=0;i<len(nums);i++ {
        if nums[i]!=0 {
            nums[index], nums[i] = nums[i], nums[index]
            index++
        }
    }
  • This code uses switching index and value.
  • So when if condition finds not 0 value, it changes both.
  • So 0 will be in array which has value, and value will move to index array.
  • After switcing, index will increase.

sample 13 ms submission

func moveZeroes(nums []int)  {
    numZeros := 0
    i := 0
    for _, num := range(nums) {
        if num == 0 {
            numZeros++
        } else {
            nums[i] = num
            i++
        }
    }

    numNonZeros := len(nums) - numZeros
    for j := numNonZeros; j < len(nums); j++ {
        nums[j] = 0
    }
}

func swap(nums []int, i int, j int) {
    tmp := nums[i]
    nums[i] = nums[j]
    nums[j] = tmp
}
  • This code counts Zero and use writer pointer.
  • I don't think It is a good code.
  • Without numZeros, I can implement better code.

Modification

func moveZeroes(nums []int) {
    i := 0
    for _, num := range nums {
        if num != 0 {
            nums[i] = num
            i++
        }
    }

    for j := i; j < len(nums); j++ {
        nums[j] = 0
    }
}

sample 14 ms submission

func moveZeroes(nums []int)  {
    n:=0
    for i,elem := range nums {
        if(elem != 0){
            if(nums[n] == 0){ 
                nums[i],nums[n] = 0, elem
            }
            n++
        }
    }
}
  • This code looks a little complex.
  • First if condition checks elem is 0 or not. So elem is 0, if conditions don't do anything.
  • When elem is not 0, if condition checks nums[n] is 0 or not, if 0 then elem array will have 0, and writer pointer will have elem value.
  • I don't know why second if condition exists.
  • without if condition, it works.Modification
  • func moveZeroes(nums []int) { n := 0 for i, elem := range nums { if elem != 0 { nums[i], nums[n] = 0, elem n++ } } }

sample 6500 KB submission

func moveZeroes(nums []int)  {
    length := len(nums)
    //right := length
    for i := 0; i < length ; i++ {
        for j := 0; j < length -1; j++ {
            if nums[j] == 0 {
                nums[j], nums[j+1] = nums[j+1], nums[j]
            }
        }
    }
}
  • I don't think this code is efficient.
  • Because it loops many times.

sample 6600 KB submission

func moveZeroes(nums []int)  {
    z := 0
    for i, n := range nums {
        if n != 0 {
            x := nums[i]
            nums[i] = nums[z]
            nums[z] = x
            z++
        }
    }
}

sample 6700 KB submission

func Switch(nums []int, i int, j int) {
    nums[i], nums[j] = nums[j], nums[i]
}

func moveZeroes(nums []int)  {
    nonZero := make([]int, len(nums))
    var index int

    for _, num := range nums {
        if num != 0 {
            nonZero[index] = num
            index++
        }
    }

    for i := range nums {
        nums[i] = nonZero[i]
    }
}
728x90
반응형

관련글 더보기

댓글 영역