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

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

長(zhǎng)春網(wǎng)站建設(shè)58同城想在百度做推廣怎么做

長(zhǎng)春網(wǎng)站建設(shè)58同城,想在百度做推廣怎么做,做ppt好的網(wǎng)站有哪些內(nèi)容,網(wǎng)站建設(shè)主要問(wèn)題及建議1、題目描述 設(shè)計(jì)一個(gè)結(jié)構(gòu)包含如下三個(gè)方法: void add(int index, int num); //把num加入到index位置 int get(int index); //取出index位置的值(是自然序的index位置,非排序后) void remove(int index); //把index位置上的值刪…

1、題目描述

設(shè)計(jì)一個(gè)結(jié)構(gòu)包含如下三個(gè)方法:

void add(int index, int num); //把num加入到index位置
int get(int index); //取出index位置的值(是自然序的index位置,非排序后)
void remove(int index); //把index位置上的值刪除

要求三個(gè)方法時(shí)間復(fù)雜度 O(logN)O(logN)O(logN)

2、思路分析

ArrayList 中刪除一個(gè)元素需要將其后的元素全部往前移動(dòng)一位,時(shí)間復(fù)雜度為 O(N)O(N)O(N)LinkedList 中雖然刪除一個(gè)元素的時(shí)間復(fù)雜度很低O(1)O(1)O(1),但是要找到這個(gè)待刪除的元素得從頭開(kāi)始遍歷,所以整體時(shí)間復(fù)雜度仍然為 O(N)O(N)O(N)

腳本語(yǔ)言中使用的“數(shù)組”好像什么功能都能完成,且很高效,是因?yàn)樵摂?shù)組底層并不是單純的數(shù)組或雙鏈表,只是高度改進(jìn)后起名為“數(shù)組”。

題目補(bǔ)充說(shuō)明:add(int index, int num) 方法在 index 位置加入 num,意思是假設(shè)原數(shù)組是 [3, 5, 2, 4],如果在 1 位置加入 7,則數(shù)組變成 [3, 7, 5, 2, 4]。

使用有序表可以設(shè)計(jì)出題目要求的復(fù)雜度的三個(gè)方法的結(jié)構(gòu)。但是要注意:

  1. 為了區(qū)分值相同的兩個(gè)數(shù),在外面再封裝一層。也就是說(shuō)如果有多個(gè)相同的值,在樹(shù)上就會(huì)有多個(gè)值相同的節(jié)點(diǎn),通過(guò)內(nèi)存地址區(qū)分開(kāi)。每個(gè)節(jié)點(diǎn)記錄的size 就是平衡因子,即以該節(jié)點(diǎn)為根的樹(shù)上的節(jié)點(diǎn)個(gè)數(shù)。

  2. 改進(jìn)的有序表并不是按照 key 進(jìn)行排序的,而是按照自然時(shí)序。即當(dāng)前節(jié)點(diǎn)的左樹(shù)的自然時(shí)序都早于當(dāng)前節(jié)點(diǎn),右樹(shù)的自然時(shí)序都晚于當(dāng)前節(jié)點(diǎn)。

  3. 如果能維持一個(gè)以自然時(shí)序排列的樹(shù),無(wú)論左旋還是右旋,自然時(shí)序都維持正確,即不會(huì)改變這些數(shù)的相對(duì)次序。

  4. 對(duì)于一棵已經(jīng)生成的樹(shù),加入新的數(shù)后無(wú)論如何旋轉(zhuǎn)都不會(huì)改變它的相對(duì)次序,那么新加入的數(shù)應(yīng)該掛在樹(shù)的哪個(gè)位置上呢?

以自然時(shí)序排列組織的樹(shù) 以及 旋轉(zhuǎn)后相對(duì)次序沒(méi)有發(fā)生改變 舉例:

輸入的數(shù)依次為[5, 3, 5]
在這里插入圖片描述

再舉例addremove 操作:

在這里插入圖片描述
刪除4位置的數(shù)后的樹(shù)對(duì)應(yīng)的自然時(shí)序就是[7, 7, 3, 5, 6]。

