Swift algorithm practice

80. Remove Duplicates from Sorte

2019-02-01  本文已影响6人  d1497e8e780a

Swift 4.2
改变 allowRepeat 可以做拓展

class Solution {
  func removeDuplicates(_ nums: inout [Int]) -> Int {
    let allowRepeat = 2
    if nums.count <= allowRepeat {
      return nums.count
    }
    var index = allowRepeat
    
    for i in allowRepeat..<nums.count {
      if nums[i] != nums[index - allowRepeat] {
        nums[index] = nums[i]
        index += 1
      }
    }
    return index
  }
}
上一篇 下一篇

猜你喜欢

热点阅读