Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does LeetCode insert an interval

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to insert the LeetCode interval, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Brief introduction of the problem.

A non-overlapping interval list sorted by the beginning and end of the interval is given. To insert a new interval into the list, you need to make sure that the intervals in the list are still orderly and do not overlap (merge intervals if necessary).

2, example

Example 1:

Input: intervals = [1jue 3], [6je 9], newInterval = [2je 5] output: [[1Jing 5], [6je 9]] example 2:

Input: intervals = [1jue 2], [3je 5], [6je 7], [8rect 10], [12je 16]], newInterval = [4je 8] output: [[1rect 2], [3jue 10], [12jue 16]] explain: this is because the new interval [4je 8] overlaps with [3je 5], [6pr 7], [8je 10].

Note: the input type was changed on April 15, 2019. Please reset to the default code definition to get the new method signature.

3, the train of thought of solving the problem

In fact, this problem is the same as the whole solution of the previous merge interval problem. Put the new array elements into the set, then sort the set elements, and then make a logical judgment. Here, the set is used as a temporary storage space to compare the contents of adjacent intervals, such as the value of the right endpoint of the previous interval and the value of the left endpoint of the next interval. After merging, the result set is put into the result set, and those that do not conform to the merge are also put into the result set. When all the intervals are processed, the merged data is processed, which is also the main idea of this topic.

4, problem solving procedure

Import java.util.*

Public class InsertTest {public static void main (String [] args) {int [] [] intervals = {{1,3}, {6,9}}

Int [] newInterval = {2,5}

Int [] [] insert = insert (intervals, newInterval); for (int [] arr: insert) {System.out.println (arr [0] + "+ arr [1]);}}

Public static int [] [] insert (int [] [] intervals, int [] newInterval) {if ((intervals = = null) & & newInterval = = null) {return new int [] [] {newInterval};}

List list = new ArrayList (); Collections.addAll (list, intervals); list.add (newInterval); int [] [] tempArray = list.toArray (new int [0] []); List result = new ArrayList (); Arrays.sort (tempArray, Comparator.comparingInt (x-> x [0])); List tempList = new ArrayList (); Collections.addAll (tempList, tempArray) Int [] temp = tempList.get (0); for (int I = 1; I < tempList.size (); iTunes +) {if (tempList.get (I) [0])

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report