Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

错误的集合 #136

Open
yankewei opened this issue Jul 4, 2021 · 1 comment
Open

错误的集合 #136

yankewei opened this issue Jul 4, 2021 · 1 comment
Labels
哈希表 题目包含哈希表解法 简单 题目难度为简单

Comments

@yankewei
Copy link
Owner

yankewei commented Jul 4, 2021

集合 s 包含从 1 到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个数字复制了成了集合里面的另外一个数字的值,导致集合 丢失了一个数字 并且 有一个数字重复 。

给定一个数组 nums 代表了集合 S 发生错误后的结果。

请你找出重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。

示例 1:

输入:nums = [1,2,2,4]
输出:[2,3]

示例 2:

输入:nums = [1,1]
输出:[1,2]

提示:

  • 2 <= nums.length <= 104
  • 1 <= nums[i] <= 104

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/set-mismatch
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

@yankewei yankewei added 哈希表 题目包含哈希表解法 简单 题目难度为简单 labels Jul 4, 2021
@yankewei
Copy link
Owner Author

yankewei commented Jul 4, 2021

哈希表

func findErrorNums(nums []int) []int {
    ret := make([]int, 2)
    slice := make([]bool, len(nums))
	for _, v := range nums {
	    if slice[v-1] {
	        ret[0] = v
	    } else {
		slice[v-1] = true
	    }
	}
	for i, v := range slice {
	    if v == false {
		ret[1] = i + 1
		break
	    }
	}
    return ret
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
哈希表 题目包含哈希表解法 简单 题目难度为简单
Projects
None yet
Development

No branches or pull requests

1 participant