LeetCode136:Single Number
题目:
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与任何数异或结果为原数
也可先进行排序再遍历排序后的数组,当某个数没有重复时,结果就是它了
也可用bitmap进行,不多说了。
实现代码:
#include <iostream> using namespace std;
/*
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?
*/
class Solution {
public:
int singleNumber(int A[], int n) {
int ret = ;
for(int i = ; i < n; i++)
ret ^= A[i];
return ret; }
}; int main(void)
{
int arr[] = {,,,,,,};
int len = sizeof(arr) / sizeof(arr[]);
Solution solution;
int once = solution.singleNumber(arr, len);
cout<<once<<endl;
return ;
}
LeetCode136:Single Number的更多相关文章
- 异或巧用:Single Number
异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...
- LeetCode137:Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode之“散列表”:Single Number
题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...
- lintcode 中等题:Single number III 落单的数III
题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...
- leetcode:Single Number
public int SingleNumber(int[] nums) { if(nums==null||nums.Length%2==0) return 0; int ret=nums[0]; fo ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
随机推荐
- sharepoint 调查问卷权限设置
参考网址:http://www.cnblogs.com/mybi/archive/2011/04/18/2019935.html 按文章设置后发现访问时提示没有权限. 于是把新权限(问卷回复)的权限组 ...
- 解决selenium不支持firefox低版本的问题
解决selenium不支持firefox低版本的问题 在火狐浏览器升级后,突然发现webdriver运行脚本的时候不能调出火狐浏览器了,并报错WebDriverException:Message:'C ...
- ispostback的使用
如果form表单属性里没有 runat="server"就不能使用ispostback因为不会生成__viewstate隐藏域
- 利用Python和webhook实现自动提交代码
最近在为公司书写项目的api文档,计划利用码云的wiki管理整个项目,公司自有git作为项目内容依托,这样全员都可参与,而我定期向码云推送就可以了. 问题 根据需求遇见了这样一个问题:我每次从git上 ...
- 不使用if switch 各种大于 小于 判断2个数的大小
哥们写的代码: dword big; __asm { mov eax,a mov ebx,b cmp eax,ebx jle HOHO big =ebx HOHO: big = eax } 网上搜了一 ...
- js this pointer 指针
this JavaScript的函数内部如果调用了this,那么这个this到底指向谁? 答案是,视情况而定! 如果以对象的方法形式调用,比如xiaoming.age(),该函数的this指向被调用的 ...
- Silverlight实用窍门系列:57.Silverlight中的Binding使用(二)-数据验证
本文将简单讲述Silverlight中的Binding数据时的数据验证. NotifyOnValidationError:是否在出现异常/错误信息的时候激发BindingValidationError ...
- 64bits access
http://www.codicode.com/art/64_bit_version_of_microsoft_jet.aspx
- 全屏API
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2679 二.相关文章以 ...
- 电商类Web原型制作分享——聚美优品
这是一家化妆品限时特卖商城.作为美妆电商类网站的佼佼者,网站以用户体验为核心,画面主色调符合女性消费者审美.排版整齐,布局合理.网站用弹出面板实现点击弹出内容,鼠标悬停文字按钮颜色改变等交互效果. 本 ...