leetcode single number系列

这个系列一共有三题,第一题是一组数里除了一个数出现一次之外,其他数都是成对出现,求这个数。

第二题是一组数里除了两个数出现一次外,其他数都是成对出现,求这两个数

第三题是一组数里除了一个数出现一次外,其他数都是出现三次,求这个数。

先说第一题,这题很简单,就是将所有的数全部异或一遍,由于两个相同的数异或之后得到0,所以最后得到的结果就是要求的那个数。

第二题有点难,

解题方法是,设所求的那两个数为n1,n2。那么n1和n2必然不相等,那么它们必然至少有一个bit的值不等。

那么与题1一样,将所有的数异或一遍,得到一个结果为n1^n2。利用这个数字求出n1与n2在哪个位置上不等,设为m。

然后将所有的数分成两组,一组为在位置m上为1的数,一组为在位置m上为0的数。那么n1,n2必然分在两个组。

这样题目就转换为了题1。

第三题的思路为

设要求的那个数为n,如果n在bit 0上为1,那么在bit 0上所有数的和必然不能被3整除,反之,必然可以被3整除。

以此类推算出所有位置上的值。

时间: 2024-08-28 19:15:12

leetcode single number系列的相关文章

leetCode: Single Number II [137]

[题目] 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: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

LeetCode: Single Number [136]

[题目] 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? [题意] 给定一个整数数组,其中除了一个数以外,其他数都是成对出现的,找出这

LeetCode——Single Number(II)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm shou

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description 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

LeetCode Single Number I / II / III

[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个相同的数异或为0,那么把这串数从头到尾异或起来,最后的数就是要求的那个数. 代码如下: class Solution { public: int singleNumber(vector<int>& nums) { int sum = 0; for(int i=0;i<nums.siz

LeetCode Single Number I II Python

Single Number Given an array of integers, every element appears twice except for one. Find that single one. def singleNumber(self, A): l = len(A) if l < 2: return A[0] A.sort() for i in range(0,l-1,2): if A[i] != A[i+1]: return A[i] return A[l-1] 思路:

leetcode Single Number II python

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. python code: class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): B={} for eachint in nums: if

leetcode Single Number python

Single Number Given an array of integers, every element appears twice except for one. Find that single one. python code: class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): B={} for i in nums: if i not in B: #建