国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

惠民網(wǎng)站建設(shè)淺議網(wǎng)絡(luò)營銷論文

惠民網(wǎng)站建設(shè),淺議網(wǎng)絡(luò)營銷論文,口碑好的網(wǎng)頁設(shè)計服務(wù),百度聯(lián)盟網(wǎng)站一定要備案嗎文章目錄二叉樹的鋸齒形層序遍歷(樹、廣度優(yōu)先搜索)用棧實現(xiàn)隊列(棧、設(shè)計)買賣股票的最佳時機 IV(數(shù)組、動態(tài)規(guī)劃)二叉樹的鋸齒形層序遍歷(樹、廣度優(yōu)先搜索) 給定一個二叉樹&…

文章目錄

    • 二叉樹的鋸齒形層序遍歷(樹、廣度優(yōu)先搜索)
    • 用棧實現(xiàn)隊列(棧、設(shè)計)
    • 買賣股票的最佳時機 IV(數(shù)組、動態(tài)規(guī)劃)

二叉樹的鋸齒形層序遍歷(樹、廣度優(yōu)先搜索)

給定一個二叉樹,返回其節(jié)點值的鋸齒形層序遍歷。(即先從左往右,再從右往左進行下一層遍歷,以此類推,層與層之間交替進行)。
例如:
給定二叉樹 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回鋸齒形層序遍歷如下:
[ [3], [20,9], [15,7] ]

解答:

public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}
}
class Solution {public List<List<Integer>> zigzagLevelOrder(TreeNode root) {List<List<Integer>> list = new LinkedList<>();if (root == null) {return list;}Stack<TreeNode> stack1 = new Stack<>();stack1.push(root);boolean postive = true;while (!stack1.isEmpty()) {Stack<TreeNode> stack2 = new Stack<>();List<Integer> subList = new LinkedList<>();while (!stack1.isEmpty()) {TreeNode current = stack1.pop();subList.add(current.val);if (postive) {if (current.left != null) {stack2.push(current.left);}if (current.right != null) {stack2.push(current.right);}} else {if (current.right != null) {stack2.push(current.right);}if (current.left != null) {stack2.push(current.left);}}}postive = !postive;stack1 = stack2;list.add(subList);}return list;}
}

用棧實現(xiàn)隊列(棧、設(shè)計)

請你僅使用兩個棧實現(xiàn)先入先出隊列。隊列應(yīng)當(dāng)支持一般隊列支持的所有操作(push、pop、peek、empty):
實現(xiàn) MyQueue 類:

  • void push(int x) 將元素 x 推到隊列的末尾
  • int pop() 從隊列的開頭移除并返回元素
  • int peek() 返回隊列開頭的元素
  • boolean empty() 如果隊列為空,返回 true ;否則,返回 false

說明:

  • 你只能使用標(biāo)準(zhǔn)的棧操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
  • 你所使用的語言也許不支持棧。你可以使用 list 或者 deque(雙端隊列)來模擬一個棧,只要是標(biāo)準(zhǔn)的棧操作即可。

進階:

  • 你能否實現(xiàn)每個操作均攤時間復(fù)雜度為 O(1) 的隊列?換句話說,執(zhí)行 n 個操作的總時間復(fù)雜度為 O(n) ,即使其中一個操作可能花費較長時間。

示例:

輸入: ["MyQueue", "push", "push", "peek", "pop", "empty"] [[], [1], [2], [], [], []] 輸出: [null, null, null, 1, 1, false] 
解釋: MyQueue myQueue = new MyQueue(); myQueue.push(1); // queue is: [1] myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) myQueue.peek(); // return 1 myQueue.pop(); // return 1, queue is [2] myQueue.empty(); // return false

提示:

  • 1 <= x <= 9
  • 最多調(diào)用 100 次 push、pop、peek 和 empty
  • 假設(shè)所有操作都是有效的 (例如,一個空的隊列不會調(diào)用 pop 或者 peek 操作)

解答:

class MyQueue {Stack<Integer> s1;Stack<Integer> s2;/** Initialize your data structure here. */public MyQueue() {s1 = new Stack<Integer>();s2 = new Stack<Integer>();}/** Push element x to the back of queue. */public void push(int x) {while (!s1.empty())s2.push(s1.pop());s1.push(x);while (!s2.empty())s1.push(s2.pop());return;}/** Removes the element from in front of queue and returns that element. */public int pop() {return s1.pop();}/** Get the front element. */public int peek() {int ret = s1.pop();s1.push(ret);return ret;}/** Returns whether the queue is empty. */public boolean empty() {return s1.empty();}
}
/*** Your MyQueue object will be instantiated and called as such:* MyQueue obj = new MyQueue();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.peek();* boolean param_4 = obj.empty();*/

買賣股票的最佳時機 IV(數(shù)組、動態(tài)規(guī)劃)

給定一個整數(shù)數(shù)組 prices ,它的第_ i 個元素 prices[i] 是一支給定的股票在第 i _天的價格。
設(shè)計一個算法來計算你所能獲取的最大利潤。你最多可以完成 k 筆交易。
**注意:**你不能同時參與多筆交易(你必須在再次購買前出售掉之前的股票)。

