1 Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
給定整型數(shù)組,從中找到兩個數(shù),使得兩數(shù)之和等于目標值,并輸出兩數(shù)的下標(從0開始)。
可以假設(shè)每個輸出只有一個對應(yīng)的結(jié)果,不會重復(fù)使用同一個元素
/**
* 方法2:
* 借助于map存儲數(shù)值和數(shù)組下標
* 時間復(fù)雜度:O(n)
* 空間復(fù)雜度:O(n)
* @param numbers
* @param target
* @return
*/
public static int[] twoNumbers(int[] numbers, int target){
int[] result = new int[2];
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < numbers.length; i++){
//要找的數(shù)值
int temp = target - numbers[i];
//找到了key
if(map.containsKey(temp)){
result[0] = map.get(temp);
result[1] = i;
return result;
}else {
//key = 數(shù)值,value = 下標
map.put(numbers[i],i);
}
}
return result;
}
2 Given a collection of intervals, merge all overlapping intervals.
Example 1:
Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:
Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considerred overlapping.
給定一組區(qū)間,合并所有重疊區(qū)間
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* 給定一組區(qū)間,合并所有重疊區(qū)間
*/
public class Demo2 {
/**
* 區(qū)間類
*/
static class Interval{
//區(qū)間起點
private int start;
//區(qū)間終點
private int end;
public Interval() {
}
public Interval(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public String toString() {
return "[" + start + ", " + end + ']';
}
}
//區(qū)間類比較器,比較start
static class IntervalComparator implements Comparator<Interval>{
@Override
public int compare(Interval o1, Interval o2) {
return o1.start - o2.start;
}
}
/**
* 合并區(qū)間
* @param intervals
* @return
*/
public static List<Interval> merge(List<Interval> intervals) {
List<Interval> result = new ArrayList<>();
if(intervals == null || intervals.size() == 0)
return result;
//使用比較器,按照Interval的start排序
intervals.sort(new IntervalComparator());
System.out.println("排序后:"+intervals);
//數(shù)組第一個區(qū)間
Interval prev = intervals.get(0);
for(int i = 1;i < intervals.size(); i++){
//逐個比較前一個的end是否比當前的end大,來判斷2個區(qū)間是否有重疊
Interval curr = intervals.get(i);
if(prev.end >= curr.start){
//有重疊,新建一個Interval實例,新的start是前一個的start,新的end的值是兩個的end的最大值
prev = new Interval(prev.start,Math.max(prev.end,curr.end));
}else{
//不重疊,就將前一個添加到result中
result.add(prev);
prev = curr;
}
}
//循環(huán)結(jié)束后,把最后一個prev添加到result中
result.add(prev);
return result;
}
//Input: [[1,3],[2,6],[8,10],[15,18]]
//Output: [[1,6],[8,10],[15,18]]
public static void main(String[] args) {
List<Interval> intervals = new ArrayList<>();
intervals.add(new Interval(1,3));
intervals.add(new Interval(8,10));
intervals.add(new Interval(2,6));
intervals.add(new Interval(15,18));
System.out.println("排序前:" + intervals);
List<Interval> list = merge(intervals);
System.out.println("合并后:" + list);
}
}