#Leet Code# Unique Path

描述:

使用了递归,有些计算是重复的,用了额外的空间,Version 1是m*n

Bonus:一共走了m+n步,例如 m = 2, n = 3 [#, @, @, #, @],所以抽象成数学问题,解是C(m + n, m)

代码:

 1 class Solution:
 2     # @return an integer
 3     def __init__(self):
 4         self.record = {}
 5
 6     def uniquePaths(self, m, n):
 7         if m == 0 or n == 0: return 0
 8         if m == 1 or n == 1: return 1
 9
10         if (m-1, n) in self.record:
11             a = self.record[(m-1, n)]
12         else:
13             a = self.uniquePaths(m-1, n)
14             self.record[(m-1, n)] = a
15
16         if (m, n-1) in self.record:
17             b = self.record[(m, n-1)]
18         else:
19             b = self.uniquePaths(m, n-1)
20             self.record[(m, n-1)] = b
21
22         return a + b
23
24 foo = Solution()
25 print foo.uniquePaths(1, 2)

#Leet Code# Unique Path

时间: 2024-10-21 06:32:04

#Leet Code# Unique Path的相关文章

#Leet Code# Unique Tree

语言:Python 描述:使用递归实现 1 class Solution: 2 # @return an integer 3 def numTrees(self, n): 4 if n == 0: 5 return 0 6 elif n == 1: 7 return 1 8 else: 9 part_1 = self.numTrees(n-1) * 2 10 part_2 = 0 11 12 for i in range(1,n-1): 13 part_left = self.numTrees(

Leet Code -- Unique BST

对于数字n(大于1),从1到n有多少种binary search tree(BST序列)?当n=3时,BST序列为: 1         3     3    2     1     \         /     /      / \      \     3      2    1    1  3     2     /       /       \                  \   2      1         2                3共5种. 分析: N=1时,

#Leet Code# Evaluate Reverse Polish Notation

描述:计算逆波兰表达法的结果 Sample: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 使用stack实现: 1 def is_op

leet code Sort List

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leet code Sort List 对链表使用快慢指针归并排序 Sort List Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-lin

#Leet Code# Sqrt

描述:log(n) 代码: 1 class Solution: 2 # @param x, an integer 3 # @return an integer 4 def getVal(self, begin, end, x): 5 if end == begin : 6 return begin 7 if end == begin + 1: 8 return begin 9 10 while True: 11 mid = (begin + end) / 2 12 tmp = mid * mid

LeetCode 63. Unique Path II(所有不同路径之二)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

[LeetCode]题解(python):062 Unique path

题目来源 https://leetcode.com/problems/unique-paths/ 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 bott

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额