-
Notifications
You must be signed in to change notification settings - Fork 0
/
0283.go
56 lines (50 loc) · 1.19 KB
/
0283.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Source: https://leetcode.com/problems/move-zeroes
// Title: Move Zeroes
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 <= 10^4
// -2^31 <= nums[i] <= 2^31 - 1
//
// Follow up: Could you minimize the total number of operations done?
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func moveZeroes(nums []int) {
i := 0
for _, v := range nums {
if v != 0 {
nums[i] = v
i++
}
}
for ; i < len(nums); i++ {
nums[i] = 0
}
}
// Use swap
func moveZeroes2(nums []int) {
i := 0
for j := range nums {
if nums[j] != 0 {
nums[i], nums[j] = nums[j], nums[i]
i++
}
}
}