异或巧用:Single Number
异或巧用: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的更多相关文章
- Single Number 普通解及最小空间解(理解异或)
原题目 Given a non-empty array of integers, every element appears twice except for one. Find that singl ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- LeetCode 【Single Number I II III】
Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...
- 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 ...
- Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
随机推荐
- Hibernate中tx.commit()
hibernate.cfg,xml文件中的自动提交事务是false.主键生成策略是native. 在表的映射继承是手动提交事务(即:tx.commit())无法发出sql语句,把数据插入到数据库的表中 ...
- 数据库课程设计 PHP web实现
纪念一下自己写的东西.. 都说很垃圾就是了 直接用XAMPP做的 菜鸟网上学的PHP和HTML <!DOCTYPE html> <html> <head> < ...
- ES6 第三章 变量的解构赋值 具体参照http://es6.ruanyifeng.com
1.基本用法 let [a, b, c] = [1, 2, 3];左右两边解构格式要保持一致. 2.默认值 let [x, y = 'b'] = ['a']; // x='a', y='b' let ...
- ibatis经验
1.insert,update,delete 返回值(1).insert 返回的为插入的主键值,但必须在配置文件中加入<selectKey/> 如果主键值为String<sele ...
- TCP三次握手简单理解
- Python中的函数(3)
一.包含返回值的函数 下面来看一个函数,它接收名和姓并返回完整的姓名: def get_formatted_name(first_name,last_name): """ ...
- LeetCode(71) Simplify Path
题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&quo ...
- 一道在CF上WA了9次才AC的A题题目与10个版本的代码代码
题目(题目链接:https://codeforces.com/problemset/problem/733/A): A. Grasshopper And the String time limit ...
- asp.net mvc数据验证
文章:asp.net mvc3 的数据验证(一) 文章:ASP.NET MVC下的四种验证编程方式 这个讲了在一个地方展示验证信息 文章:[ASP.NET MVC系列]浅谈数据注解和验证 这个在每个输 ...
- [codeforces722C]Destroying Array
[codeforces722C]Destroying Array 试题描述 You are given an array consisting of n non-negative integers a ...