异或巧用:Single Number

今天刷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.

异或特性:

(1)顺序无关:即若有多个元素相异或,则异或元素能够任意交换顺序。不会影响结果

(2)对同样的数异或两次等于没有异或:即相当于+x和-x

故,依据异或特性,从逻辑上能够觉得是数组中同样元素先各自异或。结果为0,而终于剩下的那个元素即为出现一次的元素。Java代码例如以下:

</pre><pre class="java" name="code">public class Solution {
public int singleNumber(int[] nums) {
int res = 0;
for(int i:nums) {
res ^= i;
}
return res;
}
}

另附一道相同应用异或运算解决的题目(面试时可能会遇到):

给定1-1000个连续自然数,然后从中任意去掉两个,再打乱顺序。

要求仅仅遍历一次,求出被去掉的两个数(设为x和y)。

步骤:

(1)计算x^y:现将打乱前和打乱后的两个数组(记:数组1和数组2)的全部元素做异或运算,反复的元素会互相抵消,所得终于结果即为x^y

(2)获取x^y中1所在位置并划分。继续异或:因为x和y是不同的整数,所以这两个数的异或结果。转化为二进制的话,至少有一位是1,如果在第3位。

把数组1按第3位是否为0进行划分,划分为两个数组。每一个数组各包括一个被抽取的数。

把数组2也按这个规则划分为两个数组。这样就得到了4个数组。当中两组是第3位为0,另外两组是第3位为1。

把第3位为0的两个数组进行异或就能得到被抽取的一个事。同理把第3位为1的两个数组异或就能得到另外一个被抽取的数

异或巧用:Single Number的更多相关文章

  1. Single Number 普通解及最小空间解(理解异或)

    原题目 Given a non-empty array of integers, every element appears twice except for one. Find that singl ...

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

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

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

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

  4. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

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

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

  6. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  7. LeetCode 【Single Number I II III】

    Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...

  8. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

  9. Single Number II

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

随机推荐

  1. Microsoft Windows Server

    Microsoft Windows Server Microsoft Windows Microsoft Windows 是微软推出的个人版操作系统: Microsoft Windows Server ...

  2. Omnidirectional DSO: Direct Sparse Odometry with Fisheye Cameras 论文摘要

    1. Abstract 通过一种Unified Omnidirectional Model作为投影方程. 这种方式可以使用图像的所有内容包括有强畸变的区域,而现存的视觉里程计方案只能修正或者切掉来使用 ...

  3. 洛谷——P3939 数颜色(暴力vecotr+二分)

    P3939 数颜色 $vecotr$里二分就是好用,全是$STL$ 颜色数目比较少,可以对每一种颜色弄一个$vector$记录一下,查找$l,r$内颜色数为$x$的兔子数,直接在$G[x]$这个$ve ...

  4. IOI2008 Island 岛屿

    题目描述: bz luogu 题解: 裸的基环树直径. 代码: #include<queue> #include<cstdio> #include<cstring> ...

  5. 删除mysql中user为空用户,mysql空密码

    进入mysql数据库 mysql -uroot -p 查看用户信息 select user,host ,Password from mysql.user; 如下图: 删除user为空用户 delete ...

  6. 第三讲:post-processsing with vcs+ files

    1,dump wave  by system function $vcdpluson(level_number,module_instance,....|net_or_reg) $vcdplusoff ...

  7. Couchbase第一印象(架构特性)

    Couchbase第一印象(架构特性) 面向文档 保存的字节流总有一个 DOCUMENT ID(Object_ID) 高并发性,高灵活性,高拓展性,容错性好 面向文档的集群存储系统 每个文档用一个唯一 ...

  8. 大数据学习——采集目录到HDFS

    采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去 根据需求,首先定义以下3大要素 l  采集源,即source——监控文件目录 :  spoold ...

  9. 表连接sql执行计划学习

    循环嵌套连接(Nested Loop Join) 合并连接(Merge Join) 哈西匹配(Hash Join) 文章:浅谈SQL Server中的三种物理连接操作 循环嵌套,如果内循环列上有索引, ...

  10. Codeforces474E - Pillars

    Portal Description 给出一个\(n(n\leq10^5)\)的正整数序列\(\{a_n\}(a_i\leq10^{15})\)和正整数\(d(d\leq10^9)\),求\(\{a_ ...