详细见:leetcode.com/problems/single-number
Java Solution: github
package leetcode;
/*
* 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?
*/
public class P136_SingleNumber {
public static void main(String[] args) {
int[] arr = tools.Random随机生成器.A_生成一个随机数据(100, 0, 900);
System.out.println(tools.Utils.LEETCODE_int_array_序列化_(arr));
// int[] new_arr = new int[] {
// 315, 140, 472, 637, 280, 741, 416, 105, 754, 206, 75, 729, 496, 61, 444, 865, 707, 813, 686, 243, 531, 771, 463, 890, 133, 623, 873, 381, 422, 218, 9, 395, 807, 693, 23, 159, 736, 816, 154, 17, 138, 466, 339, 493, 586, 896, 798, 136, 690, 170, 367, 625, 801, 408, 827, 569, 5, 24, 57, 733, 400, 275, 533, 166, 230, 342, 672, 503, 750, 517, 529, 616, 818, 590, 213, 726, 608, 691, 645, 37, 747, 134, 516, 872, 505, 129, 661, 896, 70, 81, 137, 148, 83, 157, 520, 312, 701, 417, 446, 830,
// 315, 140, 472, 637, 280, 741, 416, 105, 754, 206, 75, 729, 496, 61, 444, 865, 707, 813, 686, 243, 531, 771, 463, 890, 133, 623, 873, 381, 422, 218, 9, 395, 807, 693, 23, 159, 736, 816, 154, 17, 138, 466, 339, 493, 586, 896, 798, 136, 690, 170, 367, 625, 801, 408, 827, 569, 5, 24, 57, 733, 400, 275, 533, 166, 230, 342, 672, 503, 750, 517, 529, 616, 818, 590, 213, 726, 608, 691, 645, 37, 747, 134, 516, 872, 505, 129, 661, 896, 70, 81, 137, 148, 83, 157, 520, 312, 701, 417, 446, 830
// };
// Solution s = new Solution();
// System.out.println(s.singleNumber(new_arr));
}
/*
* 1 ms
* 41.62%
*/
static class Solution {
public int singleNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int ans = 0;
for (int val : nums) {
ans ^= val;
}
return ans;
}
}
}
C Solution: github
/*
url: leetcode.com/problems/single-number
AC 6ms 15.50%
*/
#include <stdio.h>
#include <stdlib.h>
int singleNumber(int* n, int nn) {
int ans = 0, i = 0;
for (i = 0; i < nn; i ++) {
ans ^= n[i];
}
return ans;
}
int main() {
int n[] = {1, 2, 2, 1, 3 ,4, 4};
int nn = 7;
printf("answer is %d\n", singleNumber(n, nn));
}
Python Solution: github
#coding=utf-8
'''
url: leetcode.com/problems/single-number
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年5月18日
@details: Solution: 89ms 68.38%
'''
from _operator import xor
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = 0
for n in nums:
ans = xor(ans, n)
return ans;
if __name__ == "__main__":
nums = [1, 2, 2, 1 ,4, 3, 3]
print(Solution().singleNumber(nums))