多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

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

【Leetcode】Count of Smaller Numbers After Self

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

題目鏈接: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; }


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 手机在线一区二区三区 | 欧美精品成人久久网站 | 国产亚洲视频网站 | 图片区小说区校园小说 | 欧美激情精品久久久久 | 成人自拍视频网站 | 亚洲三级视频在线 | 农村妇女又色黄一级毛片 | 欧美日韩亚洲国产一区二区三区 | 亚洲精品欧美精品日韩精品 | 尤物在线视频 | 欧美巨大video粗暴 | 国内成人免费视频 | 久久在精品线影院精品国产 | 亚洲最大成人在线 | 日韩 亚洲 中文 图片 小说 | 天堂入口| 在线免费成人网 | 亚洲一区二区三区高清 不卡 | 日本欧美午夜 | 伊人久久亚洲精品一区 | 成人亚洲精品一区 | 国产日韩精品欧美一区喷 | 日韩手机在线免费视频 | 亚洲欧美日韩国产一区二区精品 | 暖暖在线精品日本中文 | 国产一区二区三区在线免费观看 | 久久久夜 | 欧美最猛黑人xxxx黑人猛交98 | 波多野结衣视频在线免费观看 | 亚洲小说区图片区另类春色 | 亚洲欧美日韩国产精品网 | 免费高清不卡毛片在线看 | 国产免费叼嘿在线观看 | 精品欧美一区二区三区四区 | free中国性xxxxhd | 日本欧美一区二区三区在线观看 | 亚洲欧洲精品久久 | 欧美一级鲁丝片 | 欧美性猛交黑人 | 精品久久中文网址 |