Single Number -- leetcode
2015-05-29 15:55
423 查看
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?
基本思路:
利用异或运算。
一个数和自己异或结果为0.
0和一个数异或,则为该数。
由于数组中,每个数,都会出现两遍。只有一个数例外。
则对该数组元素全体作完异或后,结果为那个例外的数。 因为其他数都互相抵消了。
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
基本思路:
利用异或运算。
一个数和自己异或结果为0.
0和一个数异或,则为该数。
由于数组中,每个数,都会出现两遍。只有一个数例外。
则对该数组元素全体作完异或后,结果为那个例外的数。 因为其他数都互相抵消了。
class Solution { public: int singleNumber(vector<int>& nums) { int ans = 0; for (int i=0; i<nums.size(); i++) { ans ^= nums[i]; } return ans; } };
相关文章推荐
- DOJO(一) Hello Dojo!!
- wince中的串口编程
- ios 常见问题
- ndk r9d for max下载地址
- java项目开发中MyEclipse新建工作空间后的配置详细步骤
- LA 3303 / POJ 2675 Songs (数学证明+排序)
- ajax调用.net webservice
- 【转】一张图学会python编程
- [Selenium] 如何使ChromeDriver 每次启动的端口不会随机变化
- CodeBlocks调试功能快捷教程
- jenkins + git+maven做持续集成测试版本控制(未完成版)
- 开源项目NumberProgressBar的简单使用
- easyui datagrid中动态改变某行的颜色
- 关于MemStore的刷新方式
- jsoup中文API
- Eclipse运行maven的jetty插件内存溢出解决
- 不受天气影响的文档管理
- Python中函数的参数定义和可变参数
- shell oracle(sqlplus spool)
- 移动H5前端性能优化指南