注意指针数组的建立和删除
int getMaxvalue(const int*matrix, int rows, int cols)
{
if (matrix == NULL || rows <= 0 || cols <= 0)return 0;
int **dp = new int*[rows];
for (int i = 0; i < rows; i++)dp[i] = new int[cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
int left = 0, up = 0;
if (j)left = dp[i][j - 1];
if (i)up = dp[i - 1][j];
dp[i][j] = max(left, up) + matrix[i*cols + j];
}
}
int ans=dp[rows - 1][cols - 1];
for (int i = 0; i < rows; i++)delete[]dp[i];
delete[]dp;
return ans;
}
int getMaxvalue(const int*matrix, int rows, int cols)
{
if (matrix == NULL || rows <= 0 || cols <= 0)return 0;
int *dp = new int[cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
int left = 0, up = 0;
if (i)up = dp[j];//判断上和左是否存在
if (j)left = dp[j - 1];
dp[j] = max(left, up) + matrix[i*cols + j];
}
}
int ans = dp[cols - 1];
delete[]dp;
return ans;
}