[LeetCode] Single Number
2014-04-29 20:46
489 查看
Problem: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?
好久没有看Python了,已经几乎忘光光了。这是今年第一次使用Python写算法,在实现此算法的过程中,体会到Python是弱类型语言,且变量在引用之前必须声明且初始化。通过练习LeetCode中的算法,先用自己最熟悉的语言实现之,然后用另外两种语言再重写实习,主要是使用和学习另外两种语言。
附:C++实现代码
Java实现代码
注:万事开头难,加油吧,少年!!!
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?
好久没有看Python了,已经几乎忘光光了。这是今年第一次使用Python写算法,在实现此算法的过程中,体会到Python是弱类型语言,且变量在引用之前必须声明且初始化。通过练习LeetCode中的算法,先用自己最熟悉的语言实现之,然后用另外两种语言再重写实习,主要是使用和学习另外两种语言。
class Solution: # @param A, a list of integer # @return an integer def singleNumber(self, A): y=0 for x in A: y=y^x return y
附:C++实现代码
class Solution { public: int singleNumber(int A[], int n) { int xorResult=0; for(int i=0;i<n;++i){ xorResult=xorResult^A[i]; } return xorResult; } };
Java实现代码
public class Solution { public int singleNumber(int[] A) { int xorResult=0; for(int i=0;i<A.length;++i){ xorResult=xorResult^A[i]; } return xorResult; } }
注:万事开头难,加油吧,少年!!!
相关文章推荐
- LeetCode:Single Number
- 【LeetCode】136 Single Number
- [LeetCode]136. Single Number
- [Leetcode从零开刷]136. Single Number
- [leetcode]Single Number
- [LeetCode]Single Number
- LeetCode-Hash-136. Single Number
- LeetCode Single Number
- [C++]LeetCode: 66 Single Number
- leetcode 136-Single Number
- [LeetCode] Single Number
- Leetcode 136. single number
- LeetCode --Single Number
- LeetCode--Single Number(单独数字)Python
- [LeetCode] Single Number
- [Leetcode刷题]Single Number
- LeetCode 解题报告 Single Number
- [LeetCode]136. Single Number
- [LeetCode] Single Number
- Leetcode::Single Number