137. 只出现一次的数字 II
给你一个整数数组 nums
,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。
示例 1:
输入:nums = [2,2,3,2] 输出:3
示例 2:
输入:nums = [0,1,0,1,0,1,99] 输出:99
提示:
1 <= nums.length <= 3 * 104
-231 <= nums[i] <= 231 - 1
nums
中,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次
进阶:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
原站题解
rust 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2023-10-15 11:06:56
impl Solution { pub fn single_number1(nums: Vec<i32>) -> i32 { let mut ans = 0; for i in 0..32 { let mut cnt1 = 0; for &x in &nums { cnt1 += x >> i & 1; } ans |= cnt1 % 3 << i; } ans } pub fn single_number2(nums: Vec<i32>) -> i32 { let mut a = 0; let mut b = 0; for &x in &nums { let tmp_a = a; a = (a ^ x) & (a | b); b = (b ^ x) & !tmp_a; } b } pub fn single_number(nums: Vec<i32>) -> i32 { let mut a = 0; let mut b = 0; for &x in &nums { b = (b ^ x) & !a; a = (a ^ x) & !b; } b } }
javascript 解法, 执行用时: 60 ms, 内存消耗: 42.8 MB, 提交时间: 2023-10-15 11:06:12
/** * @param {number[]} nums * @return {number} */ var singleNumber = function(nums) { let a = 0, b = 0; for (const x of nums) { b = (b ^ x) & ~a; a = (a ^ x) & ~b; } return b; }; var singleNumber2 = function(nums) { let a = 0, b = 0; for (const x of nums) { [a, b] = [(a ^ x) & (a | b), (b ^ x) & ~a] } return b; }; var singleNumber3 = function(nums) { let ans = 0; for (let i = 0; i < 32; i++) { let cnt1 = 0; for (const x of nums) { cnt1 += x >> i & 1; } ans |= cnt1 % 3 << i; } return ans; };
golang 解法, 执行用时: 4 ms, 内存消耗: 3.3 MB, 提交时间: 2023-10-15 11:05:32
func singleNumber1(nums []int) int { ans := int32(0) for i := 0; i < 32; i++ { cnt1 := int32(0) for _, x := range nums { cnt1 += int32(x) >> i & 1 } ans |= cnt1 % 3 << i } return int(ans) } func singleNumber2(nums []int) int { a, b := 0, 0 for _, x := range nums { a, b = (a^x)&(a|b), (b^x)&^a } return b } func singleNumber(nums []int) int { a, b := 0, 0 for _, x := range nums { b = (b ^ x) &^ a a = (a ^ x) &^ b } return b }
cpp 解法, 执行用时: 4 ms, 内存消耗: 9.5 MB, 提交时间: 2023-10-15 11:04:48
class Solution { public: int singleNumber1(vector<int> &nums) { int ans = 0; for (int i = 0; i < 32; i++) { int cnt1 = 0; for (int x: nums) { cnt1 += x >> i & 1; } ans |= cnt1 % 3 << i; } return ans; } int singleNumber2(vector<int> &nums) { int a = 0, b = 0; for (int x: nums) { int tmp_a = a; a = (a ^ x) & (a | b); b = (b ^ x) & ~tmp_a; } return b; } int singleNumber(vector<int> &nums) { int a = 0, b = 0; for (int x: nums) { b = (b ^ x) & ~a; a = (a ^ x) & ~b; } return b; } };
java 解法, 执行用时: 0 ms, 内存消耗: 42.9 MB, 提交时间: 2023-10-15 11:03:59
class Solution { public int singleNumber1(int[] nums) { int ans = 0; for (int i = 0; i < 32; i++) { int cnt1 = 0; for (int x : nums) { cnt1 += x >> i & 1; } ans |= cnt1 % 3 << i; } return ans; } public int singleNumber2(int[] nums) { int a = 0, b = 0; for (int x : nums) { int tmpA = a; a = (a ^ x) & (a | b); b = (b ^ x) & ~tmpA; } return b; } public int singleNumber(int[] nums) { int a = 0, b = 0; for (int x : nums) { b = (b ^ x) & ~a; a = (a ^ x) & ~b; } return b; } }
python3 解法, 执行用时: 48 ms, 内存消耗: 14.4 MB, 提交时间: 2020-11-19 01:51:50
class Solution: def singleNumber(self, nums: List[int]) -> int: once = twice = 0 for num in nums: once = ~twice & (once^num) twice = ~once & (twice^num) return once
python3 解法, 执行用时: 36 ms, 内存消耗: 14.6 MB, 提交时间: 2020-11-18 22:16:49
class Solution: def singleNumber(self, nums: List[int]) -> int: counter = collections.Counter(nums) for (i, k) in counter.items(): if k == 1: return i
python3 解法, 执行用时: 48 ms, 内存消耗: 14.8 MB, 提交时间: 2020-11-18 22:14:45
class Solution: def singleNumber(self, nums: List[int]) -> int: return (3 * sum(set(nums)) - sum(nums)) // 2