solution.json 3.1 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "0e7ad87cf9d74d31b2f292832b782fe9",
  "keywords": "数组,动态规划,矩阵",
  "title": "最小路径和",
  "desc": [
    {
      "content": "\n<p>给定一个包含非负整数的 <code><em>m</em> x <em>n</em></code> 网格 <code>grid</code> ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。</p><p><strong>说明:</strong>每次只能向下或者向右移动一步。</p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0064.Minimum%20Path%20Sum/images/minpath.jpg\" style=\"width: 242px; height: 242px;\" /><pre><strong>输入:</strong>grid = [[1,3,1],[1,5,1],[4,2,1]]<strong><br />输出:</strong>7<strong><br />解释:</strong>因为路径 1→3→1→1→1 的总和最小。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>grid = [[1,2,3],[4,5,6]]<strong><br />输出:</strong>12</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>m == grid.length</code></li>\t<li><code>n == grid[i].length</code></li>\t<li><code>1 <= m, n <= 200</code></li>\t<li><code>0 <= grid[i][j] <= 100</code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic inline int min(int a, int b)\n{\n\treturn a < b ? a : b;\n}\nint minPathSum(int **grid, int gridRowSize, int gridColSize)\n{\n\tint i, j;\n\tint **dp = malloc(gridRowSize * sizeof(int *));\n\tfor (i = 0; i < gridRowSize; i++)\n\t{\n\t\tdp[i] = malloc(gridColSize * sizeof(int));\n\t}\n\tdp[0][0] = grid[0][0];\n\tint sum = dp[0][0];\n\tfor (i = 1; i < gridRowSize; i++)\n\t{\n\t\tsum += grid[i][0];\n\t\tdp[i][0] = sum;\n\t}\n\tsum = dp[0][0];\n\tfor (i = 1; i < gridColSize; i++)\n\t{\n\t\tsum += grid[0][i];\n\t\tdp[0][i] = sum;\n\t}\n\tfor (i = 1; i < gridRowSize; i++)\n\t{\n\t\tfor (j = 1; j < gridColSize; j++)\n\t\t{\n\t\t\tdp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);\n\t\t}\n\t}\n\treturn dp[gridRowSize - 1][gridColSize - 1];\n}\nint main(int argc, char **argv)\n{\n\tint i, j;\n\tint row = argc - 1;\n\tint col = strlen(argv[1]);\n\tint **grid = malloc(row * sizeof(int *));\n\tfor (i = 0; i < row; i++)\n\t{\n\t\tgrid[i] = malloc(col * sizeof(int));\n\t\tfor (j = 0; j < col; j++)\n\t\t{\n\t\t\tgrid[i][j] = argv[i + 1][j] - '0';\n\t\t\tprintf(\"%d \", grid[i][j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\tprintf(\"%d\\n\", minPathSum(grid, row, col));\n\treturn 0;\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-81f91206205e431f9ce2fa179955ecfa",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470227"
}