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].

Subscribe to see which companies asked this question

因为需要求某元素右边小于该元素的数组元素的个数,所以从数组末尾开始向前进行处理。数据结构使用树,节点有属性value,smaller,分别表示对应元素的值以及数组中小于该元素值的个数。函数insert既向树种插入新的节点,又能够求得数组右边小于value的元素的个数。因为数组末尾的元素的smaller值首先返回,所以要用到双向队列deque,将每次求得的值插入链表的头部。最终返回整个队列的值即可。

在插入操作中,请注意函数的参数root是指针的引用,因为对该指针的修改必须得到保留。如果root为空(代表数组末尾的那个元素),初始化根节点,返回0.如果根节点的值大于value,需要把这个元素对应的几点向左插入树中,同时,小于根节点的值的元素个数加1.剩余的情况下,节点需要向右插入树中,返回的值应该是比root节点小的元素个数加上树的右半部分比该元素小的元素个数,同时还要区分一下要插入的值是大于还是等于root节点的值。

很精妙的解法,真是只可意会,不可言传啊。

class Solution {
private:
class Node{
public:
int value;
int smaller;
Node *left;
Node *right;
Node(int value,int smaller) {
this ->value = value;
this ->smaller =smaller;
left=right=NULL;//在leetcode中,必须要加上这句,否则超时
}
};
int insert(Node * & root,int value){
if(root ==NULL){
root =new Node(value,);
return ;
}
if(root->value > value){
root->smaller++;
return insert(root->left,value);
}
return root->smaller+insert(root->right,value)+(root->value <value? :);
}
public:
vector<int> countSmaller(vector<int>& nums) {
Node * root=NULL;
deque <int >q;
for(int i=nums.size()-;i>-;i-- ){
int value =insert(root,nums[i]);
cout<<"this is a test! "<<value<<endl;
q.push_front(value);
}
return vector<int>(q.begin(),q.end());
}
};

315.Count of Smaller Numbers After Self My Submissions Question的更多相关文章

  1. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

  2. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  3. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  4. [LeetCode] 315. Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The countsarray has t ...

  5. LeetCode 315. Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  6. 315. 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 ...

  7. 315. Count of Smaller Numbers After Self(Fenwick Tree)

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  8. 315. 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 ...

  9. 第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树

    Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树 ...

随机推荐

  1. redis五种数据类型的使用

    redis五种数据类型的使用 redis五种数据类型的使用 (摘自:http://tech.it168.com/a2011/0818/1234/000001234478_all.shtml ) 1.S ...

  2. android脚步---图片浏览

    简单的图片浏览器,实现图像显示与点击切换下一张 首先在main.xml里面定义一个简单的线性布局容器. <?xml version="1.0" encoding=" ...

  3. Rails (堆栈)<数据结构>

    题意:<看图片> 解题思路:栈的简单应用: #include<iostream> #include<stack> #include<algorithm> ...

  4. HTML中为何P标签内不可包含DIV标签? (转)

    起因:在做项目时发现原本在DW中无误的代码到了MyEclipse6.0里面却提示N多错误,甚是诧异.于是究其原因,发现块级元素P内是不能嵌套DIV的. 深究:我们先来认识in-line内联元素和blo ...

  5. Java设计模式--Java Builder模式

    1.Java Builder模式主要是用一个内部类去实例化一个对象,避免一个类出现过多构造函数,而且构造函数如果出现默认参数的话,很容易出错. public Person(String name) P ...

  6. extjs 框架模板

    的 <script> Ext.onReady(function(){ Ext.create('Ext.container.Viewport', { layout: 'border', it ...

  7. android usb挂载分析---MountService启动

    android usb挂载分析---MountService启动 分类: android框架 u盘挂载2012-03-27 23:00 11799人阅读 评论(4) 收藏 举报 androidsock ...

  8. Android启动脚本init.rc(2)

    在Android中使用启动脚本init.rc,可以在系统的初始化中进行简单的操作. init.rc启动脚本路径:system/core/rootdir/init.rc 内容: Commands:命令 ...

  9. VS+VA 开发NDK

    工欲善其事,必先利其器. Android NDK开发环境,可选择VIM+插件.Eclipse+CDT等,这里介绍另一种选择:VS+VA 软件准备:Visual studio 2008 // 其他版本也 ...

  10. Altera CYCLONE III FPGA BGA布线

    最近在做altera FPGA BGA相关的布线工作,收集了一些资料,公开出来以供大家讨论. 首先是器件引脚,只有弄清楚器件各个引脚的功能才能够进行布线,下面的文档详细描述了每个引脚的功能. 各引脚功 ...