【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)
随机推荐
- POJ_2478 Farey Sequence 【欧拉函数+简单递推】
一.题目 The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbe ...
- CF F - Tree with Maximum Cost (树形DP)给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大。输出最大的值。
题目意思: 给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大.输出最大的值. ...
- Oracle KEEP的用法
[摘录自] http://blog.itpub.net/12932950/viewspace-687036/ http://flyfx.iteye.com/blog/1994993 聚合函数MIN, ...
- java.lang.ref.Reference<T>
//看之前先要知道java里面的四种引用.package com.zby.ref; import sun.misc.Cleaner; /** * 引用对象的抽象基础类.这个类定义了所有引用对象的公共操 ...
- intelliJ idea像eclipse一样在class中断点调试
安装插件 Bytecode Viewwe Java Bytecode Decompiler
- web前端css定位position和浮动float
最近做了几个项目:配资公司,ecmal商城等,客户对前台要求都很高.所以,今天来谈谈css的基础,以及核心,定位问题. div.h1或p元素常常被称为块级元素.这意味着这些元素显示为一块内容,即“块框 ...
- Web API 解决跨域问题
一.跨域问题的由来 同源策略:出于安全考虑,浏览器会限制脚本中发起的跨站请求,浏览器要求JavaScript或Cookie只能访问同域下的内容. 正是由于这个原因,我们不同项目之间的调用就会被浏览器阻 ...
- PIE SDK去相关拉伸
1.算法功能简介 由于高度相关的数据集经常生成十分柔和的彩色图像,因此经常使用 去相关拉伸工具来体消除多光谱数据集中的高度相关性, 从而生成一幅色彩亮丽的彩色合成图像.去相关拉伸需要 3 个输入波段, ...
- js的apply和call
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- query纠错方法
1. 第一种,在norvig介绍的方法中,详细的阐述了argmaxc P(c|w)的转换和求解办法. 这个概率不好直接算,但可以根据贝叶斯定理等价于argmaxc P(w|c)*P(c) / P(w) ...