LeetCode之数据流中第一个唯一的数字
2017-11-09 23:20
531 查看
使用一个Map维护数字出现的次数,使用一个链表维护只出现一次的数,使用一个变量记录是否找到过终止数字。
AC代码:
public class Solution { /* * @param : a continuous stream of numbers * @param : a number * @return: returns the first unique number */ public int firstUniqueNumber(int[] nums, int number) { Map<Integer, Integer> count = new HashMap<>(); List<Integer> once = new LinkedList<>(); boolean isFindStopNumber = false; for(int i : nums){ Integer c = count.get(i); if(c==null){ count.put(i, 1); once.add(i); }else{ count.put(i, c+1); once.remove(Integer.valueOf(i)); } if(i==number){ isFindStopNumber = true; break; } } if(!isFindStopNumber || once.isEmpty()) return -1; else return once.get(0); } };
相关文章推荐
- 数据流中第一个唯一的数字
- lintcode--数据流中第一个唯一的数字
- 算法爱好者——数据流中第一个唯一的数字 ? 待解决
- leetcode--TWO SUM延伸(假设答案不唯一、假设输入有相同数字)
- leetcode-字符串中的第一个唯一字符
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
- [LeetCode] 387. First Unique Character in a String 字符串的第一个唯一字符
- Leetcode 387. First Unique Character in a String 第一个唯一字符 解题报告
- 【leetcode】 字符串中的第一个唯一字符
- 剑指offer — 数据流中第一个不重复的数字
- LeetCode 352. Data Stream as Disjoint Intervals(数据流区间)
- C# GUID转换成16位字符串或19位数字并确保唯一
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
- leetcode:Remove Duplicates from Sorted Array(去掉数组重复数字,常数空间限制)【面试算法题】
- 找寻字符串中的第一个唯一字符串
- leetcode解题之153&154. Find Minimum in Rotated Sorted Array版(在旋转的数组中查找最小数字)
- LeetCode 7. Reverse Integer 倒序输出数字
- Leetcode-190 Reverse Bits 数字二进制倒置
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
- LeetCode 7. Reverse Integer (倒转数字)