题解 | #数组中只出现一次的两个数字#

数组中的逆序对

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

class Solution:
    count = 0
    def InversePairs(self , data: List[int]) -> int:
        # write code here
        def mergeSort(arr, left, right):
            if left >= right:
                return []
            mid = (left + right) // 2
            mergeSort(arr, left, mid)
            mergeSort(arr, mid + 1, right)
            merge(arr, left, mid, right)

        def merge(arr, left, mid, right):
            tmp = []
            i, j = left, mid + 1
            while i <= mid and j <= right: 
                if arr[i] <= arr[j]:
                    tmp.append(arr[i])
                    i += 1
                else:
                    tmp.append(arr[j])
                    j += 1
                    self.count = self.count%1000000007 + (mid - i +1)
            while(i <= mid):
                tmp.append(arr[i])
                i +=1
            while(j <= right):
                tmp.append(arr[j])
                j +=1
            for index in range(len(tmp)):
                arr[left + index] = tmp[index]
        if len(data) > 0:
            mergeSort(data, 0, len(data) -1)
        return self.count
全部评论

相关推荐

Pandaileee:校友加油我现在也只有一个保底太难了
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务