Single Number:

1. 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?

代码:

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

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?

方法1:(分别计算各位1的个数 mod 3)

class Solution {
public:
int singleNumber(int A[], int n) {
int count[32] = {0};
int result = 0;
int bit = sizeof(int) * 8;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < bit; ++j){
if((A[i] >> j) & 0x1) count[j] = (count[j] + 1) % 3;
}
}
for(int i = 0; i < bit; ++i){
result |= (count[i] << i);
}
return result;
}
};

方法2:(三个变量,分别记录出现一次,出现两次,出现三次的值)

class Solution {
public:
int singleNumber(int A[], int n) {
int one, two, three;
one = two = three = 0;
for(int i = 0; i < n; ++i){
two |= (one & A[i]);
one ^= A[i];
three = ~(one & two);
one &= three;
two &= three;
}
return one;
}
};

4.Single Number && Single Number (II)的更多相关文章

  1. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  2. 《Mastering Opencv ...读书笔记系列》车牌识别(II)

    http://blog.csdn.net/jinshengtao/article/details/17954427   <Mastering Opencv ...读书笔记系列>车牌识别(I ...

  3. 人人都是 DBA(II)SQL Server 元数据

    SQL Server 中维护了一组表用于存储 SQL Server 中所有的对象.数据类型.约束条件.配置选项.可用资源等信息,这些信息称为元数据信息(Metadata),而这些表称为系统基础表(Sy ...

  4. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  5. 函数式Android编程(II):Kotlin语言的集合操作

    原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...

  6. 用Kotlin开发Android应用(II):创建新项目

    这是关于Kotlin的第二篇.各位高手发现问题,请继续“拍砖”. 原文标题:Kotlin for Android(II): Create a new project 原文链接:http://anton ...

  7. 逻辑回归 vs 决策树 vs 支持向量机(II)

    原文地址: Logistic Regression vs Decision Trees vs SVM: Part II 在这篇文章,我们将讨论如何在逻辑回归.决策树和SVM之间做出最佳选择.其实 第一 ...

  8. 详解 ML2 Core Plugin(II) - 每天5分钟玩转 OpenStack(72)

    上一节我们讨论了 ML2 Plugin 解决的问题,本节将继续研究 ML2 的架构. ML2 对二层网络进行抽象和建模,引入了 type driver 和 mechansim driver. 这两类 ...

  9. 百度地图学习(II)-Android端的定位

    哎,经历了小编的最近时间的研究,我的百度定位终于成功啦,刹那间觉得自己萌萌哒啦(- ̄▽ ̄)- 话不多说,直接进入正题: 首先,我们来看一下效果: [分析定位原理] [编码分析] 1)处理程序的清单文件 ...

随机推荐

  1. Java中的数组

    1,可以创建自己的类的数组,但是自己的类必须实现get()和put函数 2,声明数组:int[] arrayOfInt.注意,定义数组时不可以指定大小 3,创建与初始化:通过new.arrayOfIn ...

  2. Rhel6-mpich2 hpc集群配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.121 server21.example.com 192.168.12 ...

  3. linux下用命令导出mysql表数据

    由于数据库服务器是内网环境,只能通过linux跳板机连接,所以navicat工具暂时用不上. 1.用Xshell工具连接跳板机 2.再通过跳板机连接数据库服务器 >ssh -p port ip ...

  4. 解决SSH会话连接超时问题

    用SSH客户端连接linux服务器时,经常会出现与服务器会话连接中断现象,照成这个问题的原因便是SSH服务有自己独特的会话连接机制.记得在一年前就有朋友问过我这个问题,那时候我便是草草打发,结果自己现 ...

  5. Java中二维数组与面向对象

    1:二维数组(理解) (1)元素是一维数组的数组. (2)格式: A:数据类型[][] 数组名 = new 数据类型[m][n]; B:数据类型[][] 数组名 = new 数据类型[m][]; C: ...

  6. Qt开发环境中使用报表控件FastReport遇到的一些问题(一)

    FastReport是一款优秀的报表控件,由俄罗斯开发,国内有代理商.Qt环境下可实现报表功能的还有一款叫NCReport,也是收费控件,比较来比较去还是觉得前者功能强点.网上讲解此控件在Qt中使用的 ...

  7. Spring 核心概念以及入门教程

    初始Spring 在学习Spring之前我们首先要了解一下企业级应用.企业级应用是指那些为商业组织,大型企业而创建并部署的解决方案及应用. 这些大型企业级应用的结构复杂,涉及的外部资源众多,事务密集, ...

  8. 使用PHP处理文本小技巧

    PHP的Cli模式使用:http://www.php.net/manual/zh/features.commandline.php PHP命令行部分参数:-B     在处理 stdin 之前先执行 ...

  9. C# 计算时间差 用timespan函数

    TimeSpan 结构  表示一个时间间隔. 命名空间:System 程序集:mscorlib(在 mscorlib.dll 中) 1.DateTime值类型代表了一个从公元0001年1月1日0点0分 ...

  10. TCP链接时主动close时可能的rst报文

    阿里核心系统团队博客http://csrd.aliapp.com/?p=1055记录了主动关闭TCP socket时,可能不发fin包,而是发rst的问题. 其原因是主动关闭socket时,若接收bu ...