示例 1:
輸入:k = 2, prices = [2,4,1] 輸出:2 解釋:在第 1 天 (股票價格 = 2) 的時候買入,在第 2 天 (股票價格 = 4) 的時候賣出,這筆交易所能獲得利潤 = 4-2 = 2 。
示例 2:
輸入:k = 2, prices = [3,2,6,5,0,3] 輸出:7 解釋:在第 2 天 (股票價格 = 2) 的時候買入,在第 3 天 (股票價格 = 6) 的時候賣出, 這筆交易所能獲得利潤 = 6-2 = 4 。 隨后,在第 5 天 (股票價格 = 0) 的時候買入,在第 6 天 (股票價格 = 3) 的時候賣出, 這筆交易所能獲得利潤 = 3-0 = 3 。

提示:

  • 0 <= k <= 100
  • 0 <= prices.length <= 1000
  • 0 <= prices[i] <= 1000

解答:

class Solution {public int maxProfit(int k, int[] prices) {if (k < 1)return 0;if (k >= prices.length / 2)return greedy(prices);int[][] t = new int[k][2];for (int i = 0; i < k; ++i)t[i][0] = Integer.MIN_VALUE;for (int p : prices) {t[0][0] = Math.max(t[0][0], -p);t[0][1] = Math.max(t[0][1], t[0][0] + p);for (int i = 1; i < k; ++i) {t[i][0] = Math.max(t[i][0], t[i - 1][1] - p);t[i][1] = Math.max(t[i][1], t[i][0] + p);}}return t[k - 1][1];}private int greedy(int[] prices) {int max = 0;for (int i = 1; i < prices.length; ++i) {if (prices[i] > prices[i - 1])max += prices[i] - prices[i - 1];}return max;}
}

本文內(nèi)容到此結(jié)束了,
如有收獲歡迎點贊👍收藏💖關(guān)注??,您的鼓勵是我最大的動力。
如有錯誤?疑問💬歡迎各位指出。
主頁:共飲一杯無的博客匯總👨?💻

保持熱愛,奔赴下一場山海。🏃🏃🏃

http://m.aloenet.com.cn/news/36752.html

相關(guān)文章:

  • 休閑旅游產(chǎn)品營銷網(wǎng)站的建設(shè)策略app運營
  • 深圳高端網(wǎng)站開發(fā)網(wǎng)絡(luò)營銷的優(yōu)勢有哪些?
  • 外貿(mào)網(wǎng)站推廣實操手冊2023年新聞小學(xué)生摘抄
  • 怎么建立自己的網(wǎng)站平臺多少錢南寧seo優(yōu)化公司排名
  • 黃江鎮(zhèn)做網(wǎng)站在線生成個人網(wǎng)站源碼
  • 自己做網(wǎng)站 發(fā)布視頻北京自動seo
  • 建設(shè)部舉報網(wǎng)站西安網(wǎng)站建設(shè)方案優(yōu)化
  • 凡科建站可以多人協(xié)作編輯嗎北京百度快照推廣公司
  • 渭南做網(wǎng)站的公司河北高端網(wǎng)站建設(shè)
  • 網(wǎng)站開發(fā)人員要求科技網(wǎng)站建設(shè)公司
  • 綁定網(wǎng)站品牌策劃與推廣方案
  • 建一個購物網(wǎng)站需要多少錢邯鄲今日頭條最新消息
  • 免費個人網(wǎng)站建設(shè)可口可樂軟文營銷案例
  • 惠州網(wǎng)站開發(fā)公司網(wǎng)頁
  • 建網(wǎng)站怎么避免備案百度推廣課程
  • 可以做哪些網(wǎng)站我想自己建立一個網(wǎng)站
  • 做網(wǎng)站怎么找優(yōu)質(zhì)客戶軟文寫作技巧有哪些
  • 公安內(nèi)網(wǎng)網(wǎng)站建設(shè)方案站群seo
  • 個人網(wǎng)站備案需要哪些資料網(wǎng)站單向外鏈推廣工具
  • 做網(wǎng)站 單頁數(shù)量網(wǎng)絡(luò)營銷的步驟
  • 網(wǎng)站建設(shè)費用細(xì)項廣州seo推廣公司
  • 中英網(wǎng)站的設(shè)計app開發(fā)費用一覽表
  • 上海網(wǎng)站建設(shè)百家號廣告投放推廣平臺
  • 網(wǎng)站建設(shè)石家莊今天國際新聞大事
  • 制作網(wǎng)站接單seo關(guān)鍵詞排名如何
  • 有哪些做買家秀的網(wǎng)站企業(yè)營銷平臺
  • 酒店網(wǎng)站策劃書網(wǎng)站打開
  • cms系統(tǒng)javaseo快速排名軟件app
  • 網(wǎng)新科技做網(wǎng)站怎么樣武漢百度seo排名
  • 專業(yè)營銷網(wǎng)站費用企業(yè)seo關(guān)鍵字優(yōu)化