【Leetcode】【Medium】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?
解题思路:
在 Single Number I 中使用了异或一个数两次,原值不变的性质,即将出现两次的数值当做没出现;同样,如何将出现三次的数字当做没出现,是本题的思路之一。
解题的方法是,将每个整数看做32位,记录每一位上面的位值出现次数;由于只有一个数出现一次,其余数出现3次;因此当一个位上出现3的倍数次时,证明此位不在single number的位序列中,所有出现3的倍数加1次的位,共同组成了single number。
实现方法1:
使用三个int变量,once、twice、thrice分别记录每一位上位值出现的次数;最终once上余留的值,合起来就是只出现一次的数;
class Solution {
public:
int singleNumber(int A[], int n) {
int once = , twice = , thrice = ;
for (int i = ; i < n; i++) {
// 当某位上once和A[i]同时为1时,twice此位设置为1,否则不变
twice |= once & A[i];
// 先不考虑thrice的存在,和twice配合,10为2次,11为3次
once ^= A[i];
// 如果once和twice某位均为1,说明已经达到3次,则置thrice为1,否则为0;
thrice = once & twice;
// 反转thrice再和once与,即当thrice中某位为1时,将once的此位置为0
once &= ~thrice;
// 反转thrice再和twice与,即当thrice中某位为1时,将twice的此位置为0
twice &= ~thrice;
}
return once;
}
};
实现方法2:
和方法1类似,用两个int变量合起来表示某位上值出现的次数,即:
00不出现、01出现一次、10出现两次、11出现三次;
当出现三次时,清除此位上的数字;
代码略
【Leetcode】【Medium】Single Number II的更多相关文章
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【LeetCode】137. Single Number II (3 solutions)
Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
随机推荐
- A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列
Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 ...
- C# 检查键盘是否唤醒到桌面上显示
大概需要的win32函数 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName)//返回值为具有 ...
- [转] 使用Docker容器,这些错误千万别犯
[From]http://www.maiziedu.com/article/23592/ 之前我写了一篇文章(作为ruby程序猿, 为什么非得用Docker?),里面详细讲了他的优点,相信大家都有所了 ...
- drf序列化器的实例
应用目录结构: views.py from django.shortcuts import render # Create your views here. from django.views imp ...
- 通过Qt从URL下载文件
示例1: 通过Qt自带的例子学习,位置:[安装盘符]:\Qt\Qt5.1.1\5.1.1\Src\qtbase\examples\network\download 示例2: 通过Qt的文档,位置: ...
- Lucene常用类
1.1 IndexWriter: 充当 创造/在索引过程中更新指标的 核心组成部分 1.2 Lucene目录 Directory: 索引的存储位置: 通常是文件的列表: 这些文件被称为索引文件. ...
- underscore javascript工具库支持seajs模块化
underscore是一个很有用的js工具库,但是好像默认不支持seajs模块化 新建一个文件例如叫做xx.js 谈后,键入 define(function(require,exports,modul ...
- Grafana监控可视化环境搭建
依赖库Go 1.6NodeJS v4+sqlite3GO 环境搭建 vi /etc/profile export GOPATH="/root/go" export GOROOT=& ...
- SVM之Python实现
SVM Python实现 Python实现SVM的理论知识 SVM原始最优化问题: \[ min_{w,b,\xi}{1\over{2}}{||w||}^2 + C\sum_{i=1}^m\xi^{( ...
- bzoj 4543: [POI2014]Hotel加强版
Description 给出一棵树求三元组 \((x,y,z)\,,x<y<z\) 满足三个点两两之间距离相等,求三元组的数量 Solution 考虑暴力 \(DP\) 设 \(f[i][ ...