您的位置:首页 > 其它

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);
}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: