【Leetcode】315. Count of Smaller Numbers After Self
来源:互联网 发布:什么软件可以看回看 编辑:程序博客网 时间:2024/06/05 16:25
题目:
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].
分析:
这道题的要求是:输入一个整型数组nums,返回一个新的数组,新数组的第i位的值为nums的第i位右边小于该位的数组元素的个数。
最简单的当然是把数组中的每一位元素的右边全部遍历一遍,不过这样复杂度为O(n^2),肯定会超时。所以我的想法是使用二叉搜索树来求解这个问题。
我的思路是创建一个二叉搜索树来存储整个数组,每个结点存储一个元素,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 其中在每个结点中用变量count来记录在结点左侧的结点数。
在插入函数的参数中添加一个引用参数cc用来记录插入元素右边小于该元素的数组元素的个数。
二叉搜索树的插入函数通过递归实现: 若当前结点root为空,则将该元素插入该结点,并返回;若该元素的值大于root数据域的值,则表示该元素大于当前结点和其左边的全部结点,所以将cc值加上当前结点的count再加1,然后查找其右子树;若该元素的值小于当前结点数据域的值,则表示该元素小于当前结点,即当前结点的左边又多了一个结点,当前结点的count加1,然后查找其左子树。
程序实现的步骤如下:
(1)将数组中最右边的那个元素作为二叉搜索树的根结点
(2)通过一个for循环从右边数的第二个元素开始调用插入函数insertnode,通过一个引用变量cc来记录该元素右边小于它的元素个数。
(3)将每次得到的cc压入栈中
(4)在遍历结束后,通过一个for循环将栈中的值插入向量result中
(5)在result的最后一位插入0,然后返回result。
该算法的最坏情况时间复杂度仍然为O(n^2),但是最后情况发生的概率比较小。
代码:
struct node{ int data; int count; node *left,*right; node(int d = 0,int c = 0,node *l = NULL,node *r = NULL): data(d),count(c),left(l),right(r){}};void insertnode(node*&root,int num,int &cc){ if(root == NULL) { root = new node(num); return; } else if(num>root->data) { cc += root->count+1; insertnode(root->right,num,cc); } else { root->count++; insertnode(root->left,num,cc); }}class Solution {public: vector<int> countSmaller(vector<int>& nums) { vector<int> result; result.clear(); if(nums.size() == 0) return result; node *root = new node(nums[nums.size()-1]); node *p = root; stack<int> st; int cc; for(int i = nums.size()-2;i>=0;i--) { cc = 0; p = root; insertnode(p,nums[i],cc); st.push(cc); } for(int i = 0;i<nums.size()-1;i++) { result.push_back(st.top()); st.pop(); } result.push_back(0); return result; }};
- 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
- 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
- 【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
- Leetcode Count of Smaller Numbers After Self
- LeetCode -- Count of Smaller Numbers After Self
- LeetCode-Count of Smaller Numbers After Self
- [leetcode] 315. Count of Smaller Numbers After Self 解题报告
- LeetCode 315. Count of Smaller Numbers After Self
- HTTP状态码
- 安装Android虚拟机,CPU/ABI 为空的解决办法
- GitHub入门
- FastJson源码解读
- PAT(A) - 1102. Invert a Binary Tree (25)
- 【Leetcode】315. Count of Smaller Numbers After Self
- 什么是ANR 如何避免
- Android 线程 thread 两种实现方法!
- VFS文件系统结构分析(容易理解)
- SIP协议小介
- Storm 1:介绍Storm的基本概念
- 被改错的密码
- 使用ffmpeg的concat可以实现简单的流文件合并功能。
- 自我质疑:我适合做程序员吗?