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

448. 找到所有数组中消失的数字 #38

Open
pigpigever opened this issue Dec 31, 2019 · 0 comments
Open

448. 找到所有数组中消失的数字 #38

pigpigever opened this issue Dec 31, 2019 · 0 comments
Labels
哈希表 哈希表类型的算法题

Comments

@pigpigever
Copy link
Owner

解题思路

  • 利用哈希表,将数组中的数字作为 key,用 boolean 值标记是否出现过
  • 1nums.length 遍历一次,每次从哈希表中取 key 值,判断该值是否被标记过
  • 没有被标记过的值 push 到数组里面

代码

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var findDisappearedNumbers = function(nums) {
    const map = new Map(), res = []
    for (const num of nums) {
        const val = map.get(num)
        map.set(num, true)
    }
    for (let i = 1; i <= nums.length; i++) {
        if (!map.get(i)) {
            res.push(i)
        }
    }
    return res
};
@pigpigever pigpigever added the 哈希表 哈希表类型的算法题 label Dec 31, 2019
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