Leetcode Single Number II (面试题推荐)
还记得《剑指offer》和《编程之美》等书上多次出现的找一个数组中仅仅出现一次的数那个题吗?
leetcode也有这道题 链接here 相信大家都知道用异或在O(n)的时间复杂度内求出的方法,这里不再赘述。
以下就是上题的升级版
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?
给你一个整数数组。每一个元素出现了三次。但仅仅有一个元素出现了一次,让你找出这个数。要求线性的时间复杂度,不使用额外空间。
这样就不能使用异或的方法了。把全部元素以后得到的就是全部元素的异或值了,对我们没有不论什么帮助。
这个问题依然使用位运算来解决,只是更加复杂。
我们用三个变量one two three,分别存储二进制未分别出现一次 两次 三次的位。
class Solution {
public:
int singleNumber(int A[], int n) {
int one = 0, two = 0, three = 0;
for(int i = 0; i < n; i++)
{
three = two & A[i]; //出现三次的位肯定是由出现两次的得到的
two = two | ones & A[i]; //出现两次的肯定是出现一次的得到的,只是还有原有的所以要或
one = one | A[i]; //计算出现一次的位
two = two & ~three; //去掉二进制中出现三次的位
ones = one & ~three; //去掉二进制中出现三次的位</span>
}
return one; //终于twos three都为0。one就是我们要的答案
}
};
Leetcode Single Number II (面试题推荐)的更多相关文章
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 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 II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- [Leetcode] single number ii 找单个数
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number II 位运算
Given an array of integers, every element appears three times except for one. Find that single one. ...
- LeetCode | Single Number II【转】
题目:Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode——Single Number II
Description: Given an array of integers, every element appears three times except for one. Find that ...
- leetcode Single Number II - 位运算处理数组中的数
题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...
随机推荐
- python 一些函数和类用法记录
这一篇主要用来记录在学习过程中遇到的一些觉得有意思的函数或者类的用法,有一些用法感觉很炫酷. 1.collections.defaultdict from collections import def ...
- InnoDB 事务隔离级别
InnoDB是一种平衡高可靠性和高性能的通用存储引擎.在MySQL数据库5.5.8版本开始,InnoDB是默认的MySQL存储引擎. InnoDB的主要优势 - 其DML操作遵循ACID,具有提交,回 ...
- MySQL 优化 之 Copying to tmp table on disk
项目中遇到了慢查询问题 Sql语句 SELECT sum(price) AS price, `member_id` FROM `crm_upload` GROUP BY member_id ORDER ...
- go语言的碎片整理:time
时间和日期是我们编程中经常用到的,本文主要介绍了Go语言内置的time包的基本用法. Go语言中导入包 单行导入 import "time" import "fmt&qu ...
- python 以及 pywin32添加注册表
python 添加注册表信息: import sys from winreg import * # tweak as necessary version = sys.version[:3] insta ...
- 数据结构实验1:C++实现静态顺序表类
写了3个多小时,还是太慢了.太菜了! 图1 程序运行演示截图1 实验1 1.1 实验目的 熟练掌握线性表的顺序存储结构. 熟练掌握顺序表的有关算法设计. 根据具体问题的需要,设计出合理的表示数据的顺序 ...
- 如何使用Visual Studio 2008(VS2008)编译C语言
大家在学习C语言的时候接触的一般都是VC6.0.但是VC6.0只能编译C或者C++,不支持C#,集成度不是很高.而且界面并不十分友好,不能自动猜测关键字,函数的参数也不能自动标示.最关键的是,编译的时 ...
- BZOJ3027 - [CEOI2004]Sweet
Portal Description 给出\(n(n\leq10),a,b(a,b\leq10^7)\)与\(\{c_n\}(c_i\leq10^6)\),求使得\(\sum_{i=1}^n x_i ...
- springboot注释详解
1.属性注入 @ConfigurationProperties(prefix="...") spring会从classpath下的/config目录或者classpath的根目录查 ...
- PMU 简介
目录 1:PMIC2:Battery管理3:功耗4:常见问题5:参考文献 PMIC[MT6322] Source code structure Build option Battery char ...