班玛县公司网站建设,登陆网站空间的后台,软件app网站建设,在线识别图片找原图leetCode 137. 只出现一次的数字 II 题解可看我的往期文章
leetCode 137. 只出现一次的数字 II 位运算 模3加法器 真值表#xff08;数字电路#xff09; 有限状态机-CSDN博客https://blog.csdn.net/weixin_41987016/article/details/134138112?spm1001.2014.3001.5501… leetCode 137. 只出现一次的数字 II 题解可看我的往期文章
leetCode 137. 只出现一次的数字 II 位运算 模3加法器 真值表数字电路 有限状态机-CSDN博客https://blog.csdn.net/weixin_41987016/article/details/134138112?spm1001.2014.3001.5501【拓展思考】如果改成除了一个数字出现一次其余数字均出现 5 次呢
(一)「同时计算」 化简 b 和 c #include iostream
#include vector
using namespace std;int singleNumber(vectorint nums) {int i, a, b, c, tmpa, tmpb, tmpc;a 0;b 0;c 0;for (const int x : nums) {// 第一种tmpa a;tmpb b;tmpc c;a a ~tmpb ~tmpc ~x | ~a tmpb tmpc x;b ~tmpa b (~tmpc | tmpc) | ~tmpa x (b ^ tmpc);c ~tmpa (c ^ x);}return c;
}int main() {vectorint nums{3,3,3,3,3,2,2,2,2,2,6,6,6,6,6,4,4,4,10,4,4 };cout打印结果:singleNumber(nums) endl;return 0;
}
(二)「分别计算」 发现上面化简c后式子很简洁 #include iostream
#include vector
using namespace std;int singleNumber(vectorint nums) {int i, a, b, c, tmpa, tmpb, tmpc;a 0;b 0;c 0;for (const int x : nums) {// 第二种c ~a (c ^ x);b ~a ~c (b ^ x) | ~a b c;a ~b ~c (a ^ x);}return c;
}int main() {vectorint nums{3,3,3,3,3,2,2,2,2,2,6,6,6,6,6,4,4,4,10,4,4 };cout打印结果:singleNumber(nums) endl;return 0;
}