合并区间常考面试题

合并区间

https://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a?tpId=117&&tqId=37737&&companyId=665&rp=1&ru=/company/home/code/665&qru=/ta/job-code-high/question-ranking

public class Solution {
    public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
        ArrayList<Interval> ret = new ArrayList<>();
        if(intervals == null || intervals.size() == 0) {
            return ret;
        }
        intervals.sort((a, b)->(a.start - b.start));
        ret.add(intervals.get(0));
        for(int i = 1; i < intervals.size(); i++) {
            int left = intervals.get(i).start;
            int right = intervals.get(i).end;
            if(ret.get(ret.size() - 1).end < left) {
                ret.add(new Interval(left, right));
            }else {
                ret.get(ret.size() - 1).end = Math.max(ret.get(ret.size() - 1).end, right);
            }
        }
        return ret;
    }
}


import java.util.*;
public class Solution {
    public ArrayList<Interval> merge(ArrayList<Interval> interval) {
        ArrayList<Interval> ret = new ArrayList<>();
        if(interval == null || interval.size() == 0) {
            return ret;
        }
        interval.sort((a, b) -> (a.start - b.start));
        ret.add(interval.get(0));
        for(int i = 1; i < interval.size(); i++) {
            int left = interval.get(i).start;
            int right = interval.get(i).end;
            if(ret.get(ret.size() - 1).end < left) {
                ret.add(new Interval(left, right));
            }else {
                ret.get(ret.size() - 1).end = Math.max(ret.get(ret.size() - 1).end, right);
            }
        }
        return ret;
    }
}

合并区间

https://leetcode-cn.com/problems/merge-intervals/

class Solution {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (a,b)->a[0] - b[0]);
        int[][] ret = new int[intervals.length][2];
        int index = -1;
        for(int[] nums : intervals) {
            if(index == -1 || nums[0] > ret[index][1]) {
                ret[++index] = nums;
            }else {
                ret[index][1] = Math.max(ret[index][1], nums[1]);
            }
        }
        return Arrays.copyOf(ret, index + 1);
    }
}


class Solution {
    public int[][] merge(int[][] nums) {
        if(nums == null || nums.length == 0 || nums[0].length == 0) {
            return new int[0][0];
        }
        Arrays.sort(nums, (a, b) -> a[0] - b[0]);
        int[][] ret = new int[nums.length][2];
        int index = -1;
        for(int[] arr : nums) {
            if(index == -1 || arr[0] > ret[index][1]) {
                ret[++index] = arr;
            }else {
                ret[index][1] = Math.max(ret[index][1], arr[1]);
            }
        }
        return Arrays.copyOf(ret, index + 1);
    }
}

合并k个已排序的链表

https://www.nowcoder.com/practice/65cfde9e5b9b4cf2b6bafa5f3ef33fa6?tpId=117&&tqId=37747&&companyId=665&rp=1&ru=/company/home/code/665&qru=/ta/job-code-high/question-ranking

import java.util.*;
public class Solution {
    public ListNode mergeKLists(ArrayList<ListNode> lists) {
        if (lists == null) {
            return null;
        }
        PriorityQueue<ListNode> queue = new PriorityQueue<>((ListNode a, ListNode b) -> (a.val - b.val));
        for (ListNode list : lists) {
            if (list != null) {
                queue.add(list);
            }
        }
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;

        while (!queue.isEmpty()) {
            ListNode node = queue.poll();
            tmp.next = node;
            if (node.next != null) {
                queue.add(node.next);
            }
            tmp = tmp.next;
        }
        return newHead.next;
    }
}

import java.util.*;
public class Solution {
    public ListNode mergeKLists(ArrayList<ListNode> lists) {
        if(lists == null || lists.size() == 0) {
            return null;
        }
        return func(lists, 0, lists.size() - 1);
    }
    public static ListNode func(ArrayList<ListNode> lists, int left, int right) {
        if(left == right) {
            return lists.get(left);
        }
        int mid = left + right - 1;
        ListNode leftList = func(lists, left, mid);
        ListNode rightList = func(lists, mid + 1, right);
        return merge(leftList, rightList);  
    }
    public static ListNode merge(ListNode l1, ListNode l2) {
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while(l1 != null && l2 != null) {
            if(l1.val <= l2.val) {
                tmp.next = l1;
                l1 = l1.next;
            }else {
                tmp.next = l2;
                l2 = l2.next;
            }
            tmp = tmp.next;
        }
        if(l1 != null) {
            tmp.next = l1;
        }
        if(l2 != null) {
            tmp.next = l2;
        }
        return newHead.next;
    }
}


class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) {
            return null;
        }
        PriorityQueue<ListNode> queue = new PriorityQueue<>((ListNode a, ListNode b) -> (a.val - b.val));
        for (ListNode list : lists) {
            if (list != null) {
                queue.add(list);
            }
        }
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;

        while (!queue.isEmpty()) {
            ListNode node = queue.poll();
            tmp.next = node;
            if (node.next != null) {
                queue.add(node.next);
            }
            tmp = tmp.next;
        }
        return newHead.next;
    }
}


class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists == null || lists.length ==0) {
            return null;
        } 
        return func(lists, 0, lists.length - 1);
    }

    private ListNode func(ListNode[] lists, int left, int right) {
        if(left == right){
            return lists[left];
        }
        int mid = left + right >> 1;
        ListNode leftList = func(lists, left, mid);
        ListNode rightList = func(lists, mid + 1, right);
        return merge(leftList, rightList);
    }

    public ListNode merge(ListNode first,ListNode second) {
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while (first != null && second != null) {
            if(first.val <= second.val){
                tmp.next = first;
                first = first.next;
            }
            else{
                tmp.next = second;
                second = second.next;
            }
            tmp = tmp.next;
        }
        if(first != null){
            tmp.next = first;
        }
        if(second != null){
            tmp.next = second;
        }
        return newHead.next;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值