LeetCode 137. Single Number II
原题链接在这里:https://leetcode.com/problems/single-number-ii/
题目:
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2] Output: 3
Example 2:
Input: [0,1,0,1,0,1,99] Output: 99
题解:
这道题的关键就是要求no extra memory space. 所以就用到了bit manipulation.
用one记录到当前计算的变量为止,二进制1出现"1次"(mod 3 之后的 1)的数位.
用two记录到当前计算的变量为止,二进制1出现"2次"(mod 3 之后的 2)的数位. update by ^= (once already appeared, num appeared again).
当one和two中的某一位同时为1时表示二进制1出现3次,此时需要清零.
即用二进制模拟三进制计算, 最终ones记录的是最终结果.
Time Complexity: O(n).
Space: O(1).
AC Java:
1 class Solution { 2 public int singleNumber(int[] nums) { 3 int one = 0, two = 0; 4 for(int num : nums){ 5 two ^= one & num; 6 one ^= num; 7 int notThree = ~(one & two); 8 two &= notThree; 9 one &= notThree; 10 } 11 12 return one; 13 } 14 }
另一方法是维护一个32位的数组,因为int 都是32位的,外层loop是对应每一位,内层loop是算所有的数在这一位上出现次数的和。
Mod 3 后剩下的就是那个只出现了一次的数。与Single Number类似.
Note:1. Check if current bit is 1 using:
nums[j] >> i & 1
但要注意的是bit operator: >>, & 都比 == operator 运算级别低,所以要加括号.
2. 给了一个array代表2进制的数,转化成10进制的数就是用
res += bitCounter[i]<<i
Time Complexity: O(n). Space: O(1).
AC Java:
1 class Solution { 2 public int singleNumber(int[] nums) { 3 int res = 0; 4 5 int [] bitCount = new int[32]; 6 for(int i = 0; i<32; i++){ 7 for(int j = 0; j<nums.length; j++){ 8 bitCount[i] = (bitCount[i] + (nums[j]>>i&1))%3; 9 } 10 } 11 12 for(int i = 0; i<32; i++){ 13 res += bitCount[i]<<i; 14 } 15 16 return res; 17 } 18 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· 提示词工程师自白:我如何用一个技巧解放自己的生产力
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· C#/.NET/.NET Core技术前沿周刊 | 第 31 期(2025年3.17-3.23)
· 如何不购买域名在云服务器上搭建HTTPS服务