[LeedCode OJ]#136 Single Number
2015-08-24 16:47
134 查看
【 声明:版权所有,转载请标明出处,请勿用于商业用途。 联系信箱:libin493073668@sina.com】
题目链接:https://leetcode.com/problems/single-number/
题意:
找出数组中只出现过一次的数,要求在线性时间内解决且不使用额外的内存
思路:
我们可以通过排序实现,只要这个位置的数与前一位还有后一位的数都不相等,那么这个数就是单一的
题目链接:https://leetcode.com/problems/single-number/
题意:
找出数组中只出现过一次的数,要求在线性时间内解决且不使用额外的内存
思路:
我们可以通过排序实现,只要这个位置的数与前一位还有后一位的数都不相等,那么这个数就是单一的
class Solution { public: int singleNumber(vector<int>& nums) { int len = nums.size(); int i,j; sort(nums.begin(),nums.end()); if(nums[0]!=nums[1]) return nums[0]; for(i = 1; i<len; i++) { if(nums[i]!=nums[i-1]&&nums[i]!=nums[i+1]) return nums[i]; } return nums[len-1]; } };
相关文章推荐
- Android 适配
- WPF中GridSplitter控件的使用
- oracle 对现有的表进行列表分区
- AlarmManager关掉进程后不起作用的问题
- notepad++ 批量替换技巧
- CAP定理小议
- C++中的static关键字的总结
- C++中的模板那点事
- 算法题:最长上升子序列
- 使用Xcode DEBUG模式和RELEASE模式
- MFC_解决按下回车键退出程序的问题
- java中执行删除文件操作,要关闭输入输出流
- 玩转Bootstrap(基础) -- (6.导航条基础)
- 【数字概念】阿里苏宁联合:一种商业文明的倒退 63.65 万亿 10.4 万亿 1.68 万亿
- 一次数据库相关操作卡住的排查--enq: TX - row lock contention
- Mac上HAX is not installed on this machine 的解决办法
- c++取余问题
- 【动态规划】[COCI]摘樱桃
- BestCoder Round #52 (div.2)(hdu 5417,hdu 5418)
- EntityFramework DbContext 线程安全