[leetCode刷题笔记]2017.02.11

昨天事情太多,就没有刷提了。。。少吃一天药,会犯病的。。。

57. Insert Interval

这道题思路是这是个按顺序排列的List,所以从头开始进行循环。for循环中有三种情况要进行两头比较(start, end)。具体步骤见标注,参考:http://blog.csdn.net/zdavb/article/details/47253711

public class Solution {
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
        List<Interval> res = new LinkedList<Interval>();
        int length = intervals.size();
        if(length<=0){
            res.add(newInterval);
            return res;
        }


        boolean filled = false;
        for(int i = 0;i<length;i++){
            Interval current = intervals.get(i);
            if(!filled && current.start>=newInterval.start){
                current = newInterval;
                // fill element
                filled = true;
                // back
                i--;
            }
            else if(!filled) {
                // add current it in output without change
                res.add(current);
            }
            // start insert
            if(filled){
                // empty output list (add new interval to first place)
                if(res.isEmpty()){
                    res.add(current);
                    continue;
                }
                // top elenment in the output list
                Interval top = res.get(res.size()-1);
                // new.start<=top.end
                if(top.end>=current.start)
                // new.start -> max(top.end,current.end) -> top.end
                    top.end = Math.max(top.end,current.end);
                else 
                // top.end -> new.start
                    res.add(current);
            }
        }
        // add to the last place
        if(!filled){
            Interval top = res.get(res.size()-1);
            if(top.end>=newInterval.start)
                top.end = Math.max(top.end,newInterval.end);
            else
                res.add(newInterval);
        }
        return res;
    }
}

59. Spiral Matrix II

这道题和Sprial Matix一个样。。。

public class Solution {
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        if (n == 0) {
            return result;
        }


        int top = 0;
        int right = 0;
        int bottom = 0;
        int left = 0;
        int index = 1;
        int i = 0;
        while (true) {
            // top
            for (i = left; i < n - right; i++) {
                result[top][i] = index++;
            }
            top++;
            if (top + bottom == n) {
                break;
            }
            // right
            for (i = top; i < n - bottom; i++) {
                result[i][n - 1 - right] = index++;
            }
            right++;
            if (left + right == n) {
                break;
            }
            // bottom
            for (i = n - 1 - right; i >= left; i--) {
                result[n - 1 - bottom][i] = index++;
            }
            bottom++;
            if (top + bottom == n) {
                break;
            }
            // left
            for (i = n - 1 - bottom; i >= top; i--) {
                result[i][left] = index++;
            }
            left++;
            if (left + right == n) {
                break;
            }
        }
        return result;
    }
}

62. Unique Paths

到达每个点的步数是点左边和上面的和,而最上面和最左边一排都是1.所以就两两相加就好了。

public class Solution {
    public int uniquePaths(int m, int n) {
        int[][] grid = new int[m][n];
        for (int i = 0; i < n; i++ ) {
            grid[0][i] = 1;
        }
        for (int i = 1; i < m; i++) {
            grid[i][0] = 1;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                grid[i][j] = grid[i - 1][j] + grid[i][j - 1];
            }
        }
        return grid[m-1][n-1];
    }
}

63. Unique Paths II

这道题和前面一道题也可以用动态规划。用一个数组保存当前列的。如

public class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if(obstacleGrid[0][0] ==1) return 0;  
        int m = obstacleGrid.length;  
        int n = obstacleGrid[0].length;  
        int[] step = new int[n];  
        step[0] = 1;  
        for(int i=0;i<m;i++){  
            for(int j=0;j<n;j++){  
                if(obstacleGrid[i][j]==1)  
                    step[j] = 0;  
                else if(j>0){  
                    step[j] = step[j-1]+step[j];  
                }  
            }  
        }  
        return step[n-1];  
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值