日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > 【Leetcode】Count of Smaller Numbers After Self

【Leetcode】Count of Smaller Numbers After Self

來源:程序員人生   發布時間:2016-06-20 17:00:05 閱讀次數:2580次

題目鏈接:https://leetcode.com/problems/count-of-smaller-numbers-after-self/

題目:

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].

Example:

Given nums = [5, 2, 6, 1] To the right of 5 there are 2 smaller elements (2 and 1). To the right of 2 there is only 1 smaller element (1). To the right of 6 there is 1 smaller element (1). To the right of 1 there is 0 smaller element.

Return the array [2, 1, 1, 0].

思路:

建立1個2叉搜索樹,在建樹的進程中,記錄在數組右側比本身小的元素個數。 算法復雜度O(nlogn),用動態計劃復雜度為O(n^2)。

其中cnt為該結點相同大小元素的個數,用于重復元素判斷。。。其實可以省略。。懶得改了

算法:

public List<Integer> countSmaller(int[] nums) { List<Integer> list = new ArrayList<Integer>(); int res[] = new int[nums.length]; for (int i = nums.length - 1; i >= 0; i--) { res[i] = insert(nums[i]); } for (int i : res) { list.add(i); } return list; } TreeNode tRoot; private Integer insert(int val) { int cnt = 0; if (tRoot == null) { tRoot = new TreeNode(val); return cnt; } TreeNode root = tRoot; while (root != null) { if (val < root.val) { root.leftCnt++; if (root.left == null) { root.left = new TreeNode(val); break; } else root = root.left; } else if (val > root.val) { cnt += root.leftCnt + root.cnt; if (root.right == null) { root.right = new TreeNode(val); break; } else root = root.right; } else { cnt += root.leftCnt; root.cnt++; break; } } return cnt; }


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产在线网站 | 国产成人无遮挡在线视频 | 91精品国产91久久久 | 在线一区二区视频 | 日本午夜精品视频 | 亚洲色图 偷拍自拍 | 成年人视频免费在线观看 | 亚洲欧洲无码一区二区三区 | 免费a级 | 成人在线观看免费 | 97在线视频观看 | 国产精品久久久一区二区三区 | 99麻豆久久久国产精品免费 | 欧美日韩免费一区二区三区 | 欧美在线播放 | 欧美 日韩 国产 成人 在线 91 | 国产一区二区三区日韩 | 久久成人在线 | 不卡的av在线播放 | 午夜精彩视频 | 国产激情精品一区二区三区 | 久久国产精品久久久久久久久久 | 久久福利一区二区 | 在线二区 | 中文字幕不卡在线 | 视频一区在线播放 | 国产福利免费视频 | 精品久久中文字幕 | 日韩精品激情 | 亚洲精品欧洲 | 一区二区日韩精品 | 少妇又紧又色又爽又刺激视频 | 国产a一区二区 | 天天综合网日日夜夜 | 欧美日韩国 | 在线视频区 | 日本国产一区二区三区 | 九九视频网 | 在线中文字幕av | 91久久精品国产 | 国产综合精品一区二区三区 |