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

题意:一个数组中,只有一个元素出现一次,其他元素出现K次,(k为奇数3)

thinking:

(1)这道题适用于出现奇次的情形,解法是:对数组所有数的每一位出现1的次数进行统计,对K取余后,就为待求数在该位的位数(0或者1),

再将2进制转换为10进制即可

(2)这道题我假设int为32位,有些机器不是32位。

code:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        string s;
        int a=0x0001;
        int count=0;
        for(int i=0;i<32;i++)
        {
            for(int j=0;j<nums.size();j++)
            {
                if((nums[j]&a)!=0)
                    count++;
            }
            s.push_back('0'+count%3);
            a=a<<1;
            count=0;
        }
        return reverse_string_to_int(s);
    }
protected:
    int reverse_string_to_int(string s)
    {
        int a=1;
        int ret=0;
        for(int i=0;i<s.size();i++)
        {
            ret+=(s.at(i)-'0')*a;
            a*=2;
        }
        return ret;
    }
};
时间: 2024-10-18 07:35:05

leetcode || 137、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笔记:Single Number II

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

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

#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——1、Single Number

这道题目的意思大概是这样: 给我一个整型数组,里面的元素都出现两次,但是有一个元素只出现一次,你要把这个只出现一次的元素给找出来.并且要求算法的时间复杂度为线性,即O(N). 一开始我思考了很久,始终没有找到方法.若是不限定元素类型为整型,那么根本不可能在线性时间内找到这个只出现一次的元素.所以我想突破点应该在于元素类型为整型. 为什么整型就可以做到在线性时间内得出结果呢?我点开了该题目所属的标签,[Bit Manipulation]——位操作.好像突然间抓住了什么,位操作是个很神奇的东西,它的

leetcode || 136、Single Number

problem: Given an array of integers, every element appears twice 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 Hash Table Bit Manipulat

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 136、137:Single Number I &amp; II

Single Number Given an array of integers, every element appears twice 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 Given an arr