【Leetcode】Single Number III
2015-12-01 10:56
351 查看
题目链接:https://leetcode.com/problems/single-number-iii/
题目:
Given an array of numbers
the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given
Note:
The order of the result is not important. So in the above example,
also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
思路:
1、用HashMap,空间复杂度O(n)
2、用异或,具体参考百度。。我不会。。
算法1:
题目:
Given an array of numbers
nums, in which exactly two elements appear only once and all
the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given
nums = [1, 2, 1, 3, 2, 5], return
[3, 5].
Note:
The order of the result is not important. So in the above example,
[5, 3]is
also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
思路:
1、用HashMap,空间复杂度O(n)
2、用异或,具体参考百度。。我不会。。
算法1:
public int[] singleNumber(int[] nums) { Map<Integer, Integer> maps = new HashMap<Integer, Integer>(); for (int i : nums) { if (maps.containsKey(i)) { maps.remove(i); } else { maps.put(i, 1); } } Set<Integer> sets = maps.keySet(); Iterator<Integer> it =sets.iterator(); int []a = new int[sets.size()]; int i=0; while(it.hasNext()){ a[i++] = it.next(); } return a; }
相关文章推荐
- TCP/UDP测试工具大全
- 解决在IE中获取数据的缓存问题,运行环境为node.js
- 深入理解IOS布局和view加载显示
- SpringMVC 拦截器实现原理和登录实现
- Linux下获取虚拟地址对应的物理地址的方法
- LVM快照备份与恢复
- springMVC重定向
- intanceof以及引出的__proto__和prototype
- TextView相关
- 计算机网络——零碎知识点
- 网络传输基础
- 全面解析Bootstrap弹窗的实现方法
- Java streamtool
- 面试算法——2.双栈实现队列
- bat脚本调用python
- Hive 动态分区 & 静态分区
- 控制UI布局的方式
- 如何使用slice,substr代替substring(原创)
- ubuntu下mysql 开启远程连接
- 每天一个linux命令(18):find 命令概览