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 ...
随机推荐
- 错误 2 error C2059: 语法错误:“::”
设置项目属性,在预定义处理器中添加定义NOMINMAX来禁止使用Vsual C++的min/max宏定义. 项目属性 ——> C/C++ ——> 预处理器 ——> 预处理器定义 ...
- vue 编写插件
1. 将插件逻辑封装成一个对象 最后在install编写业务代码暴露给Vue对象 好处: 可以添加任意参数在这个对象上 方便将install函数封装的更加精简 可拓展性也比较高 2. 将所有 ...
- hashMap 源码解读理解实现原理和hash冲突
hashMap 怎么说呢. 我的理解是 外表是一个set 数组,无序不重复 . 每个set元素是一个bean ,存着一对key value 看看代码吧 package test; import jav ...
- Pandas怎样按条件删除行?
来自: https://stackoverflow.com/questions/13851535/delete-rows-from-a-pandas-dataframe-based-on-a-cond ...
- python 为 class 添加新的属性和方法
通过继承: >>> class Point(namedtuple('Point', ['x', 'y'])): ... __slots__ = () ... @property .. ...
- springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)
springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required) 最近在项目中想试一下使用 Hikari 连接池,以前用 ...
- HBase性能优化方法总结 (转)
AutoFlush 通过调用HTable.setAutoFlushTo(false)方法可以将HTable写客户端自动flush关闭,这样可以批量写入数据到HBase,而不是有一条put就执行一次更新 ...
- 在银行业中,BP是指什么?
基点 Basis Point(BP)债券和票据利率改变量的度量单位.一个基点等于0.01个百分点,即0.01%,因此,100个基点等于1%.[例]一浮动利率债券的利率可能比LIBOR高10个基点,10 ...
- Acesrc and Travel
Acesrc and Travel 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Acesrc is a famous tourist at Nanjing University se ...
- UVA1391/LA3713 Astronauts
题意:有A.B.C3个任务分配给n个宇航员,其中每个宇航员恰好分配一个任务.假设n个宇航员的平均年龄为x,只有年龄大于x的才能领取A任务:只有年龄严格小于x的才能领取B任务,而任务C没有限制.有m对宇 ...