题解 | #数组中的逆序对#

数组中的逆序对

https://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5

class Solution {
public:
  /*确实是一个不错的解法。*/
    int InversePairs(vector<int>& nums) {
        vector<int>a;
        int ans = 0;
        for (int i = nums.size() - 1; i >= 0; i--) {
        auto it = upper_bound(a.begin(),a.end(),nums[i]); 
        int temp_ans = it - a.begin();
        a.insert(it, nums[i]);
        ans = (ans + temp_ans) % 1000000007;
    }
    return ans;
    }
};

全部评论

相关推荐

点赞 评论 收藏
分享
牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务