🤖המדריך להייטיקיסט המתחיל

Zigzag Conversion

בינוני
שאלה מראיונות עבודה ממאגר שאלות של LeetCode שאלה מספר 6
Convert the string `s` to a zigzag pattern on a given number of rows `numRows`, and then read line by line to create the final string.

פתרון קוד

JavaScript
Python
function convert(s, numRows) {
  if (numRows === 1 || s.length <= numRows) return s;
  const rows = Array.from({ length: numRows }, () => []);
  let row = 0;
  let direction = -1;
  for (const ch of s) {
    rows[row].push(ch);
    if (row === 0 || row === numRows - 1) direction *= -1;
    row += direction;
  }
  return rows.map(r => r.join("")).join("");
}

הסבר וידאו כיצד לפתור את השאלה

Book Cover

לעבור את ראיון העבודה הבא שלך בהצלחה

קורס דיגיטלי מקיף עם +25 שיעורים מעשיים, כשעתיים של וידאו, וליווי של מראיין בכיר.

התחל עכשיו ב-99 ₪ בלבד! 🚀