zoukankan      html  css  js  c++  java
  • 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 };
  • 相关阅读:
    批量 kill mysql 中运行时间长的sql
    第三方支付过程中session失效问题
    Maven常用命令:
    根据Request获取客户端IP
    MySQL之alter语句用法总结
    转:Redis使用认证密码登录
    Git添加远程库
    输入一个三位数x,它的个位数为a,十位数为b,百位数为c,请把它的各个位数分离出来并输出。
    20161129 计算95除以55,商是多少,余数是多少?(商和余数要求分两行显示)
    20161128 已知小明同学其中考试成绩,数学97.5分,语文94.5分,英语95.5分,求小明同学的三科总成绩(结果保留一位小数)。
  • 原文地址:https://www.cnblogs.com/wangxiaobao/p/6230895.html
Copyright © 2011-2022 走看看