LeetCode(137) 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?
分析
由上一题改动而来,LeetCode 136 Single Number描述的是:数组中其他数出现两次,仅有一个出现一次的,直接用所有元素异或就行了(只要是偶数次,都可以用这个方法),本题变为其他元素出现3次,而且时间复杂度要求线性,空间为常数。
用排序的方法依然可以简单解决,但是复杂度不符合线性的要求。而且,此题不能简单的用异或运算实现了。
看到了一个具有很棒实现方法的帖子:LeetCode 137 Single Number II,提供了两种方法,受益匪浅。
AC代码
class Solution {
public:
/*利用算法库的排序实现,复杂度为O(nlogn)*/
//int singleNumber(vector<int>& nums) {
// if (nums.empty())
// return -1;
// sort(nums.begin(), nums.end());
// int size = nums.size();
// for (int i = 0; i < size - 2; i+=3)
// {
// if (nums[i] != nums[i + 1])
// return nums[i];
// }
// return nums[size - 1];
//}
/* 方法二:
* int 数据共有32位,可以用32变量存储这 N 个元素中各个二进制位上 1 出现的次数,
* 最后 在进行 模三 操作,如果为1,那说明这一位是要找元素二进制表示中为 1 的那一位。
* 时间:O(32*N),这是一个通用的解法,如果把出现3次改为 k 次,那么只需模k就行了
*/
//int singleNumber(vector<int>& nums) {
// if (nums.empty())
// return -1;
// vector<int> bitnum(32, 0);
// int res = 0 , size = nums.size();
// for (int i = 0; i < 32; i++){
// for (int j = 0; j < size; j++){
// bitnum[i] += (nums[j] >> i) & 1;
// }
// res |= (bitnum[i] % 3) << i;
// }
// return res;
//}
/* 方法三:
* 利用三个变量分别保存各个二进制位上 1 出现一次、两次、三次的分布情况,最后只需返回变量一。
*/
int singleNumber(vector<int>& nums) {
if (nums.empty())
return -1;
int size = nums.size();
int one = 0, two = 0, three = 0;
for (int i = 0; i < size; i++){
two |= one&nums[i];
one ^= nums[i];
//cout<<one<<endl;
three = one&two;
one &= ~three;
two &= ~three;
}
return one;
}
};
LeetCode(137) Single Number II的更多相关文章
- leetcode文章137称号-Single Number II
#include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int cou ...
- LeetCode(136) Single Number
题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(202) Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
随机推荐
- morphia(1)-基础
二.Mapping classes entity类上加注解:@Entity,其成员变量必须有@Id @Id private ObjectId id; 其在mongodb中变量名: _id @Embed ...
- yii2 使用gii生成代码文件
访问地址: http://localhost/yii2-test/web/index.php?r=gii 如果你通过本机以外的机器访问 Gii,请求会被出于安全原因拒绝. 在web.php修改gii ...
- nodejs中的异步回调机制
1.再次clear Timer定时器的作用 setTimeOut绝非是传统意义上的“sleep”功能,它做不到让主线程“熄火”指定时间,它是用来指定:某个回调在固定时间后插入执行栈!(实际执行时间略长 ...
- C语言abort函数
C语言编程入门教程,C语言库函数的abort函数的作用是异常终止一个进程,意味着abort后面的代码将不再执行. #include<stdio.h> #include<stdlib. ...
- Objective-C Operators and Expressions
What is an Expression? The most basic expression consists of an operator, two operands and an assign ...
- log4sql介绍
log4sql介绍log4j环境中简单配置的情况下可收集执行的SQL语句和JDBC执行情况,如预编译的”?“显示成参数的实际值 下载log4sql.jar第一步:http://log4sql.sour ...
- No input file specified的解决方法
(一)IIS Noinput file specified 方法一:改PHP.ini中的doc_root行,打开ini文件注释掉此行,然后重启IIS 方法二:请修改php.ini找到; cgi.for ...
- 小tip: 使用CSS将图片转换成黑白(灰色、置灰)[转]
小tip: 使用CSS将图片转换成黑白(灰色.置灰) 这篇文章发布于 2012年08月19日,星期日,20:41,归类于 css相关, SVG相关. 阅读 159943 次, 今日 146 次 ...
- 洛谷 P2947 [USACO09MAR]仰望Look Up
题目描述 Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again stan ...
- windows8无脑式双系统安装教程(转)
转:http://blog.csdn.net/poem_qianmo/article/details/7334987 首先去微软官网将ISO文件下载下来,分为32bit跟64bit两个版本,因人而异, ...