136. Single Number && 137. Single Number II && 260. Single Number III

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?

Subscribe to see which companies asked this question

Hide Tags

Hash Table Bit Manipulation

Hide Similar Problems

(M) Single Number II (M) Single Number III (M) Missing Number (H) Find the Duplicate Number

public class Solution {
    public int singleNumber(int[] nums) {
        int xor = 0;
        for(int n : nums)
            xor ^= n;
        return xor;
    }
}

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?

Subscribe to see which companies asked this question

Hide Tags

Bit Manipulation

Hide Similar Problems

(M) Single Number (M) Single Number III

...

260. 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?

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Tags

Bit Manipulation

Hide Similar Problems

(M) Single Number (M) Single Number II

public class Solution {
    public int[] singleNumber(int[] nums) {
        // Pass 1 :
        // Get the XOR of the two numbers we need to find
        int diff = 0;
        for (int num : nums) {
            diff ^= num;
        }
        // Get the rightmost different bit between these two numbers.
        diff &= -diff;

        // Pass 2 :
        int[] rets = {0, 0}; //split the two numbers we are looking for into two groups.
        for (int num : nums)
        {
            if ((num & diff) == 0) // the group that has rightmost bit unset.
            {
                rets[0] ^= num;
            }
            else // the group that has rightmost bit set.
            {
                rets[1] ^= num;
            }
        }
        return rets;
    }
}
时间: 2024-11-02 14:54:58

136. Single Number && 137. Single Number II && 260. Single Number III的相关文章

136.137.260. Single Number && 位运算

136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) 再说一下异或的性质,满足交换律和结合律 因此: 对于任意一个数n n ^ 0 = n n ^ n = 0 对于这道题来说,所有数依次异或剩下的就是那个数了 1 class Solution(object): 2 def singleNumber(self, nums): 3 ans = 0 4 for i

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

[LeetCode] 260. Single Number III 单独数 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. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One 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 o

执行tsung时报"Maximum number of concurrent users in a single VM reached

[[email protected] ~]# tsung  -f jabber_register.xml startStarting Tsung"Log directory is: /root/.tsung/log/20141122-0639""Maximum number of concurrent users in a single VM reached and 'use_controller_vm' is true, can't start new beam !!! C

leetcode 375. Guess Number Higher or Lower II

传送门 375. Guess Number Higher or Lower II QuestionEditorial Solution My Submissions Total Accepted: 1546 Total Submissions: 5408 Difficulty: Medium We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess w

375. Guess Number Higher or Lower II (Python)

375. Guess Number Higher or Lower II Description We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is hig

LC 375. Guess Number Higher or Lower II

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is higher or lower. However, when you guess a particula

'Invalid update: invalid number of rows in section xx. The number of rows contained in an existing section after the update (xxx)...

'Invalid update: invalid number of rows in section 5.  The number of rows contained in an existing section after the update (299) must be equal to the number of rows contained in that section before the update (276), plus or minus the number of rows