3、代碼實(shí)現(xiàn)

import java.util.ArrayList;//本質(zhì)就是不去重版本的有序表/Size Balanced Tree
public class AddRemoveGetIndexGreat {//沒(méi)有key,因?yàn)閰⑴c排序的并不是key,而是隱含的自然時(shí)序public static class SBTNode<V> {public V value;public SBTNode<V> l;public SBTNode<V> r;public int size; //平衡因子,也參與業(yè)務(wù)public SBTNode(V v) {value = v;size = 1;}}public static class SbtList<V> {private SBTNode<V> root;private SBTNode<V> rightRotate(SBTNode<V> cur) {SBTNode<V> leftNode = cur.l;cur.l = leftNode.r;leftNode.r = cur;leftNode.size = cur.size;cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;return leftNode;}private SBTNode<V> leftRotate(SBTNode<V> cur) {SBTNode<V> rightNode = cur.r;cur.r = rightNode.l;rightNode.l = cur;rightNode.size = cur.size;cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;return rightNode;}private SBTNode<V> maintain(SBTNode<V> cur) {if (cur == null) {return null;}int leftSize = cur.l != null ? cur.l.size : 0;int leftLeftSize = cur.l != null && cur.l.l != null ? cur.l.l.size : 0;int leftRightSize = cur.l != null && cur.l.r != null ? cur.l.r.size : 0;int rightSize = cur.r != null ? cur.r.size : 0;int rightLeftSize = cur.r != null && cur.r.l != null ? cur.r.l.size : 0;int rightRightSize = cur.r != null && cur.r.r != null ? cur.r.r.size : 0;if (leftLeftSize > rightSize) {cur = rightRotate(cur);cur.r = maintain(cur.r);cur = maintain(cur);} else if (leftRightSize > rightSize) {cur.l = leftRotate(cur.l);cur = rightRotate(cur);cur.l = maintain(cur.l);cur.r = maintain(cur.r);cur = maintain(cur);} else if (rightRightSize > leftSize) {cur = leftRotate(cur);cur.l = maintain(cur.l);cur = maintain(cur);} else if (rightLeftSize > leftSize) {cur.r = rightRotate(cur.r);cur = leftRotate(cur);cur.l = maintain(cur.l);cur.r = maintain(cur.r);cur = maintain(cur);}return cur;}//root這棵樹(shù)上的index位置添加節(jié)點(diǎn)cur,這個(gè)cur一定不是重復(fù)的,因?yàn)榉庋b了一層,有不同的內(nèi)存地址private SBTNode<V> add(SBTNode<V> root, int index, SBTNode<V> cur) {if (root == null) {return cur;}//以root為根的樹(shù)上的節(jié)點(diǎn)個(gè)數(shù)加1,可以理解為與之前“區(qū)間和個(gè)數(shù)”問(wèn)題中的all數(shù)據(jù)項(xiàng)合并了root.size++; //左樹(shù)及根節(jié)點(diǎn)一共有多少個(gè)節(jié)點(diǎn)int leftAndHeadSize = (root.l != null ? root.l.size : 0) + 1; if (index < leftAndHeadSize) {root.l = add(root.l, index, cur);} else {root.r = add(root.r, index - leftAndHeadSize, cur); //在右樹(shù)上位于自然時(shí)序的第幾位}root = maintain(root);return root;}private SBTNode<V> remove(SBTNode<V> root, int index) {//找到要?jiǎng)h除的節(jié)點(diǎn)過(guò)程中的沿途節(jié)點(diǎn)的size都要減1root.size--;int rootIndex = root.l != null ? root.l.size : 0;if (index != rootIndex) {if (index < rootIndex) {root.l = remove(root.l, index);} else {root.r = remove(root.r, index - rootIndex - 1);}return root;}if (root.l == null && root.r == null) {return null;}if (root.l == null) {return root.r;}if (root.r == null) {return root.l;}SBTNode<V> pre = null;SBTNode<V> suc = root.r;suc.size--;while (suc.l != null) {pre = suc;suc = suc.l;suc.size--;}if (pre != null) {pre.l = suc.r;suc.r = root.r;}suc.l = root.l;suc.size = suc.l.size + (suc.r == null ? 0 : suc.r.size) + 1;return suc;}private SBTNode<V> get(SBTNode<V> root, int index) {int leftSize = root.l != null ? root.l.size : 0;if (index < leftSize) {return get(root.l, index);} else if (index == leftSize) {return root;} else {return get(root.r, index - leftSize - 1);}}//add方法:在index位置加入numpublic void add(int index, V num) {SBTNode<V> cur = new SBTNode<V>(num); //先封裝一層,以區(qū)分相同的numif (root == null) {root = cur;} else {if (index <= root.size) {root = add(root, index, cur);}}}public V get(int index) {SBTNode<V> ans = get(root, index);return ans.value;}public void remove(int index) {if (index >= 0 && size() > index) {root = remove(root, index);}}public int size() {return root == null ? 0 : root.size;}}// 通過(guò)以下這個(gè)測(cè)試,// 可以很明顯的看到LinkedList的插入、刪除、get效率不如SbtList// LinkedList需要找到index所在的位置之后才能插入或者讀取,時(shí)間復(fù)雜度O(N)// SbtList是平衡搜索二叉樹(shù),所以插入或者讀取時(shí)間復(fù)雜度都是O(logN)public static void main(String[] args) {// 功能測(cè)試int test = 50000;int max = 1000000;boolean pass = true;ArrayList<Integer> list = new ArrayList<>();SbtList<Integer> sbtList = new SbtList<>();for (int i = 0; i < test; i++) {if (list.size() != sbtList.size()) {pass = false;break;}if (list.size() > 1 && Math.random() < 0.5) {int removeIndex = (int) (Math.random() * list.size());list.remove(removeIndex);sbtList.remove(removeIndex);} else {int randomIndex = (int) (Math.random() * (list.size() + 1));int randomValue = (int) (Math.random() * (max + 1));list.add(randomIndex, randomValue);sbtList.add(randomIndex, randomValue);}}for (int i = 0; i < list.size(); i++) {if (!list.get(i).equals(sbtList.get(i))) {pass = false;break;}}System.out.println("功能測(cè)試是否通過(guò) : " + pass);// 性能測(cè)試test = 500000;list = new ArrayList<>();sbtList = new SbtList<>();long start = 0;long end = 0;start = System.currentTimeMillis();for (int i = 0; i < test; i++) {int randomIndex = (int) (Math.random() * (list.size() + 1));int randomValue = (int) (Math.random() * (max + 1));list.add(randomIndex, randomValue);}end = System.currentTimeMillis();System.out.println("ArrayList插入總時(shí)長(zhǎng)(毫秒) : " + (end - start));start = System.currentTimeMillis();for (int i = 0; i < test; i++) {int randomIndex = (int) (Math.random() * (i + 1));list.get(randomIndex);}end = System.currentTimeMillis();System.out.println("ArrayList讀取總時(shí)長(zhǎng)(毫秒) : " + (end - start));start = System.currentTimeMillis();for (int i = 0; i < test; i++) {int randomIndex = (int) (Math.random() * list.size());list.remove(randomIndex);}end = System.currentTimeMillis();System.out.println("ArrayList刪除總時(shí)長(zhǎng)(毫秒) : " + (end - start));start = System.currentTimeMillis();for (int i = 0; i < test; i++) {int randomIndex = (int) (Math.random() * (sbtList.size() + 1));int randomValue = (int) (Math.random() * (max + 1));sbtList.add(randomIndex, randomValue);}end = System.currentTimeMillis();System.out.println("SbtList插入總時(shí)長(zhǎng)(毫秒) : " + (end - start));start = System.currentTimeMillis();for (int i = 0; i < test; i++) {int randomIndex = (int) (Math.random() * (i + 1));sbtList.get(randomIndex);}end = System.currentTimeMillis();System.out.println("SbtList讀取總時(shí)長(zhǎng)(毫秒) :  " + (end - start));start = System.currentTimeMillis();for (int i = 0; i < test; i++) {int randomIndex = (int) (Math.random() * sbtList.size());sbtList.remove(randomIndex);}end = System.currentTimeMillis();System.out.println("SbtList刪除總時(shí)長(zhǎng)(毫秒) :  " + (end - start));}
}
http://aloenet.com.cn/news/44962.html

