[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 ...
随机推荐
- activiti集成drools实验
无代码,无真相. 网上的博客代码,都挺片段的.所以,我想找个现成的demo实验代码. 上github ------------------------------------------------- ...
- HDU-1035 Robot Motion
http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others) ...
- java web判断是否登录
在使用session的情况下,如何判断是否已登录呢? 1,登录成功的后续操作 session.setAttribute(Constant2.SESSION_KEY_LOGINED_USER, user ...
- C# 应用正则表达式
1.检测该字符串是否 带有字符“8”(类似sql语句:select * from tableName where name like '%8%') string pattern = @"\w ...
- pomelo 服务器之间的通信
master服务器在启动的时候会启动mater服务,生成一个MasterAgent,作为中心服务器. 然后所有服务器,包括mater服务器,都会启动monitor服务,生成一个MonitorAgent ...
- BufferedInputStream,FileInputStream,FileChannel实现文件拷贝
从上篇文章中知道BufferedInputStream是自带缓冲区的输入流,可以大大减少IO次数,提供效率.下面的例子中实现了用BufferedInputStream与FileInputStream实 ...
- C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏
1. 概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...
- EnterpriseArchitectect 软件的勾选的几个选项对应的中文意思
Business Process 业务流程 Requirements 需求分析 Use Case 用例 Domain Model 领域模型 Class 类 Database 数据库设计 Compone ...
- sqlserver 数据行统计,秒查语句
1.传统统计方式 ...
- SQLite的 SQL语法总结
SQLite库可以解析大部分标准SQL语言.但它也省去了一些特性并且加入了一些自己的新特性.这篇文档就是试图描述那些SQLite支持/不支持的SQL语法的.查看关键字列表. 如下语法表格中,纯文本用蓝 ...