zoukankan      html  css  js  c++  java
  • [Swift]LeetCode448. 找到所有数组中消失的数字 | Find All Numbers Disappeared in an Array

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

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

    Find all the elements of [1, n] inclusive that do not appear in this array.

    Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

    Example:

    Input:
    [4,3,2,7,8,2,3,1]
    
    Output:
    [5,6]

    给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。

    找到所有在 [1, n] 范围之间没有出现在数组中的数字。

    您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。

    示例:

    输入:
    [4,3,2,7,8,2,3,1]
    
    输出:
    [5,6]

    80ms
     1 class Solution {
     2     func findDisappearedNumbers(_ nums: [Int]) -> [Int] {
     3         
     4         var input = nums
     5         var result: [Int] = []
     6         
     7         for i in 0..<input.count{
     8             var check = -1
     9             
    10             if input[i] > 0{
    11                 check = input[i] - 1
    12             }else{
    13                 check = (input[i] * (-1)) - 1
    14             }
    15             
    16             if input[check] > 0{
    17                 input[check] = (input[check] * (-1))
    18             }
    19             
    20         }
    21         
    22         for i in 0..<input.count{
    23             if input[i] > 0 {
    24                 result.append(i+1)
    25             }
    26         }
    27         
    28         return result
    29     }
    30 }

    88ms

     1 class Solution {
     2     func findDisappearedNumbers(_ nums: [Int]) -> [Int] {
     3         var list = nums
     4         var results = [Int]()
     5 
     6         for i in 0 ..< list.count {
     7             let idx = abs(list[i]) - 1
     8             list[idx] = (list[idx] > 0) ? -list[idx] : list[idx]
     9         }
    10 
    11         for i in 0 ..< list.count {
    12             if (0 < list[i]) {
    13                 results.append(i + 1)
    14             }
    15         }
    16 
    17         return results
    18     }
    19 }

    104ms

     1 class Solution {
     2     func findDisappearedNumbers(_ nums: [Int]) -> [Int] {
     3         let test = 0
     4         if test == 0 {        
     5             var result = [Int]()
     6             var explored = [Bool](repeating: false, count: nums.count+1)
     7 
     8             for num in nums {
     9                 explored[num] = true
    10             }
    11 
    12             var i = 1
    13             while i < explored.count {
    14                 if explored[i] == false {
    15                     result.append(i)
    16                 }
    17 
    18                 i += 1
    19             }
    20 
    21             return result
    22         }
    23         
    24         if test == 1 {        
    25             var result = [Int]()
    26             var nums = nums
    27 
    28             var i = 0
    29             while i < nums.count {
    30                 while nums[i] != nums[nums[i]-1] {
    31                     nums.swapAt(i, nums[i]-1)
    32                 }
    33 
    34 
    35                 i += 1
    36             }
    37 
    38             var j = 0
    39             while j < nums.count {
    40                 if j+1 != nums[j] {
    41                     result.append(j+1)
    42                 }
    43 
    44                 j += 1
    45             }
    46 
    47             return result
    48         }
    49     }
    50 }

    112ms

     1 class Solution {
     2     func findDisappearedNumbers(_ nums: [Int]) -> [Int] {
     3         var nums = nums
     4         for idx in 0..<nums.count {
     5             let nextIdx = abs(nums[idx]) - 1
     6             nums[nextIdx] = nums[nextIdx] < 0 ? nums[nextIdx] : -nums[nextIdx]
     7         }
     8         var ans = [Int]()
     9         for idx in 0..<nums.count {
    10             if nums[idx] > 0 {
    11                 ans.append(idx + 1)
    12             }
    13         }
    14         return ans
    15     }
    16 }

    124ms

     1 class Solution {
     2     func findDisappearedNumbers(_ nums: [Int]) -> [Int] {
     3         var nums = nums
     4 
     5         for i in 0..<nums.count {
     6             if nums[abs(nums[i]) - 1] > 0 {
     7                 nums[abs(nums[i]) - 1] *= -1
     8             }
     9         }
    10 
    11         var res = [Int]()
    12 
    13         for i in 0..<nums.count {
    14             if nums[i] > 0 {
    15                 res.append(i + 1)
    16             } else {
    17                 nums[i] *= -1
    18             }
    19         }
    20 
    21         return res
    22     }
    23 }
  • 相关阅读:
    NoHttp开源Android网络框架1.0.0之架构分析
    3种浏览器性能測试
    自己定义控件-画板,橡皮擦,刮刮乐
    android优化 清除无效代码 UCDetector
    iOS推送 (百度推送)
    C#中的协变OUT和逆变
    使用反射构造对象实例并动态调用方法
    用反射获取构造函数带参数的实例对象
    自己实现一个IOC(控制翻转,DI依赖注入)容器
    func 和action 委托的使用
  • 原文地址:https://www.cnblogs.com/strengthen/p/9790831.html
Copyright © 2011-2022 走看看