Leetcode315——Count of Smaller Numbers After Self
来源:互联网 发布:mac开发工具 编辑:程序博客网 时间:2024/05/18 10:04
终于在bst标签下找到一个lz能用bst做的出来的题目,泪目跪谢感谢扶起了我的自尊心,直接上代码,很简单的,就是逆序对的求法。
class Solution {public: struct node{ int num, count; node *l, *r; node(int num): num(num), count(0), l(NULL), r(NULL) {} }; int Insert(node *&root, int num){ int ans = 0; if(root == NULL) root = new node(num); else{ if(num >= root->num){ ans += num == root->num? root->count : root->count + 1; ans += Insert(root->r, num); } else{ root->count++; ans += Insert(root->l, num); } } return ans; } vector<int> countSmaller(vector<int>& nums) { int n = (int)nums.size(); vector<int> ans(n, 0); node* root = NULL; for(int i = n-1; i>=0; i--){ ans[i] = Insert(root, nums[i]); } return ans; }};
阅读全文
0 0
- Leetcode315——Count of Smaller Numbers After Self
- [LeetCode315]Count of Smaller Numbers After Self
- leetcode315. Count of Smaller Numbers After Self
- LeetCode315—Count of Smaller Numbers After Self—Java版归并算法
- LeetCode—315 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 | 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
- CCF-201412-1-门禁系统
- leetcode 73. Set Matrix Zeroes
- 数据分析与软件
- WordBreakII_140
- 深入理解ES6箭头函数的this
- Leetcode315——Count of Smaller Numbers After Self
- mysql数据类型和常用字段
- HDU 6201 transaction transaction transaction(树形dp)
- 关于APP在小米5s第一次安装启动后,点击home返回桌面,再次进入重进闪屏页问题
- 浅析对象关系映射
- JSTL中foreach的使用与总结
- rails 常用命令
- Gradle学习系列之八——构建多个Project
- C#——快速排序