合并区间
合并区间
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个已排序的链表
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;
}
}