We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
原题链接: https://leetcode.cn/problems/single-number/
解题思路:
nums
Set
/** * @param {number[]} nums * @return {number} */ var singleNumber = function (nums) { let set = new Set() // 使用哈希表保存遍历时遇到的数字 for (const num of nums) { // 如果遇到出现过的数字,将其从Set中删除 if (set.has(num)) { set.delete(num) } else { // 第一次遇到的数字,将其加入Set set.add(num) } } // 最终Set中只剩下一个只出现过一次的元素 return [...set][0] }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:
https://leetcode.cn/problems/single-number/
解题思路:
nums
,使用Set
保存第一次遇到的数字。Set
中删除。Set
中只会留下一个数字,即为只出现了一次的数字。The text was updated successfully, but these errors were encountered: