lintcode 中等题:Singleton number II 落单的数 II
题目
给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字。
给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4
一次遍历,常数级的额外空间复杂度
解题
可以利用HashMap直接解决,时间复杂度和空间复杂度都是O(N)
1.map中存在该元素则:map.put(num,map.get(num) + 1)
2.map中不存在该元素则:map.put(num , 1)
3.map中这个元素出现次数等于三次,则删除该元素
空间复杂度最坏的情况是O(N*2/3)
public class Solution {
/**
* @param A : An integer array
* @return : An integer
*/
public int singleNumberII(int[] A) {
// write your code here
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
int single = 0 ;
for(int i=0;i<A.length;i++){
if(map.containsKey(A[i])){
map.put(A[i],map.get(A[i]) + 1);
}else{
map.put(A[i],1);
}
if(map.get(A[i]) == 3){
map.remove(A[i]);
}
}
Set<Integer> keySet = map.keySet();
for( Integer num:keySet){
single = num;
}
return single; }
}
Java Code
总耗时: 2647 ms
这个方法不是很好,空间复杂度不是O(1)
上面Hashmap在put的适合都要根据key计算hashcode,再计算位置,再根据所在链表顺序更新,效率不高
在stackoverflow上,value用数组定义,或者自己定义一个引用类型变量,但是上面说的数组效率最高
import java.util.*;
public class Solution {
/**
* @param A : An integer array
* @return : An integer
*/
public int singleNumberII(int[] A) {
// write your code here
HashMap<Integer,int[]> map = new HashMap<Integer,int[]>(); // 数组存储效率高
for(int i=0;i<A.length;i++){
int[] value = map.get(A[i]);
if(value==null){
map.put(A[i],new int[]{1});
}else{
value[0]++; // 直接+1
}
}
for(Map.Entry<Integer,int[]> entry:map.entrySet()){
int[] value = entry.getValue();
if(value[0]==1){
return entry.getKey();
}
}
return -1;
}
}
当a出现一次的时候,ones能保存a。当a出现两次的时候,twos能保存a。
当a出现三次的时候,ones和twos都清零。
所以,如果一个数值中所有的数都通过这个循环的话,出现三次的数都清零了,
有一个数如果出现一次,它保存在ones中;如果出现两次的话保存在twos中。
public class Solution {
/**
* @param A : An integer array
* @return : An integer
*/
public int singleNumberII(int[] A) {
// write your code here
int ones = 0;
int twos = 0;
for(int i=0;i< A.length ;i++){
ones = (ones^A[i]) & (~ twos);
twos = (twos^A[i]) & (~ ones);
}
return ones;
}
}
Java Code
总耗时: 246 ms
class Solution:
"""
@param A : An integer array
@return : An integer
"""
def singleNumberII(self, A):
# write your code here
ones = 0
twos = 0
for num in A:
ones = (ones ^ num) & (~twos)
twos = (twos ^ num) & (~ones)
return ones
Python Code
总耗时: 258 ms
表示不理解。。。
lintcode 中等题:Singleton number II 落单的数 II的更多相关文章
- lintcode 中等题:Single number III 落单的数III
题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...
- 83 落单的数 II
原题网址:http://www.lintcode.com/zh-cn/problem/single-number-ii/ 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这 ...
- LeetCode 136. Single Number (落单的数)
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- lintcode 落单的数(位操作)
题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single ...
- 84 落单的数 III
原题网址:http://www.lintcode.com/zh-cn/problem/single-number-iii/# 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到 ...
- LintCode笔记 - 82.落单的数
这一题相对简单,但是代码质量可能不是很好,我分享一下我的做题笔记以及做题过程给各位欣赏,有什么不足望各位大佬指出来 原题目,各位小伙伴也可以试着做一下 . 落单的数 中文English 给出 * n ...
- lintcode:落单的数
题目: 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 ...
- LinCode落单的数
easy 落单的数 查看执行结果 60% 通过 给出2*n + 1 个的数字,除当中一个数字之外其它每一个数字均出现两次.找到这个数字. 您在真实的面试中是否遇到过这个题? Yes 例子 给出 [1, ...
- [codevs3295]落单的数
题目描述 Description 有n个数(n是奇数),其中n-1个数两两成对,有1个数落单,找出这个数.要求O(n)的时间复杂度,O(1)的空间复杂度 输入描述 Input Description ...
随机推荐
- Linux操作杂记
centos7修改默认运行等级 查看当前默认运行等级: systemctl get-dafault 修改默认运行等级为5: systemctl set-default graphical.target ...
- sql拆分查询
有这样一个需求: 临时表sql: create table #AA ( ID int, Name nvarchar(20) ) insert #AA select 1,'苏州/上海/温州' union ...
- 图解 CSS: 理解样式表的逻辑(转载)
原文:http://www.cnblogs.com/del/archive/2009/02/01/1382141.html 样式表可以是外部的.内联的或嵌入的; 链接外部样式文件一般是:<lin ...
- wpf 仿QQ音乐歌词卡拉OK
最近用WPF做了个音乐播放器,读取歌词.歌词同步都已经实现了.卡拉OK逐字变色 也实现了,但是逐字变色时不能根据歌手唱的快慢来逐字显示.请问各位大神,这个如何解决,有何思路?(附上我做的界面) 感谢各 ...
- ios自定义选择器ActionSheetPicker改进版
ios自带的UIDataPicker和UIDatePicker最大的毛病就是没有带确定和取消这两个按钮,而ActionSheetPicker是以上两个选择器的开源封装.但是这个东东也有些小问题,就是没 ...
- iTween基础之Move(移动)
1,五种移动方法:2, 函数的基础属性及用法 原文地址:http://blog.csdn.net/dingkun520wy/article/details/50476864 iTween官网:http ...
- Javascript 面向对象编程
Javascript是一个类C的语言,他的面向对象的东西相对于C++/Java比较奇怪,但是其的确相当的强大,在 Todd 同学的“对象的消息模型”一文中我们已经可以看到一些端倪了.这两天有个前同事总 ...
- MYSQL存储过程实现in传入参数 where in('1','2')
android 服务器端开发中遇到这么一个问题: 突然发现将字符串传入到存储过程,参数为 '1','2' ,竟然执行无效 所以看到网上有在存储过程中直接拼凑sql的代码,今天也试了一下,可以执行了, ...
- [转载]关于安装Android Studio的一些问题的解决方法
最近在研究Android编程,在Android Studio安装和使用时遇到了麻烦,从园子里找到了<关于安装Android Studio的一些问题的解决方法>的,很多问题找到了解决办法. ...
- Python random模块 例子
最近用到随机数,就查询资料总结了一下Python random模块(获取随机数)常用方法和使用例子. 1.random.random random.random()用于生成一个0到1的随机符点数: ...