260. Single Number III (找到数组中仅出现一次的两个数)
2016-11-10 10:08
429 查看
Given an array of numbers
the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given
Note:
The order of the result is not important. So in the above example,
also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
题目大意:一个整型数组中除了某两个数以外,其它的数都出现了两次,找出这两个数。
题目思路:
解法1:HashSet,遍历数组,把每个元素都放入hashset中,如果hashset中已经存在这个元素,则将这个元素移除,最后hashset中剩余的两个数就是要找的那两个数。代码如下:(10ms,beats 21.25%)
解法二:题目要求只使用常数数量级的额外存储空间,则不使用解法1。可以使用位运算,计算数组中所有元素的异或值xor,由于数组中除了某两个数a、b,其他数都出现了两次,那么xor其实就为 a^b 。现在计算lowest = xor & (-xor),lowest的含义是从最低位开始取 xor 二进制表示中第一个‘1’所对应的值,假设xor = a^b = 12(即1100),则lowest = 4(即0100),那么说明在 从最低位开始 的第三位上,a和b对应的值一定不相同。那么a&lowest != b&lowest。那么解法就是:令a=b=0,对于数组中的每一个数num,如果num&lowest==0,则a^=num,否则b^=num。最后得到的a、b就是要求的那两个数。代码如下:(1ms,beats
78.47%)
nums, in which exactly two elements appear only once and all
the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given
nums = [1, 2, 1, 3, 2, 5], return
[3, 5].
Note:
The order of the result is not important. So in the above example,
[5, 3]is
also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
题目大意:一个整型数组中除了某两个数以外,其它的数都出现了两次,找出这两个数。
题目思路:
解法1:HashSet,遍历数组,把每个元素都放入hashset中,如果hashset中已经存在这个元素,则将这个元素移除,最后hashset中剩余的两个数就是要找的那两个数。代码如下:(10ms,beats 21.25%)
public int[] singleNumber(int[] nums) { Set<Integer> set = new HashSet<>(); int[] res = new int[2]; int index = 0; for (int num : nums) { if (!set.add(num)) set.remove(num); } Iterator<Integer> it = set.iterator(); while (it.hasNext()) res[index++] = it.next(); return res; }
解法二:题目要求只使用常数数量级的额外存储空间,则不使用解法1。可以使用位运算,计算数组中所有元素的异或值xor,由于数组中除了某两个数a、b,其他数都出现了两次,那么xor其实就为 a^b 。现在计算lowest = xor & (-xor),lowest的含义是从最低位开始取 xor 二进制表示中第一个‘1’所对应的值,假设xor = a^b = 12(即1100),则lowest = 4(即0100),那么说明在 从最低位开始 的第三位上,a和b对应的值一定不相同。那么a&lowest != b&lowest。那么解法就是:令a=b=0,对于数组中的每一个数num,如果num&lowest==0,则a^=num,否则b^=num。最后得到的a、b就是要求的那两个数。代码如下:(1ms,beats
78.47%)
public int[] singleNumber(int[] nums) { int[] res = new int[2]; int xor = 0, lowest, a = 0, b = 0; for (int num : nums) xor ^= num; lowest = xor & (-xor); for (int num : nums) { if ((num & lowest) == 0) a ^= num; else b ^= num; } res[0] = a; res[1] = b; return res; }
相关文章推荐
- leetcode 260. Single Number III 神奇的位操作 + 找到两个只出现一次的数
- LeetCode 260 Single Number III(数组中只出现一次的数字3)
- Single Number III & 数组中只出现一次的数字
- 位运算-Single Number III(只有两个不同的数字出现了一次,其余出现了两次,找出这两个数)
- 【leetcode】在一堆每个数字都出现三次的数组中,找到那个只出现一次的数(Single Number II)
- LeetCode——Single Number II(找出数组中只出现一次的数2)
- 从数组中找到只出现一次的两个数
- Single Number II -- 找出数组中唯一出现一次的数,其它数都出现了三次
- Leetcode 260 Single Number III 仅出现一次的数字
- LeetCode 260. Single Number III (数组查重)
- 【leetcode】数组中找出只出现一次的数字(Single Number)
- 位运算-Single Number II(给定一个数组,除了一个数字出现一次,其他都出现三次,求出现一次的数)
- single-number、single-number2,数组中只出现一次的数字
- leetcode 260. Single Number III --> 用hashmap计算出现的次数,注意containsKey获取key,用 get 方法获取值
- [Leetcode] 260. Single Number III 解题报告
- 260. Single Number III
- LeetCode 260. Single Number III
- 在其他数都出现k次的数组中找到只出现一次的数
- 260. Single Number III
- leetcode 260. Single Number III-寻找单身狗|位运算