Reverse Integer
בינוני
שאלה מראיונות עבודה ממאגר שאלות של LeetCode שאלה מספר 7
Given a signed 32-bit integer `x`, return `x` with its digits reversed. If reversing `x` causes the value to go outside the signed 32-bit integer range `[-2^31, 2^31 - 1]`, then return `0`.
פתרון קוד
JavaScript
Python
function reverse(x) {
const INT_MIN = -2147483648;
const INT_MAX = 2147483647;
const sign = x < 0 ? -1 : 1;
let n = Math.abs(x);
let rev = 0;
while (n > 0) {
rev = rev * 10 + (n % 10);
n = Math.floor(n / 10);
}
rev *= sign;
if (rev < INT_MIN || rev > INT_MAX) return 0;
return rev;
}הסבר וידאו כיצד לפתור את השאלה

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