首页 > 编程语言 > 详细

数组中的逆序对

时间:2015-09-01 16:36:42      阅读:162      评论:0      收藏:0      [点我收藏+]

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。

 1 class Solution {
 2 public:
 3     int InversePairs(vector<int> data) {
 4         int count=0;
 5         int n=data.size();
 6         if(n<1) return count;
 7         for(int i=0;i<n-1;i++){
 8             for(int j=i+1;j<n;j++){
 9                 if(data[i]>data[j])
10                     count++;
11             }
12             
13         }
14         return count;
15     }
16 };

 

数组中的逆序对

原文:http://www.cnblogs.com/zl1991/p/4775838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!