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. java排序算法-插入排序

    public class InsertSortUtils { public static void main(String[] args) { insertSortTest(); shellSortT ...

  2. oracle用户解锁和改密

    alter user hs_user account unlock; alter user hs_asset account unlock; alter user hs_his account unl ...

  3. eclipse打开一闪而过,环境安装正确

    一:查看错误信息 开始,运行->cmd.execd 进入eclipse目录D:\JavaTools\eclipse\eclipse.exe>eclipsec.exe,看console输出是 ...

  4. SKKeyframeSequence类

    继承自 NSObject 符合 NSCodingNSCopyingNSObject 框架  /System/Library/Frameworks/SpriteKit.framework 可用性 可用于 ...

  5. js判断访问者是否来自移动端代码

    <script type="text/javascript"> function is_mobile() { var regex_match = /(nokia|iph ...

  6. Chapter 4. Using the Gradle Command-Line 使用gradle命令行

    This chapter introduces the basics of the Gradle command-line. You run a build using the gradle comm ...

  7. NuGet学习笔记(1)——初识NuGet及快速安装使用(转)

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  8. shell编程备忘

    1.脚本存放目录 workspace="$(cd "$(dirname "$0")"; pwd)" 2.输出  其中 command 代表指 ...

  9. xampp进程和非进程执行

    xampp以服务和非服务运行apache有哪些区别?为什么去掉勾是以进程的形式执行?

  10. 002_系统表查询(sysdatabases等)

    002_系统表查询(sysdatabases等) --1.获取所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name --2.获取所有表 ...