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)app

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

分析:code

第一问属于技巧题,作过就会,没作过很难想。考虑异或操做,相同数异或以后为0, 0与一个数异或仍是这个数自己,且异或操做知足交换律和结合律。blog

因此这个题将全部的数异或起来,就能获得那个落单的数。three

代码:element

 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 IIit

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

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

 

分析:

除了“单身狗”之外其余元素均出现了三次,考虑上一题中的异或的思想(不进位加法,两个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 };
相关文章
相关标签/搜索