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. 使用CentOS7卸载自带jdk安装自己的JDK1.8

    不管在什么地方,什么时候,学习是快速提升自己的能力的一种体现!!!!!!!!!!! 关于JDK1.8 与之前的版本相比有哪些变化和新特性我也不在这详细的说明了,毕竟一度娘啥都有了,既然不多说那就直接开 ...

  2. jQuery树形菜单,使用zTree插件,异步载入 & 编辑功能&Check 共存

    一.下载zTree插件 地址:http://www.ztree.me 二.HTML代码 <%@ Page Language="C#" AutoEventWireup=&quo ...

  3. uitableview顶部多出20距离, UIScollView顶部多出64距离

    self.automaticallyAdjustsScrollViewInsets = NO;看 这个UIViewController的这个属性你就明白了,此属性默认为YES,这样UIViewCont ...

  4. Java数据类型转换问题

    基本数据类型 整数型 byte --- 字节型 --- 1个字节 --- -27~27-1 -> -128~127 byte b1 = 25; byte b2 = 127; short --- ...

  5. gcc安装教程(学习Linux编程只需安装cygwin)

    gcc安装教程(windows版本) —最好的C/C++编译器,没有之一 windows下的gcc有两个版本mingw和cygwin,二选一就好 已学会C++,做工程,建议用mingw 用cygwin ...

  6. 从Client应用场景介绍IdentityServer4(三)

    原文:从Client应用场景介绍IdentityServer4(三) 在学习其他应用场景前,需要了解几个客户端的授权模式.首先了解下本节使用的几个名词 Resource Owner:资源拥有者,文中称 ...

  7. UVALive 6531 Go up the ultras 单调栈+RMQ

    题目链接:点击打开链接 题意: 给定n座山 以下n个数字表示n座山的高度 若这座山u合法,则要满足: 1.若u的左边存在比u高的山,设v是u左边距离u近期的且严格比u高的山,在[v,u]之间至少有一座 ...

  8. Matlab Tricks(二十九) —— 使用 deal 将多个输入赋值给多个输出

    deal:Distribute inputs to outputs: >> [id, name, data] = deal(123, 'zhang', randn(3)) 注意: [Y1, ...

  9. STL优先级队列

    priority_queue 这是一个优先级队列的所有权值概念单向队列queue.在这个队列中.全部元素是按优先级排列的(也能够觉得queue是个按进入队列的先后做为优先级的优先级队列--先进入队列的 ...

  10. ISO9000 质量管理和质量保证系列国际标准

    (一)ISO9000认证的背景国际标准化组织(ISO)是世界上最主要的非政府间国际标准化机构,成立于二次世界大战以后,总部位于瑞士日内瓦.该组织的目的是在世界范围内促进标准化及有关工作的发展,以利于国 ...