LeetCode 55.Jump Game & 56.Merge Intervals

Problem 55 Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.


解题思路:

1. 可以考虑用一个max记录当前下标可以到达的最大下标

2. 所以从头遍历数组,寻找记录max,当记录的max小于当前遍历的下标的时候,则表明当前下标已为不可达下标,所以肯定也不能达到最后的元素


代码如下:

public class Solution {
    public boolean canJump(int[] nums) {
       int max = 0;
       
       for(int i = 0;i < nums.length; i++){
           if(i > max){
               return false;
           }
           
           max = Math.max(max, i + nums[i]);
       }
       
       return true;
        
    }
   
}



Problem 56 Merge Intervals

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].


解题思路:

1. 首先需要合并的四种情况,如下图所示:


2. 只要两个区间出现这样的情况,就考虑把它们合并,所以我们不妨用一个list记录结果的区间,一个list记录待合并的区间,对于每一个待合并的区间,都从头遍历结果区间,如果需要合并,则直接合并,如果不需要合并,则直接将该区间加入结果区间

3. 合并完一次后,将结果区间作为一个新的待合并区间,再次进行合并,直到合并之前与之后list的长度不再发生变化,则表示区间合并已完成


代码如下:

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public List<Interval> merge(List<Interval> intervals) {
        List<Interval> result = new ArrayList<>();
        mergeHelper(intervals,result,0);
        
        return result;
        
    }
    
    public void mergeHelper(List<Interval> intervals, List<Interval> result, int length){
        Iterator it = intervals.iterator();
        while(it.hasNext()){
            Interval cur = (Interval) it.next();
            boolean isHandle = false;
            for(int i = 0; i < result.size();i++){
                Interval temp = result.get(i);
                if(cur.start <= temp.start && cur.end <= temp.end && cur.end >= temp.start){
                    temp.start = cur.start;
                    isHandle = true;
                    break;
                }
                
                if(cur.start >= temp.start && cur.end <= temp.end){
                    isHandle = true;
                    break;
                }
                
                if(cur.end >= temp.end && cur.start <= temp.end && cur.start >= temp.start){
                    temp.end = cur.end;
                    isHandle = true;
                    break;
                }
                
                if(cur.start <= temp.start && cur.end >= temp.end){
                    temp.start = cur.start;
                    temp.end = cur.end;
                    isHandle = true;
                    break;
                }
            }
            
            if(!isHandle){
                result.add(cur);
            }
        }
        
        if(length == result.size()){
            return;
        }
        else{
            List<Interval> newList = new ArrayList<>(result);
            result.clear();
            mergeHelper(newList,result,newList.size());
        }
        
        
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值