Count of Smaller Numbers After Self
来源:互联网 发布:最新网络币 编辑:程序博客网 时间:2024/05/24 02:44
这串递归代码非常烧脑,先贴出来,慢慢研究,求逆序数,还可以用变形的mergesort:
class Node { int key; int smaller; int duplicates; Node left, right; public Node(int key, int smaller) { this.key = key; this.smaller = smaller; this.duplicates = 1; } } private Node put(Node root, int key, Integer[] answer, int index, int smaller) { if (root == null) { answer[index] = smaller; return new Node(key, 0); } if (key < root.key) { root.smaller++; root.left = put(root.left, key, answer, index, smaller); } else if (key > root.key) { root.right = put(root.right, key, answer, index, smaller + root.smaller + root.duplicates); } else { root.duplicates++; answer[index] = smaller + root.smaller; } return root; } public List<Integer> countSmaller(int[] nums) { int n = nums.length; Integer[] answer = new Integer[n]; Node root = null; for (int i = n - 1; i >= 0; i--) { root = put(root, nums[i], answer, i, 0); } return Arrays.asList(answer); }
0 0
- leetcode Count of Smaller Numbers After Self
- leetcode Count of Smaller Numbers After Self
- [LeetCode315]Count of Smaller Numbers After Self
- leetcode Count of Smaller Numbers After Self
- Count of Smaller Numbers After Self | LeetCode
- leetcode Count of Smaller Numbers After Self
- 315Count of Smaller Numbers After Self
- LeetCode Count of Smaller Numbers After Self
- [Leetcode]Count of Smaller Numbers After Self
- leetcode - Count of Smaller Numbers After Self
- leetcode:Count of Smaller Numbers After Self
- Count of Smaller Numbers After Self
- Count of Smaller Numbers After Self
- Count of Smaller Numbers After Self
- 315. Count of Smaller Numbers After Self
- 315. Count of Smaller Numbers After Self
- 【Leetcode】Count of Smaller Numbers After Self
- 315. Count of Smaller Numbers After Self
- 国内一线互联网公司内部面试题库
- 单词长度-week8-C语言习题集
- 127. Word Ladder
- 录音简单使用
- 使用Sax生成xml文件
- Count of Smaller Numbers After Self
- 七步从AngularJS菜鸟到专家(6):服务
- 实验项目2-13:两个有序序列的中位数
- Android自定义View自定义圆形Dialog
- MySQL优化之一--硬件和操作系统
- JAVA学习笔记--线程请求Json数据,并解析绑定到listView
- swipmenulistview+pulltorefresh整合
- 160722界面的颜色透明度渐变
- 链表相加