[LeetCode]*137.Single Number II

简介:

【题目】

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?

【题意】

给定一个整数数组,每个元素出现了三次,除了一个。找出那个出现一次的数字。

注意:

你的算法应该一个线性时间复杂度完成。你能不能无需使用额外的内存来完成它

【思路一】

所有的数用二进制表示,我们把每个数的第i 位取和之后再对3取余,那么只会有两个结果 0 或 1 。  如果为0代表只出现一次的那个数第i位也为0,

如果为1表示只出现一次的那个数第i位也为1。因此取余的结果就是那个 “Single Number”。

下面是一个直接用大小为 32的数组来记录所有 位上的和。

【代码一】

/*---------------------------------------
*   日期:2015-04-26
*   作者:SJF0115
*   题目: 137.Single Number II
*   网址:https://leetcode.com/problems/single-number-ii/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int singleNumber(int A[], int n) {
        int count[32] = {0};
        int result = 0;
        for(int i = 0;i < 32;++i){
            for(int j = 0;j < n;++j){
                if ((A[j] >> i) & 1) {
                    ++count[i];
                }//if
            }//for
            result |= ((count[i] % 3) << i);
        }//for
        return result;
    }
};

int main(){
    Solution solution;
    int A[] = {2,3,4,2,5,2,3,3,5,5};
    int result = solution.singleNumber(A,10);
    // 输出
    cout<<result<<endl;
    return 0;
}

【思路二】

这个算法是有改进的空间的,可以使用掩码变量

方法 2:用 ones 记录到当前处理的元素为止,二进制 1 出现“1 次”(mod 3 之后的 1)的有哪些二进制位;

用 twos 记录到当前计算的变量为止,二进制 1 出现“2 次”(mod 3 之后的 2)的有哪些二进制位。

当 ones 和 twos 中的某一位同时为 1 时表示该二进制位上 1 出现了 3 次,此时需要清零。

即用二进制模拟三进制运算。最终 ones 记录的是最终结果。

【代码二】

/*---------------------------------------
*   日期:2015-04-26
*   作者:SJF0115
*   题目: 137.Single Number II
*   网址:https://leetcode.com/problems/single-number-ii/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int singleNumber(int A[], int n) {
        int ones = 0, twos = 0, threes = 0;
        for(int i = 0;i < n;++i){
            // 出现2次
            twos |= (ones & A[i]);
            // 出现1次
            ones ^= A[i];
            // 当ones和twos中的某一位同时为1时表示该二进制位上1出现了3次
            threes = ones & twos;
            // 二进制位上1出现了3次此时ones和twos对应位上清零
            ones &= ~threes;
            twos &= ~threes;
        }//for
        return ones;
    }
};

int main(){
    Solution solution;
    int A[] = {2,3,4,2,5,2,3,3,5,5};
    int result = solution.singleNumber(A,10);
    // 输出
    cout<<result<<endl;
    return 0;
}


目录
相关文章
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
173 1
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
145 0
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
96 0
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero

热门文章

最新文章

下一篇
日志分析软件