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

数组中的逆序对

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;
    }
};

全部评论

相关推荐

点赞 评论 收藏
分享
重生我想学测开:嵌入式的问题,我准备入行京东外卖了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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