相關(guān)文章:

  • 國(guó)外做家譜的網(wǎng)站開(kāi)發(fā)小程序
  • 網(wǎng)站建設(shè)要學(xué)會(huì)編程嗎網(wǎng)站的營(yíng)銷(xiāo)推廣方案
  • 國(guó)外網(wǎng)站設(shè)計(jì)網(wǎng)站昆明百度推廣開(kāi)戶
  • wordpress 網(wǎng)頁(yè)目錄下湖南專(zhuān)業(yè)seo公司
  • 小貸網(wǎng)站需要多少錢(qián)可以做seo快速排名優(yōu)化方法
  • 做導(dǎo)航網(wǎng)站犯法嗎web網(wǎng)頁(yè)制作教程
  • 教師可以做網(wǎng)站嗎最近熱點(diǎn)新聞事件
  • 寧國(guó)做網(wǎng)站優(yōu)化營(yíng)商環(huán)境的措施建議
  • 網(wǎng)站的域名可以修改嗎做營(yíng)銷(xiāo)策劃的公司
  • 網(wǎng)站如何做口碑營(yíng)銷(xiāo)大數(shù)據(jù)
  • 專(zhuān)門(mén)做水果的網(wǎng)站重慶seo優(yōu)化效果好
  • wordpress底部插件超級(jí)seo助手
  • 可以免費(fèi)看日本黃片的app做網(wǎng)站上海單個(gè)關(guān)鍵詞優(yōu)化
  • 單頁(yè)面網(wǎng)站推廣重慶seo推廣運(yùn)營(yíng)
  • 優(yōu)化網(wǎng)站排名方法教程怎樣自己做網(wǎng)站
  • 武漢++外貿(mào)網(wǎng)站建設(shè)千瓜數(shù)據(jù)
  • 星空無(wú)限傳媒官網(wǎng)免費(fèi)下載seo服務(wù)收費(fèi)
  • 化妝品網(wǎng)站系統(tǒng)規(guī)劃58同城安居客
  • 南昌市有幫做網(wǎng)站的嗎作品提示優(yōu)化要?jiǎng)h嗎
  • 江蘇專(zhuān)業(yè)網(wǎng)站建設(shè)網(wǎng)絡(luò)營(yíng)銷(xiāo)站點(diǎn)推廣的方法
  • 門(mén)戶網(wǎng)站建設(shè)管理總則關(guān)鍵詞優(yōu)化排名查詢
  • 合肥做網(wǎng)站優(yōu)化哪家好建立網(wǎng)站需要什么條件
  • pc網(wǎng)站建設(shè)方案有哪些seo綜合排名優(yōu)化
  • 許昌做網(wǎng)站漢獅網(wǎng)絡(luò)網(wǎng)站片區(qū)
  • 公司網(wǎng)站開(kāi)發(fā)建設(shè)什么會(huì)計(jì)科目今日財(cái)經(jīng)最新消息
  • 手機(jī)網(wǎng)站建設(shè)價(jià)格低正規(guī)百度推廣
  • 深圳做網(wǎng)站推廣品牌推廣計(jì)劃書(shū)怎么寫(xiě)
  • 綿陽(yáng)市建設(shè)局官方網(wǎng)站軍事新聞俄烏最新消息
  • 網(wǎng)站建設(shè)費(fèi)科目外貿(mào)推廣具體是做什么
  • 手機(jī)微信網(wǎng)站怎么做的好淘寶直通車(chē)推廣怎么收費(fèi)