博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指offer——面试题36:数组中的逆序对
阅读量:4088 次
发布时间:2019-05-25

本文共 1161 字,大约阅读时间需要 3 分钟。

int InversePairs(int* data, int length){    if(data == NULL || length < 0)        return 0;    int* copy = new int[length];    for(int i = 0; i < length; ++ i)        copy[i] = data[i];    int count = InversePairsCore(data, copy, 0, length - 1);    delete[] copy;    return count;}int InversePairsCore(int* data, int* copy, int start, int end){    if(start == end)    {        copy[start] = data[start];        return 0;    }    int length = (end - start) / 2;    int left = InversePairsCore(copy, data, start, start + length);    int right = InversePairsCore(copy, data, start + length + 1, end);    // i初始化为前半段最后一个数字的下标    int i = start + length;     // j初始化为后半段最后一个数字的下标    int j = end;     int indexCopy = end;    int count = 0;    while(i >= start && j >= start + length + 1)    {        if(data[i] > data[j])        {            copy[indexCopy--] = data[i--];            count += j - start - length;        }        else        {            copy[indexCopy--] = data[j--];        }    }    for(; i >= start; --i)        copy[indexCopy--] = data[i];    for(; j >= start + length + 1; --j)        copy[indexCopy--] = data[j];    return left + right + count;}

你可能感兴趣的文章
机器学习基石 - Theory of Generalization
查看>>
Python基本图形绘制
查看>>
Python基本数据类型
查看>>
Python - time库
查看>>
DL编程遇到的问题,记录下
查看>>
deeplearning.ai - 深度学习的实用层面
查看>>
Python - 程序的控制结构
查看>>
Python - random库
查看>>
Tensorflow 学习记录
查看>>
deeplearning.ai - 优化算法 (Optimization Algorithms)
查看>>
Python - 函数和代码复用
查看>>
deeplearning.ai - 超参数调试、Batch正则化、程序框架
查看>>
deeplearning.ai - 机器学习策略 (1)
查看>>
deeplearning.ai - 机器学习策略 (2)
查看>>
deeplearning.ai - 目标检测 Detection algorithms
查看>>
Python - 组合数据类型
查看>>
Python - 文件和数据格式化
查看>>
deeplearning.ai - 人脸识别和神经风格转换
查看>>
Python - 计算生态概览
查看>>
deeplearning.ai - 循环神经网络 (Recurrent Neural Networks)
查看>>