LeetCode——single-number系列
LeetCode——single-number系列
Question 1
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Solution
这个题的求解用到了按位异或的操作,因为两个相同的数异或是为0的,然后按位操作又满足交换律,所以一直异或下去就能得到单独的一个数字,这有点像模拟二进制求和。
class Solution {
public:
int singleNumber(int A[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum ^= A[i];
}
return sum;
}
};
进阶版本
Question 2
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?
Solution
要是如果有三进制的操作,那么这道题就很简单了,那么我们需要模拟三进制的操作,ones表示一个1的个数,twos表示两个1的个数,ones&twos结果表示三个1的个数。
class Solution {
public:
int singleNumber(int A[], int n) {
int ones = 0;
int twos = 0;
int threes;
for (int i = 0; i < n; i++) {
int t = A[i];
twos |= ones & t; // 或表示加,与表示求两个1的个数
ones ^= t; // 异或操作表示统计一个1的个数
threes = ones & twos; // 与表示三个1的个数
ones &= ~threes;
twos &= ~threes;
}
return ones;
}
};
还有一种用额外存储空间的解法,统计每一位1的个数。
class Solution {
public:
int singleNumber(int A[], int n) {
int bitSum[32] = {0};
for (int i = 0; i < n; i++) {
int mask = 0x1;
for (int j = 0; j < 32; j++) {
int bit = A[i] & mask;
if (bit != 0)
bitSum[j] += 1;
mask = mask << 1;
}
}
int res = 0;
for (int i = 31; i >= 0; i--) {
res = res << 1;
res += (bitSum[i] % 3);
}
return res;
}
};
LeetCode——single-number系列的更多相关文章
- leetcode single number系列
这个系列一共有三题,第一题是一组数里除了一个数出现一次之外,其他数都是成对出现,求这个数. 第二题是一组数里除了两个数出现一次外,其他数都是成对出现,求这两个数 第三题是一组数里除了一个数出现一次外, ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode: Single Number I && II
I title: Given an array of integers, every element appears twice except for one. Find that single on ...
随机推荐
- dubbo框架的介绍,应用
http://www.cnblogs.com/Javame/p/3632473.html
- vs git .vs12.suo
GIT无法自动忽略SUO文件的解决方法 最近发现一个巨烦人的问题,项目里明明已经通过gitignore忽略了.suo文件,但是每次git pull的时候总是还得到.suo文件冲突的提示,也就是说git ...
- 02、微信小程序的数据绑定
02.微信小程序的数据绑定 目录结构: 模板内容: 使用bindtap绑定事件 <!--index.wxml--> <view class="container" ...
- cocos2d-X学习之主要类介绍:CCDirector
在cocos2d-x里面,游戏的任何时间,只有一个场景对象实例处于运行状态,该对象可以作为当前游戏内容的整体包对象 Cocos2d-x引擎除了提供了CCDirector,还提供了一个CCDisplay ...
- 用jQuery实现简单的DOM操作
通过jQuery创建元素节点:$oLi = $("<li></li>");这样我们就创建了一个li标签 如果想在元素节点中添加文本的话也挺简单:$oLi = ...
- Angular ui-route的用法
ui-router和同属AngularJS框架一部分的ng-route一样强大. ui-router提供了让我们可以做路由嵌套和视图命名的特性,嵌套路由功能主要是依赖$stateProvider服务, ...
- 巨蟒python全栈开发-第7天 基本数据类型补充&深浅拷贝
1.基本数据类型补充 2.深浅拷贝 DAY7-基本数据类型(基本数据类型补充&深浅拷贝) 本节主要内容: 1.补充基础数据类型 (1)join方法 (2)split方法 (3)列表不能在循环时 ...
- C#自动给文章关键字加链接实现代码
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- 接口测试工具 — jmeter(关联)
1.正则表达式 1)添加正则表达式提取器 2)提取关联词 3)填写正则表达式 4)使用关联,其他请求使用${sign2}代替变量值 2. 1)添加提取器 2)填写变量值 3)使用关联,其他请求使用${ ...
- HTTP 常见状态码
1. 以"1"开头(临时响应) 100: Continue,请求者应当继续提出请求;表示服务端已经收到请求的一部分,正在等待其余部分; 101: Switching Protoco ...