[leetcode]739. Daily Temperatures

用stack实现一个递增序列还是很常用的,记住stack一般存数组元素的下标

/*
        用stack实现一个递增序列,每拿到一个新数就遍历stack,把比新数小的都整理好
        stack存的是下标,如果存数就不能存下标,但是存下标的话可以一举两得,因为可以用下标索引数
        这种方法还是很常用的
         */
        int[] res = new int[temperatures.length];
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < temperatures.length; i++) {
            int a = temperatures[i];
            while (!stack.isEmpty()&&a>temperatures[stack.peek()])
            {
                    int index = stack.pop();
                    res[index] = i-index;
            }
            stack.push(i);
        }
        return res;

 

posted @ 2018-01-22 19:14  stAr_1  阅读(213)  评论(0编辑  收藏  举报