Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]
Example 2:
Input: nums = [1,1]
Output: [2]
Constraints:
Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
func findDisappearedNumbers(nums []int) []int {
n := len(nums)
i := 0
for i < n {
if nums[i] == nums[nums[i]-1] {
i++
} else {
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
}
}
missing := make([]int, 0)
for i, v := range nums {
if v != i+1 {
missing = append(missing, i+1)
}
}
return missing
}
func findDisappearedNumbers(nums []int) []int {
var results []int
for i := 0; i < len(nums); i++ {
if i != nums[i] - 1 && nums[i] - 1 < len(nums) && nums[nums[i]-1] != nums[i] {
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
i--
}
}
for i := range nums {
if nums[i] != i + 1 {
results = append(results, i + 1)
}
}
return results
}
func findDisappearedNumbers(nums []int) []int {
ans := []int{}
for _, v := range nums {
idx := abs(v) - 1
if nums[idx] > 0 { nums[idx] = -nums[idx] }
}
for i, v := range nums {
if v > 0 { ans = append(ans, i + 1) }
}
return ans
}
func abs(n int) int {
if n > 0 { return n }
return -n
}
func findDisappearedNumbers(nums []int) []int {
result := make([]int, len(nums))
for _, v := range nums {
result[v-1] = v
}
j := 0
for i := 0; i < len(nums); i++ {
if result[i] == 0 {
result[j] = i + 1
j++
}
}
return result[:j]
}
func findDisappearedNumbers(nums []int) []int {
for _,v := range nums {
ab := abs(v)
nums[ab-1] = -abs(nums[ab-1])
}
missing := make([]int, 0, len(nums))
for i, v := range nums {
if v > 0 {
missing = append(missing, i+1)
}
}
return missing
}
func abs(x int) int {
if x > 0 {
return x
}
return -x
}
func findDisappearedNumbers(nums []int) []int {
n := len(nums)
seen := make([]bool, len(nums)+1)
for _, num := range nums {
seen[num] = true
}
var res []int
for i := 1; i <=n;i++ {
if !seen[i] {
res = append(res, i)
}
}
return res
}
func findDisappearedNumbers(nums []int) []int {
output := make([]int, 0)
numberMatched := false
length := len(nums)
for i:=1; i<=length; i++{
numberMatched = false
if len(nums) == 0{
break
}
for j:=0; j<len(nums); j++{
if i == nums[j]{
numberMatched = true
nums = append(nums[:j], nums[j+1:]...)
numberMatched = true
break
}
}
if numberMatched == false{
output = append(output, i)
}
}
return output
}
func findDisappearedNumbers(nums []int) []int {
//array to store result nums
disappearedNums := []int{}
for i := 0; i < len(nums); i++ {
//find out whether or not an element "i+1" exist in input array
_, found := Find(nums, i+1)
//element is not found
if !found {
disappearedNums = append(disappearedNums, i+1)
}
}
return disappearedNums
}
func Find(slice []int, val int) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func findDisappearedNumbers(nums []int) []int {
disappeared := 0
for idx := 0; idx < len(nums); idx++ {
for nums[idx] != idx+1 {
if nums[nums[idx]-1] == nums[idx] {
disappeared++
break
}
nums[idx], nums[nums[idx]-1] = nums[nums[idx]-1], nums[idx]
}
}
count := 0
for idx := 0; idx < len(nums) && count < disappeared; idx++ {
if nums[idx] != idx+1 {
nums[count] = idx+1
count++
}
}
return nums[:count]
}
Array and String - Introduction to Array from java to Go (0) | 2022.03.22 |
---|---|
Array101 Squares of a Sorted Array in Go (0) | 2022.03.21 |
Array 101 Third Maximum Number in go (0) | 2022.03.19 |
Array 101 Height Checker (0) | 2022.03.17 |
Array101 Remove Element (0) | 2022.03.15 |
댓글 영역