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 ...
随机推荐
- POJ1470 LCA (Targan离线)
bryce1010模板 http://poj.org/problem?id=1470 /*伪代码 Tarjan(u)//marge和find为并查集合并函数和查找函数 { for each(u,v) ...
- AtCoder Regular Contest 078 D
D - Fennec VS. Snuke Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement F ...
- 运用session来控制用户的异地登录被挤下线情况
在用QQ的过程中我们如果你的账号在另外一台手机上面登录,这是腾讯后台会提醒你异地登录,可能你的账号被盗了,然后你手机上得QQ就会被退出登录,这个时候你就需要重新登录修改密码,以确保账号的安全.那这种被 ...
- substring和substr,slice和splice
substring 和 substr 这二货都是针对字符串而言的,且都是返回一个副本,而不是在原字符串上直接操作. 上代码: var str = '0123456789'; console.log( ...
- Linux下环境搭建(一)——java、tomcat配置
通过2个周末小憩的时间,终究是把linux环境下的jenkins+gitlab+jmeter框架给弄好了.jenkins的配置系列文章,可以翻看我以前的博文.此次,就将在linux下搭建环境的过程以博 ...
- liunx下忘记root密码的解决方法
1.在Liunx 刚开始重启时.我们这里按“e”键进入系统启动前的Grub配置.(注:一般要安两次e键)(如果你的系统引导程序是LILO,和Grub大体类似,请自行参照LILO给出的提示操作) 如图: ...
- css命名规范—CSS样式命名整理
CSS样式命名整理 页面结构 容器: container/wrap整体宽度:wrapper页头:header内容:content页面主体:main页尾:footer导航:nav侧栏:sidebar栏目 ...
- MySQL存储过程(批量生成论坛中发帖、回帖、主题等数据)
USE 数据库名称1;DROP PROCEDURE IF EXISTS 数据库名称1.存储过程名称;delimiter $$CREATE PROCEDURE 数据库名称1.存储过程名称(in v_co ...
- codevs 1145 Hanoi双塔问题 2007年NOIP全国联赛普及组
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的 ...
- SAP CRM中间件下载equipment时遇到的一个错误
在CRM开发系统上进行equipment下载,发现不工作.调试发现错误信息在下图定96行的WHEN default分支抛出的: MESSAGE ID 'AZ' ... 通过阅读源代码发现,ERP端支持 ...