Single Number问题

LeetCode上面有这样两道Single Number 问题:

1、假设一个整型数组中所有的元素都出现了次,唯独只有一个元素出现了一次,求出这个出现一次的元素。

2、假设一个整型数组中所有的元素都出现了次,唯独只有一个元素出现了一次,求出这个出现一次的元素。

显然,两个问题唯一的不同就是大部分元素是出现了两次还是三次。

对于问题1:很好解决,通过异或运算,我们可以把所有出现两次的元素消除 a^a = 0; 最后就只剩下那个出现一次的元素了 0^b = b。

int singleNumber(int A[], int n)
{
    int result=0;
    for(int i=0; i<n; i++)
    {
        result ^= A[i];
    }
    return result;
}

对于问题2:显然无法继续用异或来解决了,我们可以通过位运算的特点来解决。一个数从二进制的角度来看,无非就是0和1,若是我们只从各个位来看,就把这一位的内容加起来,取模3,得到的这个数就是在这一位上的值。有了单独这个数在各个位的值,这一个剩下的数也就出来了。这样来看,我们需要一个大小为32的数组来保存这个数,这个空间并不会随着数组n值的增加而变化,所以从空间角度来看是没有问题的。

int singleNumber(int A[], int n)
{
    int a[32] = {0};
    int result=0;

    for(int i=0; i<32; i++)
    {
        for(int j=0; j<n; j++)
        {
            if((A[j]>>i)&0x1 == 1) a[i]++;
        }
        result |= ((a[i]%3)<<i);
    }
    return result;
}

当然关于这个问题,在 点击打开链接 这里还给出了另一种更简便的解法。

时间: 2024-10-24 23:09:01

Single Number问题的相关文章

Configuring Single Number Reach on Cisco CUCM 7.1.3

Cisco CUCM allows you to configure single number reach (also known as mobility) so that a call destined for your desk phone's extension can ring on a number of other devices such as your cellphone, blackberry, home phone, etc.  This feature also allo

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One 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 o

Single Number II

题目描写叙述 链接地址 解法 算法解释 题目描写叙述 Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Example Given [1,1,2,3,3,3,2,2,4,1] return 4 Challenge One-pass, constant extra space. 链接地址 http://www.lintcode.com/en/problem/single-number-ii/

136.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? 思路: 一开始自己的思路非常简单,首先设置一个status的变量,然后通过2层for循环

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description 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

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

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

137.Single Number II(法1排序法2STL容器map哈希法3位运算法4改进的位运算)

Given an array of integers, every element appears three timesexcept for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement itwithout using extra memory? HideTags Bit Manipulation #pragma once

Single Number II (17)

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? 这一次数字重复出现的次数是3了,不像是 single number中的2,不能

【LeetCode】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? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可