Skip to content

Commit

Permalink
[Array] Add a solution to Find All Numbers Disappeared in an Array
Browse files Browse the repository at this point in the history
  • Loading branch information
gannasong committed Jul 18, 2019
1 parent 8500d75 commit a21f9fc
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Array/FindDisappearedNumbers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Question Link: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
* primary idea: Traverse the array and get num really position in array, then set negative.
* In the final, filter greater than 0 num.
*
* Time Complexity: O(n), Space Complexity: O(1)
*
*/

class FindDisappearedNumbers {
func findDisappearedNumbers(_ nums: [Int]) -> [Int] {
var nums = nums
var result = [Int]()

for i in 0..<nums.count {
let index = abs(nums[i]) - 1
if nums[index] > 0 {
nums[index] = -nums[index]
}
}

for i in 0..<nums.count {
if nums[i] > 0 {
result.append(i+1)
}
}

return result
}
}

0 comments on commit a21f9fc

Please sign in to comment.