LeetCode 笔记26 Single Number II
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?
没辙,智商碾压题。楼主没遇到之前就只会这种做法。
public int singleNumber(int[] A) {
Map<Integer, Integer> counters = new HashMap<>();
for(int i = 0; i < A.length; i++) {
if (!counters.containsKey(A[i])) {
counters.put(A[i], 1);
} else {
counters.put(A[i], counters.get(A[i]) + 1);
}
} for (Integer key : counters.keySet()) {
if (counters.get(key) == 1) {
return key;
}
}
throw new RuntimeException("no single number");
}
而后我们知道可以先声明个长度32的整形数组,int[32]。第i个元素存放“这些整数第i位的1的个数除以三的余数”。好拗口是不是?
public int singleNumber2(int[] A) {
int[] counter = new int[32];
int ret = 0;
for(int i = 0; i < 32; i++) {
for (int j = 0; j < A.length; j++) {
counter[i] += (A[j] >> i) & 1;
}
ret |= (counter[i] % 3) << i;
}
return ret;
}
看了代码其实也不是“不明觉厉”。因为assume只有一个数出现一次,其他都出现3次,那么那个single number的第i位就是余下的那个数。
。。。
楼主中文不好。
btw,还有一个不是很直观的位操作方法。贴这里吧,不过没做过这个的感觉很难想到。
http://oj.leetcode.com/discuss/857/constant-space-solution
LeetCode 笔记26 Single Number II的更多相关文章
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】137. Single Number II (3 solutions)
Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【leetcode刷题笔记】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【一天一道LeetCode】#137. Single Number II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】137. Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- leetcode 笔记5 single number
question: Given an array of integers, every element appears twice except for one. Find that single o ...
- leetcode笔记:Ugly Number II
一. 题目描写叙述 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prim ...
- 【一天一道LeetCode】#260. Single Number III
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- Linux与Windows共享文件夹之samba的安装与使用(Ubuntu为例)
1.写在前面 当你在Windows上安装了一台Linux的虚拟机,你想访问Linux中的文件夹,将虚拟机中的文件复制到Windows主机上,你会怎么做呢?如果这台Linux主机不是虚拟机,而是 ...
- TCP面向连接网络编程
一 TCP&UDP协议 TCP,Tranfer Control Protocol,是一种面向连接的保证可靠传输的协议.通过TCP协议传输,得到的是一个顺序的无差错的数据流.发送方和接收方的成对 ...
- Effective Java 02 Consider a builder when faced with many constructor parameters
Advantage It simulates named optional parameters which is easily used to client API. Detect the inva ...
- 用java程序输出自己的姓名
代码部分: public class Hello { public static void main(String[] args) { System.out.println("$$$$$$$ ...
- .NET 创建Windows服务,及服务的安装卸载
.NET服务创建过程 http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html 相关命令(要以管理员身份打开cmd) 安装服务 -& ...
- spring mvc 配置文件拦截器过滤url
最近在用spring mvc拦截器,sprin 版本号4.0.6.RELEASE, <mvc:interceptor> <mvc:mapping path="/admin/ ...
- 一个初学者对于MVC架构的理解
我很早之前就开始接触.NET开发,一直都在2.0的框架下,所以对于MVC这种架构,听说过,但没有具体使用过,近期和外部朋友接触时,有了解到他们公司在使用MVC这种架构,所以自己就找来相关资料了解一下M ...
- LessonFifth Redis的持久化功能
#验证redis的快照和AOF功能 1.先验证RDB快照功能,由于AOF优先级高,先关闭,然后测试,截图如下 2.设置打开AOF 然后进行实验,截图如下: ...
- NOIP2007 T2纪念品分组 解题报告-S.B.S.
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...
- Verilog (二) multiplexer and decoder
1 mutiplexer 数据选择器 1) one-bit wide 2-1 mux wire dout = sel? din1 : din0; // conditional continuous ...