【LeetCode-面試演算法經典-Java實現】【062-Unique Paths(唯一路徑)】

來源:互聯網
上載者:User

標籤:mod   func   位置   data   技術分享   ott   var   機器   img   

【062-Unique Paths(唯一路徑)】 【LeetCode-面試演算法經典-Java實現】【全部題目檔案夾索引】 原題

  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?
  
  Above is a 3 x 7 grid. How many possible unique paths are there?


  Note: m and n will be at most 100.

題目大意

  一個機器人在一個m*n的方格的左上方。
  機器人僅僅能向右或都向下走一個方格,機器人要到達右下角的方格。


  請問一共同擁有多少種唯一的路徑。
  注意:m和n最大不超100。

解題思路

  典型的動態規劃問題,對問題使用動態規劃的方法進行求解。
  用一個m*n的組數A儲存結果。
  對於A數組中的元素有。
  1、當x=0或者y=0時有A[x][y] = 1;
  2、當x>=1而且y>=1時有A[\x][\y] = A[x-1][y]+A[\x][y-1]。
  3、所求的結點就是A[m-1][n-1]。

代碼實現

演算法實作類別

public class Solution {    public int uniquePaths(int m, int n) {        int[][] result = new int[m][n];        // 第一列的解        for (int i = 0; i < m; i++) {            result[i][0] = 1;        }        // 第一行的解            for (int i = 1; i < n; i++) {            result[0][i] = 1;        }        // 其他位置的解        for (int i = 1; i < m; i++) {            for (int j = 1; j < n; j++) {                result[i][j] = result[i - 1][j] + result[i][j - 1];            }        }        // 所求的解        return result[m - 1][n - 1];    }}
評測結果

  點擊圖片,滑鼠不釋放。拖動一段位置,釋放後在新的表單中查看完整圖片。

特別說明 歡迎轉載。轉載請註明出處【http://blog.csdn.net/derrantcm/article/details/47182719】

【LeetCode-面試演算法經典-Java實現】【062-Unique Paths(唯一路徑)】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.