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- ...
随机推荐
- OpenJudge-百练-2755
这道题用递归写的话还是很好写的,我们设递归函数的名称为Ways(w,k) . 它的含义就是,w的大小,取k个物品,有多少种方式. 我们可以知道递归的边界条件就是当w的大小为0的时候,我们的方法数只有一 ...
- Java Web中的编码解析
在springmvc工程web.xml中配置中文编码 <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> <filter> <filter-name&g ...
- CSS3---关于文本
1.text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 2.但是text-overflow只是用来说明文字溢出时用什么方式显示,要实现溢出时产生省略号的效果,还须定 ...
- Activiti数据表
--1:资源库流程规则表SELECT * FROM JEESITE.act_re_deployment --部署信息表SELECT * FROM JEESITE.act_re_model --流程设计 ...
- POJ 4118 开餐馆
Description 北大信息学院的同学小明毕业之后打算创业开餐馆.现在共有n 个地点可供选择.小明打算从中选择合适的位置开设一些餐馆.这 n 个地点排列在同一条直线上.我们用一个整数序列m1, m ...
- 如何在开发时用PC端对移动端页面进行调试
原文转载于:https://cnodejs.org/topic/56ebdf2db705742136388f71 项目名称: spy-debugger 项目地址:https://github.com/ ...
- 【三种负载均衡器的优缺点】LVS Nginx HAProxy
搭建负载均衡高可用环境相对简单,主要是要理解其中原理.此文描述了三种负载均衡器的优缺点,以便在实际的生产应用中,按需求取舍. 目前,在线上环境中应用较多的负载均衡器硬件有F5 BIG-IP,软件有LV ...
- zoj 2886 Look and Say
Look and Say Time Limit: 2 Seconds Memory Limit: 65536 KB The look and say sequence is defined ...
- 66. No EntityManager with actual transaction available for current thread【从零开始学】
[从零开始学习Spirng Boot-常见异常汇总] 具体异常信息: org.springframework.dao.InvalidDataAccessApiUsageException: No En ...
- centos 7 下vnc弹出窗口太小解决方法
使用以下参数启动 : vncserver :2 -geometry 800x600