7. Merging repeating intervals
Given a set of intervals, merge the duplicates. Cases:
Given [1,3],[0,7],[2,6],[8,10],[15,18], where [1,3] is duplicated with [0,7] and [2,6] intervals, it is merged into one interval: [0,7]. Final return:
[0,7],[8,10],[15,18].
The solution used in the book comparator, the general idea is as follows:
1. Create an interval class interval, whose member variables are start and end, representing the start and end of the interval, respectively.
2. Create a solution class that contains the merge method, the input parameter intervals, and the return value result are a list of interval classes that represent the interval between input & output. The merge method is used to merge repeating intervals: First, the input interval list is sorted from small to large by intervals by the start size of each interval. The inatervals after sorting is: [0,7],[1,3],[2,6],[8,10],[15,18]. Finally create result, traverse the intervals, if there is a repeating interval in the traversal process, then merge, otherwise the interval join result
3. Return result
Code:
Import Java.util.arraylist;import Java.util.collections;import Java.util.comparator;public class Test {public static Class Interval {int Start;int end;interval () {start = 0;end = 0;} Interval (int s, int e) {start = S;end = e;} Public String toString () {return ' start: ' + start + ', end: ' + end + ' \ n ';}} public static class Solution {public arraylist<interval> merge (arraylist<interval> intervals) {if ( intervals = = NULL | | Intervals.size () <= 1) Return intervals;//sort intervals by using self-defined comparatorcollections.sort (intervals, New Intervalcomparator ()); for (Interval t:intervals) {System.out.println (t);} arraylist<interval> result = new arraylist<interval> () Interval prev = intervals.get (0); for (int i = 1; i < Intervals.size (); i++) {Interval Curr = Intervals.get (i), if (prev.end >= curr.start) {//merged caseinterval merged = new Interval (prev.s Tart, Math.max (Prev.end, Curr.end));p rev = merged;} else {result.add (prev);p rev = Curr;}} Result.add (prev);return result;}} public static class Intervalcomparator implements comparator<interval> {public int compare (Interval i1, Interval i2 ) {return i1.start-i2.start;}} public static void Main (string[] args) {solution s = new solution (); arraylist<interval> intervals = new arraylist<interval> () Intervals.add (New Interval (1, 3)); Intervals.add (New Interval (0, 7)), Intervals.add (New Interval (2, 6)), Intervals.add (new Interval (8)); Intervals.add (New Interval (15, 18)); arraylist<interval> temp = new arraylist<interval> (), temp = s.merge (intervals); for (Interval t:temp) {Syste M.out.println (t);}}}
8. Insertion Interval
Example 1:
Original Interval list:[1,3],[6,9]
Insertion interval: [2,5]
End result: Because [2,5] is duplicated with [1,3], the output is [1,5],[6,9].
Example 2:
Original Interval list:[1,2],[3,5],[6,7],[8,10],[12,16]
Insertion interval: [4,9]
End result: Because [4,9] is duplicated with [3,5],[6,7],[8,10], the output is [1,2],[3,10],[12,16]
This is the same as last time.
Code:
Import Java.util.arraylist;import Java.util.collections;import Java.util.comparator;public class Test {public static Class Interval {int Start;int end;interval () {start = 0;end = 0;} Interval (int s, int e) {start = S;end = e;} Public String toString () {return ' start: ' + start + ', end: ' + end + ' \ n ';}} public static class Solution {public arraylist<interval> insert (arraylist<interval> intervals,interval NewInterval) {arraylist<interval> result = new arraylist<interval> (); for (Interval interval:intervals) {if (Interval.end < Newinterval.start) {Result.add (interval);} else if (Interval.start > Newinterval.end) {result.add (newinterval); newinterval = interval;} else if (interval.end ; = Newinterval.start| | Interval.start <= newinterval.end) {newinterval = new interval (math.min (Interval.start,newinterval.start), Math.max (Newinterval.end,interval.end));}} Result.add (newinterval); return result;}} public static void Main (string[] args) {solution s = new Solution (); arraylist<interval> intervals = new arraylist<interval> () Intervals.add (New Interval (1, 3)); Intervals.add (New Interval (6, 9)); arraylist<interval> temp = new arraylist<interval> (), temp = S.insert (intervals, new Interval (2, 5)); for ( Interval t:temp) {System.out.println (t);}}}
9. The sum of two numbers
Given an array of numbers and a target, it is required to return index1 and Index2, which makes numbers[index-1]+numbers[index2-1]== target, where index1 <index2
Cases:
Input: Array numbers={2,7, one, one, and target=9
Output: index1=1,index2=2
Solution One:
The simplest way to think first is to be poor.
Code:
public class Test {public static void GetResult (int[] numbers, int target) {int len = Numbers.length;int i = 0, j = 0;for (i = 0; i < len; ++i) for (j = i + 1; j < Len; ++j) if (Numbers[i] + numbers[j] = = target) {++i;++j; String str = (i > J?) ("Index1:" + j + ", Index2:" + i): ("index1:" + i + ", Index2:" + j)); System.out.println (str);}} public static void Main (string[] args) {int[] numbers = {2, 7, one,};getresult (numbers, 9);}}
Solution Two: Use HashMap. Where key is meant to be plus how much and can be taget,value represents the position of the value in the array. i.e. Numbers[value] + key ==target. That's a little messy, keep looking at examples:
Array numbers={2,7, one, one, and target=9
Solution: 1. New hashmap;2. Traverse Numbers;3. If there is a value of numbers in the key of the map, it indicates that it was found.
Code:
Import Java.util.hashmap;public class Test {public static class solution {public static int[] Twosum (int[] numbers, int ta Rget) {hashmap<integer, integer> map = new Hashmap<integer, integer> (); int[] result = new Int[2];for (int i = 0; i < numbers.length; i++) {if (Map.containskey (Numbers[i])) {int index = Map.get (Numbers[i]); result[0] = index + 1;result[1] = i + 1;break;} el SE {map.put (target-numbers[i], i);}} return result;}} public static void Main (string[] args) {int[] numbers = {2, 7, one, 9};int[] result = solution.twosum (numbers,); System.out.println ("index1:" + result[0] + ", Index2:" + result[1]);}}
Interview 10 Big algorithm Rollup-strings and Arrays 5