数组中的逆序对&&两个链表的第一个公共结点

博客主要围绕数组和链表的两个问题展开。一是求数组中的逆序对总数,通过归并排序改进,在合并数组时记录逆序对数;二是找出两个链表的第一个公共结点,利用HashMap查找效率高的特点,先将链表1放入其中,再判断是否含链表2的结点。

数组中的逆序对

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

解题思路

利用归并排序的改进,将数组分成两个数组(递归到只有一个数据项),再合并数组,再合并的时候用count记录逆序对的数
在这里插入图片描述

代码

/**
 * 剑指offer一刷:数组中的逆序对
 *
 * @author User
 * @create 2019-05-27-12:55
 */

public class jzo35 {
    public static int InversePairs(int [] array) {
        if(array==null||array.length==0)
        {
            return 0;
        }
        int[] copy = new int[array.length];
        for(int i=0;i<array.length;i++)
        {
            copy[i] = array[i];
        }
        int count = InversePairsCore(array,copy,0,array.length-1);//数值过大求余
        return count;

    }
    private  static int InversePairsCore(int[] array,int[] copy,int low,int high) {
        if (low == high) {
            return 0;
        }
        int mid = (low + high) >> 1;
        int leftCount = InversePairsCore(array, copy, low, mid) % 1000000007;
        int rightCount = InversePairsCore(array, copy, mid + 1, high) % 1000000007;
        int count = 0;
        int i = mid;    //前半段最后一个数的下标
        int j = high;    //后半段最后一个数的下标
        int locCopy = high;
        while (i >= low && j > mid) {
            if (array[i] > array[j]) {
                count += j - mid;
                copy[locCopy--] = array[i--];
                if (count >= 1000000007)//数值过大求余
                {
                    count %= 1000000007;
                }
            } else {
                copy[locCopy--] = array[j--];
            }
        }
        for (; i >= low; i--) {
            copy[locCopy--] = array[i];
        }
        for (; j > mid; j--) {
            copy[locCopy--] = array[j];
        }
        for (int s = low; s <= high; s++) {
            array[s] = copy[s];
        }
        return (leftCount + rightCount + count) % 1000000007;
    }
    public static void main(String[] args){
        int[] arr={1,2,3,4,5,6,7,0};
        jzo35 so=new jzo35();
        System.out.println(so.InversePairs(arr));
    }
}

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

输入两个链表,找出它们的第一个公共结点。

解题思路

对于查找类的题型,我首先想到的是HashMap,因为HashMap的查找效率是O(1);思路也就是先将链表1放入HashMap中再判断HashMap中是否含有链表二的结点,如果有,就是第一个公共节点(也可将公共节点全部输出)

代码

import java.util.HashMap;

/**
 * 剑指offer一刷:两个链表的第一个公共结点
 *
 * @author User
 * @create 2019-05-27-13:53
 */

public class jzo36 {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode current1=pHead1;
        ListNode current2=pHead2;

        HashMap<ListNode,Integer> hashMap=new HashMap<>();
        while (current1!=null){
            hashMap.put(current1,null);
            current1=current1.next;
        }
        while (current2!=null){
            if (hashMap.containsKey(current2)){
                return current2;
            }
            current2=current2.next;
        }
        return null;
    }
    public static void main(String[] args){
        ListNode node1=new ListNode(1);
        ListNode node2=new ListNode(2);
        ListNode node3=new ListNode(3);
        ListNode node4=new ListNode(4);
        ListNode node5=new ListNode(5);
        ListNode node6=new ListNode(6);
        ListNode node7=new ListNode(7);
        ListNode node8=new ListNode(8);
        ListNode node9=new ListNode(9);
        node1.next=node3;
        node3.next=node2;
        node2.next=node4;
        node4.next=node6;
        node7.next=node8;
        node8.next=node2;
        node2.next=node5;
        node5.next=node9;
        jzo36 so=new jzo36();
        System.out.println(so.FindFirstCommonNode(node1,node7).val);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值