完美世界笔试求教
求教:刚做完完美世界第一题的动态规划,只过88.89,看不出来哪里有数组越界.
public int maximumTotal (int[][] triangle) { // write code here int ceng = triangle.length; int ans = 0; int[][] dp = new int[ceng][ceng]; dp[0][0]= triangle[0][0]; // index[0] = 0; for (int i = 1; i < ceng; i++) { for (int j = 0; j < triangle[i].length; j++) { if(j==0){ dp[i][j]= dp[i-1][j]+triangle[i][j]; }else if(j==triangle[i].length-1){ dp[i][j]= dp[i-1][j-1]+triangle[i][j]; }else{ dp[i][j]= Math.max(dp[i-1][j-1]+triangle[i][j],dp[i-1][j]+triangle[i][j]); } } } for (int i = 0; i < dp[ceng-1].length; i++) { ans = Math.max(ans,dp[ceng-1][i]); } return ans; }
#深度学习代码#