LeetCode--Single Number
2015-11-03 10:54
459 查看
题目:
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
代码:
运用"按位异或"
a^b^a = b;
将所有数异或,所有出现两次的都消除,得到出现一次的那个.
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
代码:
int singleNumber(vector<int>& nums) { int n = 0; for(int i = 0; i < nums.size(); ++i) { n ^= nums[i]; } return n; }
运用"按位异或"
a^b^a = b;
将所有数异或,所有出现两次的都消除,得到出现一次的那个.
相关文章推荐
- div有最小高度且自适应高度
- xcode7.1不能运行ios7模拟器问题
- 黑马程序员——Foundation框架—字符串方法
- 验证身份证,手机号码正则表达式
- hdu1059Dividing【多重背包】
- 字符串指针与字符数组的区别
- ubuntu下修改网卡名称
- Win32API:CreateDialog、DialogBox、DialogProc
- mysql常用查询语句
- 网页的排版布局和原则
- c语言学习之基础知识点介绍(十一):字符串的介绍、使用
- day3 文件系统 内核模块 ctags
- db2_高性能的sql语句
- linux 信号处理函数集合
- MyBatis:Mapped Statements collection does not contain value for xxx
- Eclipse启动Tomcat时45秒超时解决方法
- MySQL 5.7.9 免安装配置
- 1.4 使用Sqoop从MySQL数据库导入数据到HDFS
- heritrix3 后台运行job
- Subsets