LeetCode---Submission Details
2015-09-18 16:35
459 查看
题目大意:给出一个一维数组,该数组中每个元素的值都出现过两次只有一个元素值只出现一次,现在找出这个元素。要求在线性时间内完成,不适用额外的空间。
算法思想:
1.对数组排序
2.遍历数组,比较相邻的两个元素值是否相同,若相同则比较下一组相邻元素,若不相同则返回该元素。注意迭代器的值每次递增2
代码如下:
算法思想:
1.对数组排序
2.遍历数组,比较相邻的两个元素值是否相同,若相同则比较下一组相邻元素,若不相同则返回该元素。注意迭代器的值每次递增2
代码如下:
class Solution { public: int singleNumber(vector<int>& nums) { sort(nums.begin(),nums.end()); vector<int>::iterator pos; for(pos=nums.begin();pos!=nums.end();pos+=2){ if(*pos==*(pos+1)) continue; return *pos; } } };
相关文章推荐
- 设计模式之职责链模式(Chain Of Responsibility)
- Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536
- nonatomic, retain,weak,strong用法详解
- Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE
- GTS Test fail testTestTelephonyCall_bindsToInCallServiceAndAddsCall
- boost的安装问题 http://blog.csdn.net/begtostudy/article/details/1879213
- [Leetcode] Container With Most Water
- Learning to rank的讲解,单文档方法(Pointwise),文档对方法(Pairwise),文档列表方法(Listwise)
- [Leetcode] 11 Container With Most Water
- Codeforces Round #319 (Div. 1)E.Painting Edges(并查集)
- Contains Duplicate II
- HDU 4819 Mosaic D区段树
- Contains Duplicate
- 添加第三方类库造成的linker command failed with exit code 1 (use -v to see invocation)的错误调试
- Magic Pairs - SGU 119(同余)
- 升级xcode7之后,真机调试出现BaiduMapAPI.framework/BaiduMapAPI(BMAddrList.o)' does n 4000 ot contain bitcode的问题
- INSTALL_FAILED_DUPLICATE_PERMISSION
- Contains Duplicate 重复数的判断
- 端口状态说明 LISTENING、ESTABLISHED、TIME_WAIT及CLOSE_WAIT
- SSH 错误解决案例1:Read from socket failed: Connection reset by peer