leetcode笔记:Single Number II

一.题目描述

二.解题思路

这道题与Single Number(数组中其他数出现两次,仅有一个出现一次的)有所不同,本题变为序列中有一个数出现一次,其他元素出现了三次,同样要求时间复杂度为线性,空间复杂度为常数。事实上,该算法仍可以借助位运算来实现。

首先需要确定int类型数据的长度:intWidth = sizeof(int) * 8,可以用intWidth大小的变量来存储数组中每个元素的各个二进制位上1 出现的次数,最后 在进行 模3 操作,如果为1,那说明这一位是要找元素二进制表示中为 1 的那一位。

一个例子

以下有一组序列,写出每个数的二进制形式:

13:1 1 0 1
13:1 1 0 1
13:1 1 0 1
8: 1 0 0 0
9: 1 0 0 1
9: 1 0 0 1
9: 1 0 0 1

统计每一位二进制位上 1 出现的次数,由高位到低位依次为:7 3 0 6 ,最后对7 3 0 6 中各元素进行模3(%3),得到:1 0 0 0 ,即为出现一次的数的二进制表示,返回该值即可。实际上对于该命题的扩展,即:若存在一序列,除了一个数只出现一次,其他数均出现k次的情况下,同样可使用以上方法,对每一位二进制位上 1 出现的次数进行统计,最后进行模k(%k)即得到目标值。

三.示例代码

// 时间复杂度O(n),空间复杂度O(1)
class Solution
{
public:
    int findSingleNumber(int A[], int n)
    {
        const int intWidth = sizeof(int) * 8;
        int bitNum[intWidth] = { 0 };  // initialize
        int res = 0;
        for (int i = 0; i < intWidth; i++){
            for (int j = 0; j < n; j++){
                bitNum[i] += (A[j] >> i) & 1;
            }
            res |= (bitNum[i] % 3) << i;
        }
        return res;
    }
};

四.总结

阅读了网上其他博文,发现事实上有更为高效的方法,其基本思路是利用三个变量分别保存各个二进制位上 1 出现一次、两次、三次的分布情况,最后只需返回第一个变量就行了。这种方法我本人并没有实现,日后再继续研究。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 05:11:12

leetcode笔记:Single Number II的相关文章

[LeetCode][JavaScript]Single Number II

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? https://leetcode.com/

Leetcode 137 Single Number II 仅出现一次的数字

原题地址https://leetcode.com/problems/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 co

LeetCode 137 Single Number II(只出现一次的数字 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 im

【LeetCode】Single Number II (3 solutions)

Single Number II Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:开辟map记录次数 class So

LeetCode 137 Single Number II(仅仅出现一次的数字 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

Leetcode 137. Single Number II JAVA语言

Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 题意:一个数组中除

【LeetCode】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? 思路: 用 ones, twos, threes 分别记录数字在二

Java [Leetcode 137]Single Number II

题目描述: Given an array of integers, every element appears three times except for one. Find that single one. 解题思路: 具体参考Detailed explanation and generalization of the bitwise operation method for single numbers 讲的实在是很全面,而且拓展到了一般的情况,膜!! 代码如下: public class

[LeetCode] 137. Single Number II (位运算)

传送门 Description Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra me

Java for 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? 解题思路一: 一个int的长度是32,因此可以开一个长度为32的数组,表示