[LeetCode] Single Number III

Given an array of numbers 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:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

解题思路

位运算。与Single Number的区别在于首先根据所有元素的亦或结果的某一位是否为1进行分组,然后分别找出每组中的Single Number。

实现代码

Java:

// Runtime: 2 ms
public class Solution {
    public int[] singleNumber(int[] nums) {
        int xor = 0;
        for (int num : nums) {
            xor ^= num;
        }
        int bit = xor & (~(xor - 1));

        int single[] = new int[2];
        for (int num : nums) {
            if ((num & bit) != 0) {
                single[0] ^= num;
            } else {
                single[1] ^= num;
            }
        }

        return single;
    }
}
时间: 2024-07-28 18:22:35

[LeetCode] Single Number III的相关文章

[LeetCode] Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解题思路 考虑全部用二进制表示,如果我们把 第 ith 个位置上所有数字的

[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? 解题思路 亦或运算. 实现代码 // Runtime: 1 ms public cla

[LeetCode]*137.Single Number II

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数数组,每个元素出现了三次,除了一个.找出那

[LeetCode]--136. 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? 耐心看下我的思想演变过程吧,可能是大脑缺氧,用List装下标. public int

[LeetCode] Course Schedule III 课程清单之三

There are n different online courses numbered from 1 to n. Each course has some duration(course length) tand closed on dth day. A course should be taken continuously for t days and must be finished before or on the dth day. You will start at the 1st 

[LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 Output: True Explanation: The binary representation of 5 is: 101 Example 2: Input: 7 Output: False Ex

[LeetCode]191.Number of 1 Bits

题目 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should r

[LeetCode]200.Number of Islands

题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded

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? 翻译:给定一个整数数组,数组中所有元素都出现了两次,只有一个元素只出现