摘要: 和unique path一样的 1 public int minPathSum(int[][] grid) { 2 if(grid == null || grid.length == 0 || grid[0].length == 0) { 3 return 0; 4 } 5 int len = gr 阅读全文
posted @ 2016-03-10 08:40 warmland 阅读(134) 评论(0) 推荐(0) 编辑
摘要: 和Unique paths是一样的 1 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 2 if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[ 阅读全文
posted @ 2016-03-10 00:04 warmland 阅读(105) 评论(0) 推荐(0) 编辑
摘要: 基础的动规。 方法一: 1.第一行,第一列初始化为1 2.board[i][j] = borad[i-1][j] + board[i][j-1]; 3.返回board[row-1][col-1] 1 if(m <= 0 || n <= 0) { 2 return 0; 3 } 4 int[][] b 阅读全文
posted @ 2016-03-05 09:30 warmland 阅读(150) 评论(0) 推荐(0) 编辑
摘要: 道理我都懂,但是还是调了n久,写出来发现哪哪都没问题,最后发现是k没有-- 都是泪 1 public String getPermutation(int n, int k) { 2 if(n == 0 || k == 0) { 3 return ""; 4 } 5 k--; 6 List<Integ 阅读全文
posted @ 2016-03-05 09:19 warmland 阅读(123) 评论(0) 推荐(0) 编辑
摘要: 1 public int jump(int[] nums) { 2 if(nums == null || nums.length == 0) { 3 return 0; 4 } 5 int reach = 0; 6 int lastReach = 0; 7 int step = 0; 8 for(i 阅读全文
posted @ 2016-03-05 00:04 warmland 阅读(109) 评论(0) 推荐(0) 编辑
摘要: 和Spiral Matrix是一样的,这题还简单一点 1 public int[][] generateMatrix(int n) { 2 int[][] res = new int[n][n]; 3 if(n <= 0) { 4 return res; 5 } 6 int layer = n / 阅读全文
posted @ 2016-03-04 07:31 warmland 阅读(148) 评论(0) 推荐(0) 编辑
摘要: 就是实现题。于是我用了一个半小时 = =心好累 如果layer数是奇数的时候要多处理一下 1 public List<Integer> spiralOrder(int[][] matrix) { 2 List<Integer> res = new ArrayList<Integer>(); 3 if 阅读全文
posted @ 2016-03-04 07:03 warmland 阅读(133) 评论(0) 推荐(0) 编辑
摘要: 思路: 对于数列里的每一个数,从头到尾扫一遍,每一个位置上的数能够达到最远的地方,就是这个位置加上这个位置可以走的最远的位置。 再维持一个目前为止的最大值,如果目前的位置已经超过了曾经有过的最大的值,那么就结束,返回false。 如果全局的最大值已经能够达到数列的末尾,那么就返回true。 1 pu 阅读全文
posted @ 2016-03-04 04:36 warmland 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 和53. Maximum Subarray是一个思路,所以在一起做了,主要就是维护一个局部最优和一个全局最优。 不同在于,因为是有正负数,负号会导致上一轮的最小可能在这轮成为最大的,所以每一次不仅要记录一个局部最大解,还要记录一个局部最小解。 1 public int maxProduct(int[ 阅读全文
posted @ 2016-03-03 10:31 warmland 阅读(113) 评论(0) 推荐(0) 编辑
摘要: 特别重要的一题,主要就是动规全局最有和局部最优的概念。 local[i] = Math.max(nums[i], local + nums[i]) global[i] = Math.max(global[i-1], local[i]) 1 public int maxSubArray(int[] n 阅读全文
posted @ 2016-03-03 09:59 warmland 阅读(109) 评论(0) 推荐(0) 编辑