【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?

思路: 用一个32位的数组存每一位bit值之后。得到答案后每一位除三取余即为答案。

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

我本觉得

res |= (bitnum[i]%3)<<i;

可以用

res =res+ (bitnum[i]%3)<<i;

代替,

或者是

bitnum[i] += (A[j]>>i)&1;

可以用

bitnum[i] = bitnum[i]+ (A[j]>>i)&1;

代替

不过全部报错

【Leetcode长征系列】Single Number II

时间: 2024-10-01 04:05:29

【Leetcode长征系列】Single Number II的相关文章

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? Single Number II 比Single Number要复杂的多,

LeetCode 笔记26 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? 没辙,智商碾压题.楼主没遇到之前就只会这种做法. public int si

【一天一道LeetCode】#137. Single Number II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 complex

leetcode笔记:Single Number II

一.题目描述 二.解题思路 这道题与Single Number(数组中其他数出现两次,仅有一个出现一次的)有所不同,本题变为序列中有一个数出现一次,其他元素出现了三次,同样要求时间复杂度为线性,空间复杂度为常数.事实上,该算法仍可以借助位运算来实现. 首先需要确定int类型数据的长度:intWidth = sizeof(int) * 8,可以用intWidth大小的变量来存储数组中每个元素的各个二进制位上1 出现的次数,最后 在进行 模3 操作,如果为1,那说明这一位是要找元素二进制表示中为 1

【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? 题解: The code seems tricky and hard

leetcode || 137、Single Number II

problem: 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? Hide Tags Bit Manipulation 题

【一天一道LeetCode】#260. Single Number III

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 a

LeetCode OJ 之 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. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The or

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