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?

题意:给定一个整型数组,有n个整数,除了一个只出现过一次,其他的都出现了2次!找出只出现一次的整数。

要求:算法是线性复杂度,无需使用额外的内存。

解题:

  用二进制位的方式来思考。按位异或运算将两个运算分量的对应位进行异或,即相应位的值相同的,结果为 0,不相同的结果为 1。

  例如:2^4=6(010^100 =110).

  故只需对数组元素进行异或,出现两次的元素异或结果为0,0与只出现一次的元素异或结果即为答案。

  即:ans = 0;

  ans = A[0]^A[1]^A[2]....^A[i](The single one) ^....A[n-1];

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

C++中的位运算

  C++中位运算的运算分量只能是整型或字符型数据,位运算把运算对象看作是由二进位组成的位串信息,按位完成指定的运算,得到位串信息的结果。

  位运算符有:&(按位与)、|(按位或)、^(按位异或)、~ (按位取反)。

  其中,按位取反运算符是单目运算符,其余均为双目运算符。位运算符的优先级从高到低,依次为~、&、^、|,其中~的结合方向自右至左,且优先级高于算术运算符,其余运算符的结合方向都是自左至右,且优先级低于关系运算符。

 

Single Number-C++中的异或的更多相关文章

  1. 136. Single Number【LeetCode】异或运算符,算法,java

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

  2. Single Number 数组中除了某个元素出现一次,其他都出现两次,找出这个元素

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

  3. LeetCode 136 Single Number 数组中除一个数外其他数都出现两次,找出只出现一次的数

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

  4. 136 Single Number 数组中除一个数外其他数都出现两次,找出只出现一次的数

    给定一个整数数组,除了某个元素外其余元素均出现两次.请找出这个只出现一次的元素.备注:你的算法应该是一个线性时间复杂度. 你可以不用额外空间来实现它吗? 详见:https://leetcode.com ...

  5. 【题解】【位操作】【Leetcode】Single Number II

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

  6. Leetcode 137 Single Number II 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...

  7. 【Leetcode】【Medium】Single Number II

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

  8. LeetCode——Single Number II(找出数组中只出现一次的数2)

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

  9. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

随机推荐

  1. [示例]NSDictionary编程题-字典的排序应用(iOS5班)

    代码? #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepo ...

  2. JQuery判断checkbox是否选中-批量

    在html的checkbox里,选中的话会有属性checked="checked". 如果用一个checkbox被选中,alert这个checkbox的属性"checke ...

  3. Oracle合并函数内容

    --MINUS去差集,取第一个集合有的而第二集合没有的,并以第一个字段排序select t.bumenbm from T_HQ_BM t minus select b.bumenbm from t_h ...

  4. C# DES加密

    需要引用名称空间 using System; using System.Text; using System.Security.Cryptography; using System.IO; 具体代码: ...

  5. C++-const_cast只能用于指针和引用,对象的const到非const可以用static_cast

    Static_cast可以对对象也可以对指针也可以对引用,但是const_cast只可以对指针和引用使用,后者不可以对对象用,如果你要把一个const值转化为非const值只能用隐式执行或通过使用st ...

  6. root运行chrome

    os:centos7 edit file : /usr/bin/google-chrome Add "--user-data-dir" (without the quotes) a ...

  7. 更新安装xcode7插件

    mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-inscurl -fsSL https://raw.github ...

  8. HTML--5 JavaScript

    一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司 ...

  9. 使用开源工具MonoDevelop开发GTK#图形界面

    转自:http://developer.51cto.com/art/201011/235040.htm Mono一直到现在的2.8已经完全可以胜任一些比较小的项目了,但相关的开发文档与教程一直比较匮乏 ...

  10. (转)html5开发之viewport使用

    原文:http://www.php100.com/html/webkaifa/HTML5/2012/0831/10979.html 随着高端手机(Andriod,Iphone,Ipod,WinPhon ...