Count of Smaller Numbers After Self
来源:互联网 发布:计算机自动编程 编辑:程序博客网 时间:2024/06/06 11:46
首先吐槽下CSDN,写完,发表,直接没了,辛苦写的东西就这么不见了……
You are given an integer array nums and you have to return a new counts array. Thecounts 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]
.
class BinarySearchTreeNode{BinarySearchTreeNode leftChildren;BinarySearchTreeNode rightChildren;int value;int num;public BinarySearchTreeNode(int v){this.value = v;num = 1;}}public int insert(int v, BinarySearchTreeNode node){int re = 0;while(true){if(v < node.value){node.num ++;if(node.leftChildren == null){node.leftChildren = new BinarySearchTreeNode(v);break;}node = node.leftChildren;}else{node.num ++;// v >= node.value,加上左子树以及node本身re += getNum(node.leftChildren);if(node.value != v)re++;if(node.rightChildren == null){node.rightChildren = new BinarySearchTreeNode(v);break;}node = node.rightChildren;}}return re;}private int getNum(BinarySearchTreeNode node){if(node == null)return 0;elsereturn node.num;}public List<Integer> countSmaller(int[] nums) { LinkedList<Integer> reList = new LinkedList<Integer>(); if(nums == null || nums.length == 0) return reList; reList.addFirst(0); BinarySearchTreeNode root = new BinarySearchTreeNode(nums[nums.length-1]); for(int i=nums.length-2; i >= 0; i--){ int el = nums[i]; int re = insert(el,root); reList.addFirst(re); } return reList;}
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
- 继承Thread的LooperThread
- 操作系统raid
- F
- CI之路--junit学习笔记
- 【Git+Source Tree使用教程之二】Branching & Merging
- Count of Smaller Numbers After Self
- Android中Parcelable接口用法
- 数据结构实验报告1
- Android杂记(4)获取ViewPager中Fragment的实例
- fetchxml分页和QueryExpression分页
- 十五、访问者设计模式
- 动态规划——图像压缩
- 1006 Problem F
- 多线程多任务断点下载