当前位置:网站首页>LeetCode只出现一次的数字
LeetCode只出现一次的数字
2022-07-31 13:26:00 【超爱学习芸】
题目要求:
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
输入: [2,2,1] 输出: 1
输入:nums = [1,2,3,4] 输出:false
解题思路:
根据异或运算的规律,a^a = 0
;0^a = a;
a^b^a = a^a^b = b;
当数组进行异或运算后,只剩下单独的数字
代码展示:
int singleNumber(int* nums, int numsSize){
int k=0;
for(int i=0;i<numsSize;i++){
k^=nums[i];
}
return k;
}
边栏推荐
- Reasons and solutions for Invalid bound statement (not found)
- Error IDEA Terminated with exit code 1
- Flutter keyboard visibility
- What should I do if selenium is reversed?
- 八大排序汇总及其稳定性
- 图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
- Centos7 install mysql5.7
- 365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
- 关于MySQL主从复制的数据同步延迟问题
- MATLAB | 我也做了一套绘图配色可视化模板
猜你喜欢
Golang - gin - pprof - use and safety
抓住金三银四的尾巴,解锁程序员面试《刷题神器》
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
C# control ListView usage
C#获得网卡信息 NetworkInterface IPInterfaceProperties
LeetCode·304竞赛·6132·使数组中所有元素都等于零·模拟·哈希
NameNode (NN) and SecondaryNameNode (2NN) working mechanism
机器学习模型验证:被低估的重要一环
Architecture Camp | Module 8
ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning
随机推荐
365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
知名无人驾驶公司:文远知行内推
CentOS7 installation MySQL graphic detailed tutorial
golang中使用泛型
Golang - gin - pprof - use and safety
C#Assembly的使用
The latest complete code: Incremental training using the word2vec pre-training model (two loading methods corresponding to two saving methods) applicable to various versions of gensim
ECCV 2022 | Robotic Interaction Perception and Object Manipulation
P5019 [NOIP2018 提高组] 铺设道路
sqlalchemy determines whether a field of type array has at least one consistent data with an array
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
六石编程学:不论是哪个功能,你觉得再没用,会用的人都离不了,所以至少要做到99%
MATLAB | 我也做了一套绘图配色可视化模板
战略进攻能力的重要性,要远远高于战略防守能力
C#使用NumericUpDown控件
C# using NumericUpDown control
Error: npm ERR code EPERM
networkx绘制度分布
技能大赛训练题:登录安全加固
滑窗法切分数据