LeetCode之数据流中第一个唯一的数字

image 

使用一个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);
    }
    
};
原文地址:https://www.cnblogs.com/cc11001100/p/7811988.html