zoukankan      html  css  js  c++  java
  • 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);
        }
        
    };
  • 相关阅读:
    购物菜单
    增删改查
    第七次Android
    第七次作业
    第四次作业
    第二次作业
    第七次
    第二次作业
    第三次作业
    第六周安卓作业
  • 原文地址:https://www.cnblogs.com/cc11001100/p/7811988.html
Copyright © 2011-2022 走看看