LeetCode 136

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?

1 public class Solution {
2     public int singleNumber(int[] nums) {
3         int result = 0;
4         for(int i = 0 ; i<nums.length ; i++ ){
5             result ^= nums[i];
6         }
7         return result;
8     }
9 }
 1 /*************************************************************************
 2     > File Name: LeetCode136.c
 3     > Author: Juntaran
 4     > Mail: JuntaranMail@gmail.com
 5     > Created Time: Tue 10 May 2016 07:37:54 PM CST
 6  ************************************************************************/
 7  
 8 /*************************************************************************
 9     
10     Single Number
11     
12     Given an array of integers, every element appears twice except for one. 
13     Find that single one.
14 
15     Note:
16     Your algorithm should have a linear runtime complexity. 
17     Could you implement it without using extra memory?
18 
19     Subscribe to see which companies asked this question
20 
21  ************************************************************************/
22 
23 #include <stdio.h>
24 
25 int singleNumber( int* nums, int numsSize )
26 {
27     int ret = 0;
28     
29     int i;
30     for( i=0; i<numsSize; i++ )
31     {
32         ret ^= nums[i];
33     }
34     return ret;
35 }
36 
37 int main()
38 {
39     int nums[] = { 1, 2, 3, 3, 1 };
40     int numsSize = 5;
41     int ret = singleNumber( nums, numsSize );
42     printf("%d
", ret);
43 
44     return 0;
45 }
原文地址:https://www.cnblogs.com/Juntaran/p/5428818.html