剑指 Offer 56 - II. 数组中数字出现的次数 II

tech2025-11-05  4

剑指 Offer 56 - II. 数组中数字出现的次数 II - 力扣(LeetCode)

类似题:LeetCode第 137 题:只出现一次的数字II(C++)_zj-博客

class Solution { public: int singleNumber(vector<int>& nums) { int res = 0; for(int i = 0; i < 32; ++i){ int cnt = 0; for(const auto &num : nums){ cnt += (num >> i) & 1; } res ^= (cnt%3) << i; } return res; } };

空间复杂度为O(n)的方法很简单,就不写了。

最新回复(0)