leetcode -- Single Number II
2013-10-05 14:43
288 查看
http://www.cnblogs.com/feiling/p/3351379.html
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?
[解题思路]
人生最悲催的事是刚面完试,leetcode就把我面试的题目给刷出来了。。。。
1.O(n) time complexity O(n) space complexity count the ocurrence of every number
![](http://common.cnblogs.com/images/copycode.gif)
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?
[解题思路]
人生最悲催的事是刚面完试,leetcode就把我面试的题目给刷出来了。。。。
1.O(n) time complexity O(n) space complexity count the ocurrence of every number
![](http://common.cnblogs.com/images/copycode.gif)
1 public class Solution { 2 public int singleNumber(int[] A) { 3 // Note: The Solution object is instantiated only once and is reused by each test case. 4 int N = A.length; 5 if(N == 0){ 6 return N; 7 } 8 9 Map<Integer, Integer> counts = new HashMap<Integer, Integer>(); 10 for(int i = 0; i < N; i++){ 11 if(counts.containsKey(A[i])){ 12 counts.put(A[i], counts.get(A[i]) + 1); 13 } else { 14 counts.put(A[i], 1); 15 } 16 } 17 18 Iterator<Map.Entry<Integer, Integer>> iterator = counts.entrySet().iterator(); 19 while(iterator.hasNext()){ 20 Map.Entry<Integer, Integer> entry = iterator.next(); 21 if(entry.getValue() != 3){ 22 return entry.getKey(); 23 } 24 } 25 return 0; 26 } 27 }
![](http://common.cnblogs.com/images/copycode.gif)
相关文章推荐
- navicat for mysql 10.1.7注册码
- MySQL插入数据之INSERT
- 从总线模型看USB架构
- Segment Tree, Interval Tree,Range Tree and Binary Indexed Tree
- P2P中的NAT穿越方案简介
- PHP -- 字符串
- 黑马程序员————————C#基础知识之面向对象(一)
- 自己动手实现Android平台手写体数字识别之概述和训练集准备
- 顺序存储链表 C语言实现
- Gson学习
- 随便记几个函数
- 20131005完成第四,五章程序同学经常会犯得错误。
- 数据挖掘聚类算法之K-MEDOIDS
- Bell数
- CxGrid 的加强右键菜单实现方法
- Oracle死锁查询及处理
- 1208. Legendary Teams Contest(dfs)
- 如何在64位win7中使用未有签名的驱动程序
- cocos2d-x游戏开发(十七)NDK+ant编译暗黑世界
- UBuntu安装配置记录