LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III
136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one. (Easy)
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
分析:
第一问属于技巧题,做过就会,没做过很难想。考虑异或操作,相同数异或之后为0, 0与一个数异或还是这个数本身,且异或操作满足交换律和结合律。
所以这个题将所有的数异或起来,就能得到那个落单的数。
代码:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int result = ;
for (int i = ; i < nums.size(); ++i) {
result ^= nums[i];
}
return result;
}
};
137. Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. (Medium)
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
分析:
除了“单身狗”以外其他元素均出现了三次,考虑上一题中的异或的思想(不进位加法,两个1之后就变成0),推广到三个数,就是有一位1如果出现三遍,就应该抵消为0;
所以可以考虑建立一个32个元素数组表示int的各个位置,然后把每个数的每一位对应加进去,mod 3后的结果恢复成一个数即为结果。
代码:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int bitArray[];
memset(bitArray, , sizeof(bitArray));
int result = ;
for (int i = ; i < ; ++i) {
for (int j = ; j < nums.size(); ++j) {
bitArray[i] += (nums[j] >> i & );
}
bitArray[i] %= ;
result |= (bitArray[i] << i);
}
return result;
}
};
260. Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. (Medium)
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
Note:
- The order of the result is not important. So in the above example,
[5, 3]is also correct. - Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
分析:
有两个单身狗,其他都是出现两次。考虑怎么把这个问题转化为single number1的问题。
利用1中的思路,先把所有的数异或,最后可以得到一个数。可以知道这个数(xorResult)是那两个单身狗异或的结果,但是我们无法从这个数恢复两个数本身。
但是我们可以xorResult中第一个1出现的位置,这个位置是1说明之前两个数在这一位不同(一个0,一个1);
于是我们可以把原数组中的元素这一位是0还是1分为两个数组,这两个数组便都是single number1的问题,最后把两个结果添加到vector返回。
代码:
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int xorResult = ;
for (int i = ; i < nums.size(); ++i) {
xorResult ^= nums[i];
}
int lastBitofOne = xorResult - (xorResult & (xorResult - ) );
int result1 = , result2 = ;
for (int i = ; i < nums.size(); ++i) {
if ( (nums[i] & lastBitofOne) == ) {
result1 ^= nums[i];
}
else {
result2 ^= nums[i];
}
}
vector<int> result{result1, result2};
return result;
}
};
LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III的更多相关文章
- 执行tsung时报"Maximum number of concurrent users in a single VM reached
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ovcer.blog.51cto.com/1145188/1581326 [roo ...
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- LC 375. Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 'Invalid update: invalid number of rows in section xx. The number of rows contained in an existing section after the update (xxx)...
'Invalid update: invalid number of rows in section 5. The number of rows contained in an existing s ...
- 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)
We define the smallest positive real number as the number which is explicitly greater than zero and ...
- LeetCode 137. 只出现一次的数字 II(Single Number II)
题目描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: ...
- [LeetCode] Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
随机推荐
- 基于在树上走的DP问题
笔者已经很久没有打过题解了,如果打题解,就总是要连着一个知识点来打题解. 最近做过一共两道这样的题目.笔者认为这样的题有较强的可拓展性,比较有意义. 所以就打一篇博客. 问题概述 先说说这是个什么样的 ...
- CF1140F - Extending Set of Points
题意:对于点集S,定义函数F(S)为对S不断扩展到不能扩展时S的点数.一次扩展定义为如果有一个平行于坐标轴的矩形的三个点在S中,则第四个点加入S. 动态在S中加点删点,每次操作完后求F(S)的值. 解 ...
- CentOS 6.5 Apache+SVN配置
yum -y install subversion #安装SVN svnserve -- version #查看svn版本信息确定是否安装 yum -y install httpd #安装Apache ...
- CentOS 6.5 服务器相关配置
vi /etc/sysconfig/network-scripts/ifcfg-eth0 #配置网卡 ONBOOT=YES #静态IPBOOTPROTO=static #启用地址协议 --static ...
- react-native start停止在Loading dependency graph, done.
在试验的过程中. 发现运行 react-native start会卡住,停留在Loading dependency graph, done. 原因大概是之前运行过 react-native run-a ...
- jquery学习:选择器&dom操作
分类; 1.基本选择器 1.标签选择器(元素选择器) * 语法:$("html标签名”) 获得所有匹配标签名称的元素 2.id选择器 * 语法:$("#id的属性值" ...
- create user
create创建的用户,只有usage权限,即,连接数据库的权限,最低的权限. # 1.新建用户,这里的用户是由user_name 和ip一起唯一确定一个用户.# 2.若省略ip表达式,则表示%,即所 ...
- Web应用托管服务(Web+)隐藏的十个上云最佳姿势
随着云计算浪潮的推进,技术架构云化已经成为大势所趋.特别是最近由CNCF推动的云原生概念,将符合云原生标准的各种开源技术方案推向了前所未有的高度.在这一波浪潮的推动下,越来越多的企业开始了自身的数字化 ...
- Nginx 日志切割后无法记日志
日志切割会向Nginx Pid发送一个信号重新打开日志文件,如果nginx.conf没有配置PID,切割日志后找不到PID文件,就会出问题
- 洛谷P1470 最长前缀
P1470 最长前缀 Longest Prefix 题目描述 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 ...