תיאור השאלה
Given an integer numRows, return the first numRows rows of Pascal's triangle, where each number is the sum of the two numbers directly above it.
איך פותרים?
כל שורה מתחילה ומסתיימת ב־1, וכל איבר באמצע הוא סכום שני האיברים שמעליו בשורה הקודמת. בונים שורה אחרי שורה. סיבוכיות: O(numRows²).
פתרון קוד
JavaScript
Python
function generate(numRows) {
const rows = [];
for (let i = 0; i < numRows; i++) {
const row = new Array(i + 1).fill(1);
for (let j = 1; j < i; j++) {
row[j] = rows[i - 1][j - 1] + rows[i - 1][j];
}
rows.push(row);
}
return rows;
}
לעבור את ראיון העבודה הבא שלך בהצלחה
קורס דיגיטלי מקיף עם +25 שיעורים מעשיים, כשעתיים של וידאו, וליווי של מראיין בכיר.