为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode283. 移动零 | Move Zeroes

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9756870.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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:

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

Note:

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

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]

说明:

  1. 必须在原数组上操作,不能拷贝额外的数组。
  2. 尽量减少操作次数。

 

16ms

 1 class Solution {
 2     func moveZeroes(_ nums: inout [Int]) {
 3         //0要后移,反意是非0元素前移。
 4         if nums == nil || nums.count == 0
 5         {
 6             return
 7         }
 8         //记录非0元素开始位置
 9         var j:Int = 0
10         for i in 0..<nums.count
11         {
12             if nums[i] != 0
13             {             
14                 nums[j] = nums[i]
15                 j += 1
16             }
17         }
18         while(j < nums.count)
19         {
20             nums[j] = 0
21             j += 1
22         }
23     }
24 }

16ms

 1 class Solution {
 2     func moveZeroes(_ nums: inout [Int]) {
 3         var firstPointer = 0 
 4         var secondPointer = 0
 5          
 6         while secondPointer < nums.count {
 7             if nums[firstPointer] == 0 {
 8                 if nums[secondPointer] != 0 {
 9                     swap(&nums, firstPointer, secondPointer)
10                 } else {
11                     secondPointer += 1
12                 }
13             } else {
14                 secondPointer += 1
15                 firstPointer += 1
16             }
17         }
18     }
19     
20     func swap(_ nums: inout [Int], _ firstIndex: Int, _ secondIndex: Int) {
21         var temp = nums[firstIndex]
22         nums[firstIndex] = nums[secondIndex]
23         nums[secondIndex] = temp
24     }
25 }

16ms

 1 class Solution {
 2     func moveZeroes(_ nums: inout [Int]) {
 3         var index1 = 0
 4         var index2 = nums.count
 5         while index1 <= index2 - 1 {
 6             if nums[index1] == 0 {
 7                 for _ in 0..<(index2-index1) {
 8                     index2 -= 1
 9                     if nums[index2] != 0 {
10                         nums.remove(at: index1)
11                         nums.append(0)
12                         break;
13                     }
14                 }
15             }else {
16                 index1 += 1
17             }
18         }
19     }
20 }

20ms

 1 class Solution {
 2     func moveZeroes(_ nums: inout [Int]) {
 3         var removedCount = 0
 4         for (idx, val) in nums.enumerated() {
 5             if val == 0 {
 6                 nums.remove(at: idx-removedCount)
 7                 removedCount += 1
 8                 nums.append(0)
 9             }
10         }
11     }
12 }

 

posted @ 2018-10-08 20:13  为敢技术  阅读(334)  评论(0编辑  收藏  举报