美文网首页Swift algorithm practice
80. Remove Duplicates from Sorte

80. Remove Duplicates from Sorte

作者: d1497e8e780a | 来源:发表于2019-02-01 15:08 被阅读6次

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
  }
}

相关文章

网友评论

    本文标题:80. Remove Duplicates from Sorte

    本文链接:https://www.haomeiwen.com/subject/cordsqtx.html