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出现的个数,最后摸3求余数,1则置最终位为1

class Solution {
public:
int singleNumber(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int res = ;
int x, sum; for(int i = ; i < ; ++i){ sum = ;
x = (<<i); for(int j = ; j< n; ++j)
if(A[j]&x)
++sum; sum = sum%; if(sum == )
res |= x;
} return res;
}
};

LeetCode_Single Number II的更多相关文章

  1. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  2. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  3. 【题解】【位操作】【Leetcode】Single Number II

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

  4. LightOJ 1245 Harmonic Number (II)(找规律)

    http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS    ...

  5. [OJ] Single Number II

    LintCode 83. Single Number II (Medium) LeetCode 137. Single Number II (Medium) 以下算法的复杂度都是: 时间复杂度: O( ...

  6. [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III

    Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...

  7. Single Number,Single Number II

    Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of ...

  8. Ugly Number,Ugly Number II,Super Ugly Number

    一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...

  9. 1245 - Harmonic Number (II)(规律题)

    1245 - Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...

随机推荐

  1. 修改mysql数据存储的地址

    修改mysql数据存储的地址 修改步骤如下 1,修改前为默认路径/var/lib/mysql/,计划修改为/data/mysql/data mysql> show variables like ...

  2. C++ —— 笔记汇总

    导读 本文仅用于记录在个人在使用C++过程中的遇到一些的疑问和概念. 目录 语法和概念基础 常用函数 编程注意 编译问题 拓展链接 1.语法和概念基础 1.块域     2.static 作用域    ...

  3. Object -C self -- 笔记

    - 是实例方法,是用实例对象进行调用的:+ 是类方法,又称静态方法,是用类来调用的: @interface Person: NSObject { int _age; } - (void) setAge ...

  4. [Angular 2] Handling Clicks and Intervals Together with Merge

    Observable.merge allows you take two different source streams and use either one of them to make cha ...

  5. LAMP php5.4编译 _php_image_stream_putc等问题

    编译时出现下列问题时: In file included from /usr/local/src/php-5.4.6/ext/gd/gd.c:103: /usr/local/src/php-5.4.6 ...

  6. [转] splice系列系统调用

    关注splice系列系统调用(包括splice,tee和vmsplice)已经有一段时间了,开始的时候并未能领会splice的意义所在,致使得出了“splice系列系统调用不怎么实用”的错误结论.随着 ...

  7. Java基础知识强化95:Calendar类之Calendar类的add()和set()方法

    1. Calendar的add()和set()方法: public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作 public ...

  8. 使用nice命令调整进程优先级

    Adjusting Process Priority with nice   When Linux processes are started, they are started with a spe ...

  9. 图片样式 scaleType 属性

    ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType),不同值的意义区别:

  10. GitHub的css/js文件给墙了的解决方法

    今天早上一打开github发现css等都加载失败. 后来发现是给墙了. 本人用的是windows. 解决方法:改hosts 一般情况是在这里:C:\Windows\System32\drivers\e ...