日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[剑指offer]面试题第[59-2]题[JAVA][队列的最大值][暴力][双端队列]

發(fā)布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [剑指offer]面试题第[59-2]题[JAVA][队列的最大值][暴力][双端队列] 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

【問題描述】[中等]

【解答思路】

1. 暴力

復雜度分析

class MaxQueue {Queue<Integer> queue = new LinkedList();int maxValue;public MaxQueue() {queue = new LinkedList();}public int max_value() {if(queue.isEmpty()) return -1;return maxValue;}public void push_back(int value) {queue.offer(value);if(value > maxValue) maxValue = value;}public int pop_front() {if(queue.isEmpty()) return -1;int ans = queue.poll();if(ans == maxValue){int size = queue.size();if(size == 0){maxValue = Integer.MIN_VALUE;return ans;}maxValue = queue.peek();for(int i = 0; i<size; i++)this.push_back(queue.poll());}return ans;} }
2. 維護一個單調(diào)的雙端隊列


時間復雜度:O(1) 空間復雜度:O(N)

public class MaxQueue {Queue<Integer> queue;LinkedList<Integer> max;public MaxQueue() {queue = new LinkedList<>();max = new LinkedList<>();//LinkedList是雙端鏈表}public int max_value() {return max.size()==0?-1:max.getFirst();}public void push_back(int value) {queue.add(value);while(max.size()!=0&&max.getLast()<value){//注意:這里第二個判斷條件不能帶等號,即max中對于當前queue中的具有相同值的元素會全部存儲,而不是存儲最近的那個。max.removeLast();}max.add(value);}public int pop_front() {if(max.size()!=0&&queue.peek().equals(max.getFirst()))//Integer類型的值的比較不能直接使用==max.removeFirst();return queue.size()==0?-1:queue.poll();}/*** Your MaxQueue object will be instantiated and called as such:* MaxQueue obj = new MaxQueue();* int param_1 = obj.max_value();* obj.push_back(value);* int param_3 = obj.pop_front();*/ }

【總結】

1.ArrayList與LinkedList對比

ArrayList是順序結構,所以定位很快,但插入,刪除數(shù)據(jù)慢。
LinkedList 是鏈表結構,定位慢,但插入,刪除數(shù)據(jù)快。

2.ArrayList與LinkedList常見方法

ArrayList實現(xiàn)了List接口,常見方法有:
add(); contains(); get(); indexOf():定位對象所處的位置; remove(); size(); toArray(); toString();//轉換為字符串

LinkedList也實現(xiàn)了List接口外,可以實現(xiàn)上述ArrayList中的常用方法,此外:
1.LinkedList還實現(xiàn)了雙向鏈表結構Deque,可以很方便的在頭尾插入刪除數(shù)據(jù)。
LinkedList link = new LinkedList<>();
常用方法:
addFirst(); addLast();
getFirst(); getLast();
removeFirst(); removeLast();

2.LinkedList除了實現(xiàn)了List和Deque外,還實現(xiàn)了Queue接口(隊列),
Queue是先進先出隊列 FIFO
Queue< class > queue = new LinkedList<>();
常用方法:
poll()取出第一個元素;
peek()查看第一個元素;
offer()在最后添加元素,可用add()替換;
擴展
先進后出FILO Stack棧
Stack stack = new Stack<>();
常用方法:
push();可用add();代替
pop();輸出末尾的元素相當于LinkedList中的removeLast();
peek();查看最后一個元素,相當于getLast();

3.做題需要不斷總結整理歸納

建議和[劍指offer][JAVA]面試題第[09]題[用兩個棧實現(xiàn)隊列]LinkedList(https://blog.csdn.net/dadongwudi/article/details/106478719)一起學習

參考鏈接:https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/solution/mian-shi-ti-59-ii-javashi-xian-yuan-li-he-mian-shi/
參考鏈接:https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/solution/qi-shi-bu-tai-li-jie-jun-tan-fu-za-du-by-acnesu/
參考鏈接:https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/solution/mian-shi-ti-59-ii-dui-lie-de-zui-da-zhi-by-leetcod/

總結

以上是生活随笔為你收集整理的[剑指offer]面试题第[59-2]题[JAVA][队列的最大值][暴力][双端队列]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。