美文网首页
leetcode:数组的重复数系列

leetcode:数组的重复数系列

作者: air03_30 | 来源:发表于2019-03-10 20:29 被阅读0次

题目一:Find All Duplicates in an Array

1、题目链接

leetcode No448 https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/

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

Find all the elements that appear twice in this array.

Could you do it without extra space and in O(n) runtime?

Example:
Input:
[4,3,2,7,8,2,3,1]

Output:
[2,3]

2、Solution

题目的意思是,有一个n个数的数组,每个数的大小都在[1,n]之间,有的数出现了2次,有的数出现了1次。找到出现2次的数。

不用额外的空间,从数值大小的设定上来看,可以将数值和数组的索引关联起来,出现过的数,我们可以将对应索引上标记为负数,第二次出现时,如果索引上的值已被标记为负数,我们就知道它其实已经出现过一次啦~

go版的代码如下:

func findDuplicates(nums []int) []int {
    var res []int
    for i:=0;i<len(nums);i++{
        index := abs(nums[i])-1
        if nums[index] <0 {
            res =append(res,index+1)
        }
        nums[index] = -nums[index]
        
        
    }
    return res
}

func abs(num int) int{
    if num <0 {
        return -num
    }
    return num
}

题目二:Find All Numbers Disappeared in an Array

1、题目链接

leetcode No448:https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/

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]

2、Solution

其实这个和题目一的思路几乎一模一样。
题目的意思是,有一个n个数的数组,每个数的大小都在[1,n]之间,有的数出现了2次,有的数出现了1次。找到没有出现过的数。

不用额外的空间,从数值大小的设定上来看,可以将数值和数组的索引关联起来,出现过的数,我们可以将对应索引上标记为负数,遍历一遍,不是负数的索引,就是一次都没有出现过的数啦~

golang的代码如下

func findDisappearedNumbers(nums []int) []int {
    var res []int
    for i:=0;i<len(nums);i++ {
        index := nums[i] -1 
        if(nums[i] < 0){
            index = nums[i]*(-1) - 1
        }
        if(nums[index] > 0){
            nums[index] = nums[index] * (-1)
        }
    }
    for i:=0;i<len(nums);i++{
        if(nums[i]>0){
            res = append(res,i+1)
        }
    }
    return res
}

相关文章

  • leetcode:数组的重复数系列

    题目一:Find All Duplicates in an Array 1、题目链接 leetcode No448...

  • 寻找重复数 LeetCode287

    寻找重复数 LeetCode287 难点:在于题目限制 :不能更改原数组(假设数组是只读的)。只能使用额外的 O(...

  • First Missing Positive

    标签: C++ 算法 LeetCode 数组 每日算法——leetcode系列 问题 First Missing...

  • Next Permutation

    标签: C++ 算法 LeetCode 数组 每日算法——leetcode系列 问题 Next Permuta...

  • Trapping Rain Water

    标签: C++ 算法 LeetCode 数组 每日算法——leetcode系列 问题 Trapping Rain...

  • Combination Sum

    标签: C++ 算法 LeetCode 数组 DFS 每日算法——leetcode系列 问题 Combinat...

  • 常用sql语句

    删除重复数据 更新操作数组数据 插入去重更新

  • Search Insert Position

    标签: C++ 算法 LeetCode 数组 二分查找 每日算法——leetcode系列 问题 SeSearch...

  • 全排列

    给定一个无重复数字的array,求这个数组的全排列https://leetcode.com/problems/pe...

  • 数组中元素交换位置案例分析

    BUG描述: Leetcode剑指offer题目,链接:数组中的重复数字。 本人出错代码: 错误描述:该程序运行时...

网友评论

      本文标题:leetcode:数组的重复数系列

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