题目地址:here

题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数

对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把数组中所有数异或的结果就是这个出现一次的数,leetcode上也有这个题目

关于本题:我们用一个大小为32的数组cnt来记录:(int数据的每个二进制位1的出现次数)%3,程序中cnt[i]记录第i+1个二进制位。如果某个数出现了三次并且他的第i个二进制位为1,因为做了模3操作,那么该数对数组cnt[i-1]的贡献为0,那么遍历完整个输入数组后,cnt[i]存储的就是这个出现一次的数的第i+1个二进制位。

程序中对于数组中的负数,都取其绝对值,这对结果没有影响,只是cnt数组最后保存的是这个出现一次的数的绝对值的二进制位,因此同时还要通过记录原数据的符号位来确定这个只出现一次的数的正负

代码如下:

 class Solution {
public:
int singleNumber(int A[], int n)
{
// Note: The Solution object is instantiated only once and is reused by each test case.
short cnt[];//cut[i]表示A中第(i+1)个二进制位的1个数(结果对3求模)
short sign = ;//用来确定single number的符号,0正1负
memset(cnt, , sizeof(short)*);
for(int i = ; i < n; i++)
{
int tmp = A[i];
if(A[i] < )
{
tmp *= -;
sign = (sign + ) % ;
}
for(int k = ; k < ; k++)
{
cnt[k] = (cnt[k] + (tmp & 0x00000001))%;
tmp = tmp >> ;
}
}
int res = , base = ;
for(int i = ; i < ; i++)
{
res += (base*cnt[i]);
base <<= ;
}
return sign == ? res : res*-;
}
};

 【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3388170.html

LeetCode:Single Number II的更多相关文章

  1. [LeetCode] Single Number II 单独的数字之二

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

  2. LeetCode——Single Number II(找出数组中只出现一次的数2)

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

  3. [leetcode]Single Number II @ Python

    原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...

  4. [Leetcode] single number ii 找单个数

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

  5. [LeetCode] Single Number II 位运算

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

  6. Leetcode Single Number II (面试题推荐)

    还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here  相信大家都知道用异或在O(n)的时间复 ...

  7. LeetCode | Single Number II【转】

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

  8. LeetCode——Single Number II

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

  9. leetcode Single Number II - 位运算处理数组中的数

    题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...

随机推荐

  1. INFORMATICA 的部署实施 MTP&MTS

    软件开发的一般都有三个环境,开发环境,用户接受度测试环境,生产环境.我最近实施了从开发环境到生产环境的部署工作,在此跟大家分享一下. 大概步骤如下: 1 备份生产环境INFORMATICA 知识库  ...

  2. Effective Java 69 Prefer concurrency utilities to wait and notify

    Principle Use the higher-level concurrency utilities instead of wait and notify for easiness. Use Co ...

  3. 编写一个Java项目,定义包,在包下定义包含main方法的类,在main方法中声明8种基本数据类型的变量并赋值,练习数据类型转换。

  4. D_S 循环队列的基本操作

    //  main.cpp #include <iostream> using namespace std; #include "Status.h" typedef in ...

  5. web.xml文件报错:The processing instruction target matching "[xX][mM][lL]" is not allowed.

    昨晚把我的项目上传到了gitlab,然后今天在公司从gitlab下载下来, 发现web.xml报错.

  6. A python tool to static sim.log duration time

    When working ALU IMS Patch team, we need to static the SU duration to add it to the patch report, th ...

  7. Windows8下PhoneGap 4 + Android Studio 1.0 + VS2013配置指南

    1.准备工作 安装JDK1.6+,设置环境变量 JAVA_HOME C:\Program Files\Java\jdk1.5.0_07 CLASSPATH .;%JAVA_HOME%\lib Path ...

  8. [转载]ExtJs4 笔记(7) Ext.tip.ToolTip 提示

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  9. java 15-10 List的三个子类的特点

    List:(面试题List的子类特点) ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高. Vector: 底层数据结构是数组,查询快,增删慢. 线程安全,效率低. Li ...

  10. SQL语句统计每天、每月、每年的数据

    1.每年select year(ordertime) 年,sum(Total) 销售合计from 订单表group by year(ordertime) 2.每月select year(orderti ...