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

Posted wangxiaobao的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 };

 



以上是关于LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode-136 Single Number

LeetCode OJ 136Single Number

LeetCode 136 Single Number

LeetCode 136. Single Number

LeetCode 136. Single Number

[leetcode]136.Single Number