程序员面试金典——番外篇之数组中的逆序对
此题曾多次遇到,然鹅还是本能的想起来复杂度为
O(n2)
O
(
n
2
)
的笨蛋方法。。。
Solution1:笨蛋方法
class AntiOrder {
public:
int count(vector<int> A, int n) {
// write code here
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if(A[i] > A[j])
sum++;
}
}
return sum;
}
};
Solution2:
参考《剑指offer》第36题:https://blog.csdn.net/allenlzcoder/article/details/79682716