zoukankan      html  css  js  c++  java
  • [Swift]LeetCode283. 移动零 | Move Zeroes

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

    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 }
  • 相关阅读:
    关于工作流程引擎中的岗位的设置的问题
    将要发布的开源CCOA的照片一览
    关于多个checkbox在IE6下自由表单设计器中的兼容问题
    ccflow流程自动发起功能增加,如何按指定的时间触发方式发起流程?
    Windows 如何远程强行关机
    Report bulder
    微软sample and code down web address
    如何查看sql server的死锁情况
    如何读取数据的所有用户表
    复制和数据库镜像
  • 原文地址:https://www.cnblogs.com/strengthen/p/9756870.html
Copyright © 2011-2022 走看看