【LeetCode OJ】Single Number II
来源:互联网 发布:数据分解传输技术 编辑:程序博客网 时间:2024/05/24 06:06
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?
java code : O(n)时间复杂度, O(n)空间复杂度 : 哈希
public class Solution { public int singleNumber(int[] A) { // Note: The Solution object is instantiated only once and is reused by each test case. if(A.length == 0) return 0; HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();for(int a : A){if(hash.containsKey(a)){hash.put(a, hash.get(a) + 1);}else hash.put(a, 1);}for(int key : hash.keySet()){if(hash.get(key) == 1)return key;}return 0; }}底下转载 http://www.cnblogs.com/feiling/p/3351379.html O(n) 时间复杂度,O(1)空间复杂度
public class Solution { public int singleNumber(int[] A) { // Note: The Solution object is instantiated only once and is reused by each test case. if(A.length == 0) return 0; int[] cnt = new int[32]; for(int i = 0; i < A.length; i++) { for(int j = 0; j < 32; j++) { if( (A[i]>>j & 1) ==1) { cnt[j] = (cnt[j] + 1)%3; } } } int res = 0; for(int i = 0; i < 32; i++) { res += (cnt[i] << i); } cnt = null; return res; }}
- 【LeetCode OJ】Single Number II
- [LeetCode OJ] Single Number II
- [LeetCode OJ]Single number II
- LeetCode OJ Single Number II
- LeetCode OJ : Single Number II
- LeetCode OJ——Single Number II
- [LeetCode OJ]Single Number
- LeetCode OJ:Single Number
- [LeetCode OJ] Single Number
- [LeetCode OJ]Single Number
- LeetCode OJ Single Number
- Single Number II - leetcode
- Leetcode: Single Number II
- [LeetCode] Single Number II
- LeetCode: Single Number II
- leetcode -- Single Number II
- [leetcode]Single Number II
- [LeetCode] Single Number II
- squid,nginx,lighttpd反向代理的区别
- 如何在任务栏添加托盘图标
- MySQL数据库的注释
- 打造专利信息检索平台 服务创新型国家建设
- 小白
- 【LeetCode OJ】Single Number II
- 11gr2 oracle concepts(翻译) --第九章 数据并发和一致性
- #pragma once&&#ifndef解释:头文件只被编译一次
- JNI学习笔记
- 图形操作类CBitmap 把内存数据输出到PIC控件
- 攻克一维二维数组以及冒泡等各种排序思维,进一步了解字符数组级string的数据
- C语言编程
- yum故障最终解决办法.移魂版 bash:yum:command not found
- 数组,ArrayList,List的区别