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?

代码:

  1. class Solution {
  2. public:
  3. int singleNumber(int A[], int n) {
  4. int result = 0;
  5. for(int i = 0; i < n; ++i){
  6. result ^= A[i];
  7. }
  8. return result;
  9. }
  10. };

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)

  1. class Solution {
  2. public:
  3. int singleNumber(int A[], int n) {
  4. int count[32] = {0};
  5. int result = 0;
  6. int bit = sizeof(int) * 8;
  7. for(int i = 0; i < n; ++i) {
  8. for(int j = 0; j < bit; ++j){
  9. if((A[i] >> j) & 0x1) count[j] = (count[j] + 1) % 3;
  10. }
  11. }
  12. for(int i = 0; i < bit; ++i){
  13. result |= (count[i] << i);
  14. }
  15. return result;
  16. }
  17. };

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

  1. class Solution {
  2. public:
  3. int singleNumber(int A[], int n) {
  4. int one, two, three;
  5. one = two = three = 0;
  6. for(int i = 0; i < n; ++i){
  7. two |= (one & A[i]);
  8. one ^= A[i];
  9. three = ~(one & two);
  10. one &= three;
  11. two &= three;
  12. }
  13. return one;
  14. }
  15. };

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. launch文件

    launch在ROS应用中,每个节点通常有许多参数需要设置,为了方便高效操作多个节点,可以编写launch文件,然后用roslaunch命令运行roslaunch: roslaunch [option ...

  2. Oracle中TO_DATE用法

    TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits 三位年 显示值:007 yyy ...

  3. Jmeter教程索引

    一.基础部分: 使用Jmeter进行http接口测试 Jmeter之Http Cookie Manager Jmeter之HTTP Request Defaults Jmeter之逻辑控制器(Logi ...

  4. MyJni

    package com.baidu.jnitest; import android.os.Bundle; import android.app.Activity; import android.vie ...

  5. 委托Delegate,多播委托和委托链

    定义一个委托 public delegate void CalculateDelegate(int 32 x,int 32 y); 定义一个委托类型的变量 public static Calculat ...

  6. python中的if __name__ == '__main__' what hell is it?

    python中的if __name__ == '__main__' what hell is it? python认为一切模块都可能被执行或者被import 如果一个模块是被import导入的,那么该 ...

  7. 在Bootstrap中 强调相关的类

    .text-muted:提示,使用浅灰色(#999) .text-primary:主要,使用蓝色(#428bca) .text-success:成功,使用浅绿色(#3c763d) .text-info ...

  8. Mysql分区简述

    1. 数据量大的时候 mysql分表非常常用,但是mysql还可以分区. 2. 分区就是把同一张表放在不同的磁盘文件上, 当查询的时候首先定位是哪个分区(查询的时候一定要用到分区的key) 3. 分区 ...

  9. CVE-2014-6271 Bash漏洞利用工具

    CVE-2014-6271 Bash漏洞利用工具 Exploit 1 (CVE-2014-6271) env x='() { :;}; echo vulnerable' bash -c "e ...

  10. mac osx 快捷键符号以及意义 触发角:锁屏

    快捷键中常用符号⌘(command).⌥(option).⇧(shift).⇪(caps lock).⌃(control).↩(return).⌅(enter). OSX快捷键 快捷键中常用符号 ⌘( ...