siddontang
LeetCode题解

Updated 6 months ago

Zell Ho (@zellho) started discussion #41

4 months ago · 0 comments

Open

Pascal's Triangle的解法有問題

for(int i = 0; i < numRows; i++) {

vals[i].resize(i + 1);

vals[i][0] = 1;

vals[i][vals[i].size() - 1] = 1;

for(int j = 1; j < vals[i].size() - 1; j++) {

vals[i][j] = vals[i - 1][j - 1] + vals[i - 1][j]; //i從0開始會掛

}

}

應該是要跳過i=0這組, 直接從i = 1這組開始 =>

vals[0].resize(1);

vals[0][0] = 1;

if(numRows < 2) return vals;

for(int i = 1; i < numRows; i++) {

        vals\[i\].resize\(i + 1\);

        vals\[i\]\[0\] = 1;

        vals\[i\]\[vals\[i\].size\(\) - 1\] = 1;

        for\(int j = 1; j &lt; vals\[i\].size\(\) - 1; j++\) {

            vals\[i\]\[j\] = vals\[**i - 1**\]\[j - 1\] + vals\[i - 1\]\[j\];


        }

    }

No comments on this discussion.


to join this conversation on GitBook. Already have an account? Sign in to comment
Notifications

You’re not receiving notifications from this thread.


1 participant