题目:

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?

解题思路:

很简单的一题,直接用异或运算解决:连个相同数进行异或结果为0,0与任何数异或结果为原数

也可先进行排序再遍历排序后的数组,当某个数没有重复时,结果就是它了

也可用bitmap进行,不多说了。

实现代码:

#include <iostream>

using namespace std;
/*
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?
*/
class Solution {
public:
int singleNumber(int A[], int n) {
int ret = ;
for(int i = ; i < n; i++)
ret ^= A[i];
return ret; }
}; int main(void)
{
int arr[] = {,,,,,,};
int len = sizeof(arr) / sizeof(arr[]);
Solution solution;
int once = solution.singleNumber(arr, len);
cout<<once<<endl;
return ;
}

LeetCode136:Single Number的更多相关文章

  1. 异或巧用:Single Number

    异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...

  2. LeetCode137:Single Number II

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

  3. LeetCode之“散列表”:Single Number

    题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...

  4. lintcode 中等题:Single number III 落单的数III

    题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...

  5. leetcode:Single Number

    public int SingleNumber(int[] nums) { if(nums==null||nums.Length%2==0) return 0; int ret=nums[0]; fo ...

  6. [LeetCode] Single Number III 单独的数字之三

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  7. [LeetCode] Single Number II 单独的数字之二

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  8. [LeetCode] Single Number 单独的数字

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  9. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

随机推荐

  1. jquery 使用方法(转载)

    原文地址:http://www.cnblogs.com/Chenfengtao/archive/2012/01/12/2320490.html jQuery是目前使用最广泛的javascript函数库 ...

  2. tortoise svn 请求的名称有效,但是找不到请求的类型的数据

    可能是博客园出bug了,现在无法放图,间断重复刷新十多次依然如故,弃之 这个问题是在安装完svn服务器(visual svn)后复制url,tortoise svn import 粘贴url后 出现的 ...

  3. Spring框架的事务管理之基于AspectJ的注解方式(重点掌握,最简单的方式)

    1. 步骤一:恢复转账的开发环境(具体开发环境实现见:https://www.cnblogs.com/wyhluckdog/p/10137283.html)2. 步骤二:applicationCont ...

  4. WPF之数据触发器 改变控件背景色或闪烁

    需求,很多矩形表示桶,其中:空桶=红色,满桶=绿色,使用中=红绿闪烁. <Window x:Class="FlickerDemo.MainWindow" xmlns=&quo ...

  5. python的代码检查

    #!/bin/python3.4# coding=utf-8 class lexicon(object): # direction = ['north', 'south', 'east', 'west ...

  6. Luogu 1070 道路游戏

    看完题面想了一会发现只会写$n^3$,愣了一会才想出了单调队列优化的做法. 90分算法: 设$f_{i, j, k}$表示第$i$分钟在第$j$座城市已经走了$k$步的最大价值,转移显然,时间复杂度$ ...

  7. Ubuntu12.04添加环境变量

    环境变量分为系统级和用户级. 系统级变量设置环境为/etc/environment和/etc/profile等,不要轻易修改,否则可能造成系统错误. 用户级变量设置路径为-/.bashrc和~/.pr ...

  8. UI设计小白怎样学才能快速入门?

    最近有很多同学问我,UI小白有什么捷径可以快速入门?其实我想说设计这门学科没有什么捷径,真的只有多学.多看.多练.多想.多做. 1.先学软件 如果你是小白,软件也不会用,那就先学软件,从PS开始,先看 ...

  9. laravel在控制器中赋值给视图

    1.控制器 2.视图

  10. mysql的头文件

    yum install mysql /usr/include/mysql yum install mysql-devel