Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

杨辉三角 II #94

Open
louzhedong opened this issue Nov 23, 2018 · 0 comments
Open

杨辉三角 II #94

louzhedong opened this issue Nov 23, 2018 · 0 comments

Comments

@louzhedong
Copy link
Owner

习题

出处 LeetCode 算法第119题

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。

img

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 3
输出: [1,3,3,1]

思路

二维数组,或者一维数组

解答

/**
 * @param {number} rowIndex
 * @return {number[]}
 */
var getRow = function (rowIndex) {
  var result = [];
  for (var i = 0; i < rowIndex + 1; i++) {
    var temp = [];
    for (var j = 0; j <= i; j++) {
      if (j == 0 || j == i) {
        temp.push(1);
      } else {
        temp.push(result[i - 1][j - 1] + result[i - 1][j]);
      }
    }
    result.push(temp);
  }
  return result[rowIndex];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant