本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

Leetcode: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 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         // Note: The Solution object is instantiated only once and is reused by each test case.
 5         int res = 0;
 6         for(int i = 0; i < n; i++)
 7             res = res^A[i];
 8         return res;
 9     }
10 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3422329.html

posted @ 2013-11-13 22:34  tenos  阅读(478)  评论(0编辑  收藏  举报

本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

公益页面-寻找遗失儿童