博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指offer 第九天
阅读量:7098 次
发布时间:2019-06-28

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

35.数组中的逆序对

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007

输入描述:

题目保证输入的数组中没有的相同的数字

数据范围:

对于%50的数据,size<=10^4对于%75的数据,size<=10^5对于%100的数据,size<=2*10^5

示例1

输入1,2,3,4,5,6,7,0 输出7

public class Solution {    public int InversePairs(int [] array) {        if(array == null||array.length == 0) return 0;        int length = array.length;        int[] copy = new int[length];        for(int i = 0;i < length ;i++){            copy[i] = array[i];        }        int count = InversePairsCore(array,copy,0,array.length-1);        return count;    }        public int InversePairsCore(int[] array,int[] copy,int start,int end){        if(start == end){            return 0;        }        int count = 0;        int length = (end-start)/2;        //注意:这里是故意将copy和array调换位置的,因为每次执行InversePairCore之后copy在[start,end]部分都是排好序的        //随意使用data作为array输入,省去了来回复制的资源消耗。        int left = InversePairsCore(copy,array,start,start+length);        int right = InversePairsCore(copy,array,start+length+1,end);        int p1 = start+length;        int p2 = end;        int copyIdx = end;        while(p1 >= start && p2 >= start+length+1){            if(array[p1]>array[p2]){                count += p2-start-length;                //此处先判断一下,以免超出运算范围。                if(count > 1000000007)                    count = count%1000000007;                copy[copyIdx--] = array[p1--];            }else{                copy[copyIdx--] = array[p2--];            }        }        while(p1 >= start){            copy[copyIdx--] = array[p1--];        }        while(p2 >= start+length+1){            copy[copyIdx--] = array[p2--];        }        return (count+left+right)%1000000007;    } }

36.两个链表的第一个公共结点

输入两个链表,找出它们的第一个公共结点。技巧:不适用辅助Stack

/*public class ListNode {    int val;    ListNode next = null;    ListNode(int val) {        this.val = val;    }}*/public class Solution {    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {        if(pHead1 == null || pHead2 == null) return null;        int L1 = getListLength(pHead1);        int L2 = getListLength(pHead2);        if(L1>L2)            for(int i = 0 ;i<(L1-L2);i++)                pHead1 = pHead1.next;        else if(L2>L1)            for(int i = 0 ;i<(L2-L1);i++)                pHead2 = pHead2.next;                while(pHead1!=null){            if(pHead1 == pHead2)                return pHead1;            pHead1 = pHead1.next;            pHead2 = pHead2.next;        }        return null;    }    public int getListLength(ListNode head){        ListNode temp = head;        int count = 1;        while(temp.next != null){            temp = temp.next;            count++;        }        return count;    }}

转载于:https://www.cnblogs.com/guoyaohua/p/8519698.html

你可能感兴趣的文章
A example that using JQuery clone
查看>>
随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比...
查看>>
Android 使用JSON格式与服务器交互 中文乱码问题解决
查看>>
_DataStructure_C_Impl:链串
查看>>
openvas
查看>>
SecureCRT同时向多个终端发送命令
查看>>
【IntelliJ】IntelliJ IDEA常用设置及快捷键以及自定义Live templates
查看>>
indexOf 和 lastIndexOf 的区别
查看>>
spring boot整合activemq消息中间件
查看>>
Spark:java api实现word count统计
查看>>
mqtt-jmeter
查看>>
Android上的蓝牙通信功能的开发:BluetoothChat例程分析
查看>>
Eclipse快捷键大全(转载)
查看>>
Yahoo!网站性能最佳体验的34条黄金守则——图片、Coockie与移动应用
查看>>
微博采撷(一)
查看>>
【CLR的执行模型:加载公共语言运行库(3)】
查看>>
中国用户通过rchange用银联充值到PerfectMoney再给BTC-E充值进行搬砖的方法
查看>>
DBCC--CHECKDB
查看>>
判断一个坐标点是否在不规则多边形内部的算法
查看>>
10个优秀的 HTML5 & CSS3 下拉菜单制作教程
查看>>