[LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.
class Solution {
public:
int singleNumber(int A[], int n) {
int bits = sizeof(int)*;
int result=;
for(int i=; i<=bits; i++)
{
int w=;
int t=;
for(int j=; j<n; j++)
w += (A[j]>>(i-))&t;
result+= (w%)<<(i-); //若是除过一个数之外,其他数重复k次,则将此处的3改为k
}
return result;
}
};
利用二进制的位运算来解决这个问题,比如输入的数组是[2,2,3,2]
它们对应的二进制为:
0010 (2)
0010 (2)
0011 (3)
0010 (2)
————
对应位求和后:0 0 4 1
将所有的整数对应的二进制位加起来,再求除以3之后的余数,得到0011 (3),这就是我们要找的single number。
对于除一个整数只出现一次,其他整数都出现k次(k=2,3,4...),要求找到single number这种问题,也可以用上面位运算的方法来解决,只需将程序中的3改为k即可。
[LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.的更多相关文章
- [LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.
class Solution { public: int singleNumber(int A[], int n) { int i,j; ; i<n; i++) { ; j<n; j++) ...
- LeetCode OJ -Happy Number
题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...
- LeetCode OJ:Number of Islands(孤岛计数)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- LeetCode OJ:Number of 1 Bits(比特1的位数)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- LeetCode OJ 之 Number of Digit One (数字1的个数)
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- LeetCode OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- LeetCode OJ 154. Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- LeetCode OJ 153. Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
随机推荐
- 【转】使用命令行方式创建和启动android模拟器
原文网址:http://blog.csdn.net/tiandinilv/article/details/8953001 1.Android模拟器介绍 Android中提供了一个模拟器来模拟ARM核的 ...
- Linux学习笔记5——虚拟内存
一.为什么要有虚拟内存 虚拟内存的提出,是为了禁止用户直接访问物理存储设备,有助于系统稳定. 二.为什么一个程序不能访问另外一个程序的地址指向的空间 1:每个程序的开始地址0x80084000 2:程 ...
- SPOJ 375 (树链剖分+线段树)
题意:一棵包含N 个结点的树,每条边都有一个权值,要求模拟两种操作:(1)改变某条边的权值,(2)询问U,V 之间的路径中权值最大的边. 思路:最近比赛总是看到有树链剖分的题目,就看了论文,做了这题, ...
- 使用JavaScript扫描端口
<html> <title>端口扫描</title> <head></head><form><label fo ...
- php类获取静态变量值以及调用
<?php class Test{ public static $static_var = 20; } echo Test::$static_var;exit;
- javascript 函数参数
1.javascript函数参数的个数以及类型没有强制规定,调用时不必严格按照函数的参数或类型,函数的参数只是在调用函数的时候提供了便利,但不是必须的! 2.参数在javascript内部是用数组ar ...
- 在Sql Server 2005中将主子表关系的XML文档转换成主子表“Join”形式的表
本文转载:http://www.cnblogs.com/Ricky81317/archive/2010/01/06/1640434.html 最近这段时间在Sql Server 2005下做了很多根据 ...
- 开始lisp的旅程
不知道是不是<黑客与画家>的老pual太能忽悠了,一直想把他吹捧的lisp学习一下. 看common lisp和On lisp两本书也有一段时间了,中间还夹着看了一点SICP和land o ...
- 通过MultipleOutputs写到多个文件
MultipleOutputs 类可以将数据写到多个文件,这些文件的名称源于输出的键和值或者任意字符串.这允许每个 reducer(或者只有 map 作业的 mapper)创建多个文件. 采用name ...
- spring mvc DispatcherServlet详解之interceptor和filter的区别
首先我们看一下spring mvc Interceptor的功能及实现: http://wenku.baidu.com/link?url=Mw3GaUhCRMhUFjU8iIDhObQpDcbmmRy ...