【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)
随机推荐
- sharepoint_study_5
描述:手动进行SharePoint网页调试图解 解决: 第一步:打开页面的后台代码,设置断点 第二步:添加到进程 第三步:选择SharePoint进程,我这里都选了,如果你知道要调试的页面是哪一个进程 ...
- Visual Studio Ultimate 2013 免费下载地址
ed2k://|file|cn_visual_studio_2010_ultimate_x86_dvd_532347.iso|2685982720|4AE6228933DDE49D9BFA4C3467 ...
- #537 (Div. 2) Creative Snap (思维+dfs)
https://codeforces.com/contest/1111/problem/C 横坐标1..2^n对应着2^n个复仇者的基地,上面有k个复仇者(位置依次给出).你是灭霸你要用以下方法消灭这 ...
- POJ - 2528 奇怪的测试数据
听说POJ内部测试数据有问题 我这份代码是WA的(UPD:第二份是AC代码),不过目前把discuss的数据试了一下没毛病 自己试了几组好像也没毛病? 感觉线段树部分的简单hash处理方法还是值得学习 ...
- WSH的SpecialFolders对像
前面我做过一个VBS,来实现添加网站快捷方式到开始菜单,当然你也可以添加到别的地方,这就是那个对像的一些用法返回 SpecialFolders 对象(特殊文件夹集). object.SpecialFo ...
- java8 方法引用与lambda
List<String> list = new ArrayList<>(); //list.stream().filter((String s)->System.out. ...
- PIE SDK面元素的绘制
1. 功能简介 在数据的处理中会用到线元素的绘制,目前PIE SDK支持IFillSymbol接口,FillSymbol对象是用于修饰填充面状对象的符号,它包括MarkerFillSymbol(由点状 ...
- js插件编程-tab框
JS代码 (function (w) { //tab对象 function Tab(config) { //定义变量,防止变量污染 this.tabMenus=null; this.tabMains= ...
- 获取window.location.href路径参数
GetQueryString(param) { //param为要获取的参数名 注:获取不到是为null var currentUrl = window.location.href; //获取当前链接 ...
- jumpserver 安装详解
一,下载软件 下载前安装依赖软件 yum install -y epel-release yum -y install git python-pip my ...