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. VC只运行一个程序实例

    方法有很多,以下只是提供一种用的多的 一. 单文档程序 在程序App类的InitInstance中添加如下代码 BOOL CDDZApp::InitInstance() { /*只运行一个实例*/ / ...

  2. 大数运算Swift

    前几天开始,打算用Swift写大数的运算,加法跟乘法都已经写好了,写减法发现,真是难,感觉有可能是我的想法不对?不不不我相信我的逻辑. 首先把数字分成小数部分跟整数部分,再遍历一下,识别当前的结果,是 ...

  3. MySQL数据库1 - 基本概念及安装

    一.数据管理技术的产生和发展: 1.人工管理阶段 - 效率低,成本高(文字) 2.文件系统阶段 - 易于存储,处理速度快,数据形式丰富(文字,声音,图片...磁带,磁盘) 3.数据库系统阶段 - 易于 ...

  4. java中时间类型的问题

    时间类型:System.currentTimeMillis() 获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为longimport java.sql.Date d ...

  5. Python 基礎 - 變量

    變量 變量主要是用來存東西,是存在內存裡 Python 變量寫法: name = "Nobody One" Shell Script 變量寫法: name="Nobody ...

  6. ListView中itemz中控件的点击事件和条目点击事件冲突

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. oc实例变量初始化方法

    1 使用实例setter方法 默认初始化方法 + setName:xxx setAge:xxx 2 使用实例功能类方法,默认初始化方法 + setName:xxx age:xxx3 使用实例初始化方法 ...

  8. c++对象成员的引用---12

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 在程序中经常需要访问对象中的成员.访问对象中的成员可以有3种方法: 通过对象名和成员运算符访问对象中的成 ...

  9. ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  10. HTML标签小记

    <body> </body>标签,网页内容放在这里 <p> </p>标签,网页的段落 <hx> </hx>标签,网页的标题 &l ...