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-ii/
解题思路:
3
i
1
nums[i]
nums[i + 1]
i = i + 3
/** * @param {number[]} nums * @return {number} */ var singleNumber = function (nums) { // 将数组排序 nums.sort((a, b) => a - b) // 遍历nums,查找只出现1次的值 // 如果相邻两个数字相同,那么nums[i]肯定出现了3次,下次从i+3位置开始搜索 for (let i = 0; i < nums.length; i += 3) { // 如果遇到相邻两个数字不相等,nums[i]肯定只出现过一次 if (nums[i] !== nums[i + 1]) { return nums[i] } } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:
https://leetcode.cn/problems/single-number-ii/
解题思路:
3
个一组的形式出现。i
指向的都是3
个数字中的第1
个。nums[i]
与nums[i + 1]
不相等,那么nums[i]
只出现了一次。nums[i]
与nums[i + 1]
相等,那么查找下一组数字,即i = i + 3
。The text was updated successfully, but these errors were encountered: