LeetCode 62. Unique Paths (Medium)
来源:互联网 发布:菜鸟网络上市 编辑:程序博客网 时间:2024/06/06 12:54
题目描述:
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
Note: m and n will be at most 100.
题目大意:从方格图的左上角开始,每次只能向右或向下走一格,问有多少种不同的路径走到右下角。
思路:一开始用dfs,结果超时了…想不到更好的解法,只能看网上的博客了。用DP,用一个m*n二维数组存路径(从(0, 0)到(i ,j))的数量。初始化数组每个元素为1,然后开始遍历:arr[i][j] = arr[i - 1][j] + arr[i][j - 1] (因为(i, j)只能从(i - 1, j)或(i, j - 1)转移过来),最后得到的arr[m - 1][n - 1](下标从0开始)即为路径总数。
c++代码:
class Solution {public: int uniquePaths(int m, int n) { vector<vector<int>> arr; for (int i = 0; i < m; i++) { vector<int> temp; for (int j = 0; j < n; j++) { temp.push_back(1); } arr.push_back(temp); } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { arr[i][j] = arr[i - 1][j] + arr[i][j - 1]; } } return arr[m - 1][n - 1]; }private: int ans;};
阅读全文
0 0
- (Leetcode)62.Unique Paths(medium)
- LeetCode 62. Unique Paths (Medium)
- Leetcode 62. Unique Paths (Medium) (cpp)
- Leetcode 62. Unique Paths (Medium) (java)
- 62. Unique Paths -Medium
- 62. Unique Paths Medium
- [Leetcode 62, Medium] Unique Paths
- 【LeetCode】(63)Unique Paths (Medium)
- 第七周 leetcode 62. Unique Paths(Medium)
- Leetcode解题笔记 62. Unique Paths [Medium] 动态规划
- [Leetcode 63, Medium] Unique Paths II
- 【LeetCode】(63)Unique Paths II(Medium)
- LeetCode-62-Unique Paths(动态规划)-Medium
- Leetcode 63. Unique Paths II (Medium) (cpp)
- Leetcode 63. Unique Paths II (Medium) (java)
- [LeetCode]62.Unique Paths
- LeetCode --- 62. Unique Paths
- LeetCode 62.Unique Paths
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- 数据库创建临时表
- C和指针之实现strlen函数
- UGUI检测射线穿透的方法
- android 读取res/values/arrays中的数据
- LeetCode 62. Unique Paths (Medium)
- leetcode20. Valid Parentheses
- Spring学习笔记
- LeetCode--LRU Cache
- HYSBZ
- C 语言指针怎么理解?
- HTML5上传图片前实现预览(超简单)
- java基础[10]-函数式编程及Lambda
- MySQL 学习教程(三)【整理】-列类型( 数值型/ 日期时间型/ 字符串型)