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?

分析:

第一问属于技巧题,做过就会,没做过很难想。考虑异或操作,相同数异或之后为0, 0与一个数异或还是这个数本身,且异或操作满足交换律和结合律。

所以这个题将所有的数异或起来,就能得到那个落单的数。

代码:

 1 class Solution {
 2 public:
 3     int singleNumber(vector<int>& nums) {
 4         int result = 0;
 5         for (int i = 0; i < nums.size(); ++i) {
 6             result ^= nums[i];
 7         }
 8         return result;
 9     }
10 };

137. Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. (Medium)

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

分析:

除了“单身狗”以外其他元素均出现了三次,考虑上一题中的异或的思想(不进位加法,两个1之后就变成0),推广到三个数,就是有一位1如果出现三遍,就应该抵消为0;

所以可以考虑建立一个32个元素数组表示int的各个位置,然后把每个数的每一位对应加进去,mod 3后的结果恢复成一个数即为结果。

代码:

 1 class Solution {
 2 public:
 3     int singleNumber(vector<int>& nums) {
 4         int bitArray[32];
 5         memset(bitArray, 0, sizeof(bitArray));
 6         int result = 0;
 7         for (int i = 0; i < 32; ++i) {
 8             for (int j = 0; j < nums.size(); ++j) {
 9                 bitArray[i] += (nums[j] >> i & 1);
10             }
11             bitArray[i] %= 3;
12             result |= (bitArray[i] << i);
13         }
14         return result;
15     }
16 };

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. (Medium)

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 number1的问题。

利用1中的思路,先把所有的数异或,最后可以得到一个数。可以知道这个数(xorResult)是那两个单身狗异或的结果,但是我们无法从这个数恢复两个数本身。

但是我们可以xorResult中第一个1出现的位置,这个位置是1说明之前两个数在这一位不同(一个0,一个1);

于是我们可以把原数组中的元素这一位是0还是1分为两个数组,这两个数组便都是single number1的问题,最后把两个结果添加到vector返回。

代码:

 1 class Solution {
 2 public:
 3     vector<int> singleNumber(vector<int>& nums) {
 4         int xorResult = 0;
 5         for (int i = 0; i < nums.size(); ++i) {
 6             xorResult ^= nums[i];
 7         }
 8         int lastBitofOne = xorResult - (xorResult & (xorResult - 1) );
 9         int result1 = 0, result2 = 0;
10         for (int i = 0; i < nums.size(); ++i) {
11             if ( (nums[i] & lastBitofOne) == 0 ) {
12                 result1 ^= nums[i];
13             }
14             else {
15                 result2 ^= nums[i];
16             }
17         }
18         vector<int> result{result1, result2};
19         return result;
20     }
21 };
时间: 2024-10-25 23:39:08

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III的相关文章

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 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 co

执行tsung时报&quot;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

&#39;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

Find the smallest number whose digits multiply to a given number n

Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ‘n’. The result ‘p’ should have minimum two digits. Examples: Input: n = 36 Output: p = 49 // Note that 4*9 = 36 and 49 is the smallest such number In

hdu1212 Big Number &amp;第六届山东省赛Single Round Math (同余定理,大数取模)

题目链接:Big Number 题目大意:每次输入两个数,第一个是高精度,第二个数小于100000:求 a mod b 根据同余定理: (a+b)% c = (a%c+ b%c)%c (a*b)%c =  ( a%c* b%c)%c 所以 对于大数,例如 :123 可以这样分解 123 =  (1*10+2)*10 + 3: 123 % c =   (  (  (  1%c *  10%c)%c + 2%c) %c  * 10%c) + 3 %c  ) %c; 因此,我们用字符串处理这个数,通过

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