【LeetCode从零单刷】Single Number II

题目:

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

解答:

如果是挑出非偶数次数的元素,可以利用每个元素依次异或的方法。但是非奇数次数(3,5,7,9……)的元素?

如果要快,可以hash_map存储。然后遍历一遍hash_map找到Single Number。

换种思路,按找不同的Single Number对应的位。细节上:

  1. bit = 1 << i; 然后利用当前数与 bit 位与,根据结果判断当前数的第 i 位是否有 1;
  2. 将最终的结果第 i 位置为 1,可以位或操作:ans | bit.
class Solution {
public:
    int singleNumber(int A[], int n) {
        int bit, bitsum;
        int ans;
        for(int i = 0; i < 32; i++)
        {
            bit = 1 << i;
            bitsum = 0;
            for(int j = 0; j < n; j++)
            {
                if(bit & A[j]) bitsum++;
            }

            if(bitsum % 3)  ans = ans | bit;
        }
        return ans;
    }
};
时间: 2024-11-21 05:09:37

【LeetCode从零单刷】Single Number II的相关文章

【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值之后.得到答案后每一位除

leetcode文章137称号-Single Number II

#include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int count[32]={0}; int i,j,number=0; for(i=0;i<numsSize;i++) { for(j=0;j<32;j++) count[j]+=((nums[i]&(1<<j))!=0); } for(i=0;i<32;i++) { if(coun

【LeetCode从零单刷】N-Queens II

题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 解答: 思路很简单,就是暴力求解的N皇后问题计数.过程如下: 如果第 i 行的第 j 列放着皇后,然后放第 (i+1) 行的皇后使其不矛盾,然后第 (i+2) 行--: 如果每一列都不可行,那我们就回溯一行,然后继续第 1 步直至成功(转3)

【LeetCode从零单刷】Number of 1 Bits

题目: Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).  For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should

leetcode第137题-Single Number II

#include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int count[32]={0}; int i,j,number=0; for(i=0;i<numsSize;i++) { for(j=0;j<32;j++) count[j]+=((nums[i]&(1<<j))!=0); } for(i=0;i<32;i++) { if(coun

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][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: Single Number II [137]

[题目] 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? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

leetcode 刷题之路 84 Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 思路,根据数组构成的特点可知,数组中所有数的某一位上1的个数总和为3*n或者3*n+1. 当只出现一次的整数该位为0时,总和为3*n,当只出现一次的整数该位为1时,总和为3*n+1. 因此我们可以计算数

leetcode Single Number II python

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. python code: class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): B={} for eachint in nums: if