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内存泄露的理解与解决(转)

    Java内存管理机制 在C++语言中,如果需要动态分配一块内存,程序员需要负责这块内存的整个生命周期.从申请分配.到使用.再到最后的释放.这样的过程非常灵活,但是却十分繁琐,程序员很容易由于疏忽而忘记 ...

  2. Hadoop学习资料

    转自:http://cloud21.iteye.com/blog/607175 第一手资源 hadoop官方网站 hadoop.apache.org 最权威的官方资源之一 dev.yahoo.hado ...

  3. 高通CP Crash分析调试

    1. 转换tlcore文件 获取 EBICS0.BIN tl2elf --qconly tlcore 2.使用T32 命令把Riva的dump信息从EBICS0文件分离出来 data.load.BIN ...

  4. iOS-Xcode7 网络连接

    Xcode7 网络无法连接问题 1.The resource could not be loaded because the App Transport Security policy require ...

  5. raw_input 和input的区别

    input它会根据用户输入变换相应的类型, raw_input则是不管用户输入什么类型的都会转变成字符型.

  6. git新建仓库

    克隆地址 git clone https://git.oschina.net/dy09/dy_shop.git 在克隆下来的文件夹里面进行下面的操作1.git add -A 2.git commit ...

  7. 关于如何使用Altium Designer 10以上版本官方库

    开卷有益:如果本帖不适合在此板块,请斑竹自行删除,发帖的目的纯属报答各位Amofans.    Altium公司的Altium Designer 09版本及以下还能到Altium官网下载第三方Labr ...

  8. LeetCode() Word Search II

    超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...

  9. LeetCode() Issomorphic Strings

    bool isIsomorphic(string s, string t) { int size=s.size(); if (size==0) return true; char ch[128],is ...

  10. 使用as3控制动画的播放与暂停

    1.需要两个按钮元件 2.在属性面板为两个按钮元件分别命名为pausebutton与playButton 3.代码 stop(); pausebutton.visible = false; playB ...