博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[容易]落单的数
阅读量:4603 次
发布时间:2019-06-09

本文共 1287 字,大约阅读时间需要 4 分钟。

题目来源:http://www.lintcode.com/zh-cn/problem/single-number/

方法:

异或满足交换律,任意两个相同的数可以异或是0。0和任何数异或的结果是该数,那么最后的结果一定是落单的数字。

C++版 VS2012测试通过:

1 //#include 
2 //#include
3 //#include
4 //#include
5 //using namespace std; 6 7 class Solution { 8 public: 9 /**10 * @param A: Array of integers.11 * return: The single number.12 */13 int singleNumber(vector
&A) {14 // write your code here15 int x=0;16 for (int i = 0; i < A.size(); i++) {17 x ^= A[i];18 }19 return x;20 }21 };22 23 //测试24 //int main()25 //{26 // Solution s;27 // int a[]={1,2,2,1,3,4,3};28 // int i=sizeof(a)/sizeof(int);29 // vector
v(a,a+i);30 // cout<

Python2.7版 spider测试通过:

1 class Solution: 2     """ 3    @param A : an integer array 4    @return : a integer 5    """ 6     def singleNumber(self, A): 7         # write your code here 8         ans = 0; 9         for x in A:10             ans = ans ^ x11         return ans12 #测试        13 #if __name__=='__main__':14 #    s=Solution()15 #    n=[1,2,2,1,3,4,3]16 #    print s.singleNumber(n)

转载于:https://www.cnblogs.com/hslzju/p/5624924.html

你可能感兴趣的文章
codevs3027 线段覆盖 2
查看>>
markdown
查看>>
【leetcode】107-Binary Tree Level Order Traversal II
查看>>
Jquert data方法获取不到数据,显示为undefined。
查看>>
ssm项目中 数据库和资源的备份
查看>>
hdoj5671 BestCoder Round #81 (div.2)
查看>>
HDU5950【矩阵快速幂】
查看>>
在线C++编译器
查看>>
C#中各种serialization的比较
查看>>
P2617 Dynamic Rankings
查看>>
工作学习常识1
查看>>
Linux小知识点
查看>>
VisualVM监控远程主机
查看>>
C#中检查网络是否连通的二种方法
查看>>
节假日设置
查看>>
网络游戏_客户端
查看>>
Tomcat8 配置APR模式
查看>>
<五>初探opengl,编写我们的镜头
查看>>
大数据操作:删除和去重
查看>>
《那些年啊,那些事——一个程序员的奋斗史》——28
查看>>