1234567891011121314151617181920212223242526272829303132333435 |
- func findDisappearedNumbers(nums []int) (res []int) {
- n := len(nums)
- for i := 0; i < n; i++ {
- if idx := abs(nums[i]) - 1; 0 < nums[idx] {
- nums[idx] = -nums[idx]
- }
- }
- for i := 0; i < n; i++ {
- if 0 < nums[i] {
- res = append(res, i+1)
- }
- }
- return
- }
- func abs(x int) int {
- if x < 0 {
- return -x
- }
- return x
- }
- func findDisappearedNumbersOn(nums []int) (res []int) { // O(n) extra space
- n := len(nums)
- set := make([]bool, n+1)
- for _, i := range nums {
- set[i] = true
- }
- for i := 1; i <= n; i++ {
- if !set[i] {
- res = append(res, i)
- }
- }
- return
- }
|