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

非常简单的一道题。

直接相异或剩下的那个数就是答案。原理是两个相等的数异或的值为0。

 class Solution {
public:
int singleNumber(int A[], int n) {
int temp;
for(int i=;i!=n;i++)
temp=temp^A[i];
return temp;
}
};

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

用好位运算,多看看与或非到底能做什么。

 class Solution {
public:
int singleNumber(int A[], int n) {
int one=,two=,three=;
for(int i=;i!=n;i++){
three = A[i] & two;
two=two | (one & A[i]);
one = one | A[i];
one = one & ~three;
two = two & ~three;
}
return one;
}
};

[Leetcode]Single Number && Single Number II的更多相关文章

  1. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  2. 【一天一道LeetCode】#260. Single Number III

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  4. [LeetCode] Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  5. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  6. 乘风破浪:LeetCode真题_009_Palindrome Number

    乘风破浪:LeetCode真题_009_Palindrome Number 一.前言 如何判断一个整型数字是回文呢,我们可能会转换成String来做,但是还有更简单的方法. 二.Palindrome ...

  7. leetcode 321 Create Max Number

    leetcode 321 Create Max Number greedy的方法,由于有两个数组,我们很自然的想到从数组1中选i个数,数组2中选k-i个数,这样我们只需要遍历max(0, k-数组2长 ...

  8. leetcode bug & 9. Palindrome Number

    leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...

  9. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  10. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

随机推荐

  1. 混合使用C++语言和Objective-C语言

    如果你的源文件扩展名是.m的,你还需要改成.mm,这样编译器才知道你将会在该文件中混合使用C++语言和Objective-C语言.

  2. cordova/phoneGap 开发调试工具

    原文地址 一.前言 Ripple Emulate:使用Google模拟器,可以帮到大部分的调试,除了需要调用手机设备方面的功能除外.GapDebug:真机安装apk,电脑端和真机端同步调试,适用的项目 ...

  3. C#--动态操作DataTable

    C#动态操作DataTable(新增行.列.查询行.列等) 方法一:动态创建一个DataTable ,并为其添加数据 public void CreateTable()        {        ...

  4. AlertDialog通过反射机制阻止Dialog关闭

    在开发Android应用程序时,我们可能会用到需要用户输入的Dialog,如登录对话框等.这时候,如果用户没有输入登录信息而点击<确定>按钮时,我们并不希望登录Dialog消失,而是采用一 ...

  5. Opencv 张正友相机标定傻瓜教程

    注: 程序所用的OpenCV版本是 2.4.10 ,3.0以上的版本可能会有不同 先贴一下完整的工程代码: #include "opencv2/core/core.hpp" #in ...

  6. 3.RabbitMQ相关概念的杂谈

    1.vhost,为什么我要有这个vhost呢? 这是因为可能有很多组使用RabbitMQ,有产品组,用户组,vhost,虚拟主机的意思,可以避免命名冲突. 2.Exchange,交换机 有四种交换机 ...

  7. WPF 同一窗口内的多线程/多进程 UI(使用 SetParent 嵌入另一个窗口)

    原文 WPF 同一窗口内的多线程/多进程 UI(使用 SetParent 嵌入另一个窗口) WPF 的 UI 逻辑只在同一个线程中,这是学习 WPF 开发中大家几乎都会学习到的经验.如果希望做不同线程 ...

  8. 从贝叶斯模型(Bayes)到生成模型(Generative models)(生成式分类器,generative classifier)

    0. 基于贝叶斯公式的生成式分类器 生成式分类器(generative classifier)即是已知类别得样本: p(y=c|x,θ)∝p(x|y=c,θ)p(y=c|θ) p(x|y=c,θ) 称 ...

  9. 如何获得iframe中元素的值

    在Web开发时,很多时候会遇到一个问题.我在一个页面嵌入了iframe,并且我想获得这个iframe页面某个元素的值.那么该如何实现这个需求呢? 先来看下演示: 效果演示     iframe1中文本 ...

  10. virtualbox--在xp设置ubuntu虚拟机网络 主宿机能互通,宿机能通过主机上网Host-Only + Bridged

    由于最近要写一个简单的C/S模式的程序,刚好虚拟机装了个ubuntu12.04TLS,正好拿来当服务器测试,但是发现无法ping通.刚学习ubuntu,实在不熟,苦苦弄了2小时,才弄明白,在此记录一下 ...