#Leet Code# Unique Path

來源:互聯網
上載者:User

標籤:style   blog   color   使用   io   問題   div   代碼   

描述:

使用了遞迴,有些計算是重複的,用了額外的空間,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)] = a15 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)] = b21 22         return a + b 23 24 foo = Solution()25 print foo.uniquePaths(1, 2)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.