LeetCode: Plus One Solution

1/**
2 * @param {number[]} digits
3 * @return {number[]}
4 */
5var plusOne = function (digits) {
6 let mem = 0
7 digits = digits.reverse().map((digit, i) => {
8 digit += mem + (i === 0 ? 1 : 0)
9 if (digit > 9) {
10 mem = 1
11 digit = 0
12 } else {
13 mem = 0
14 }
15 return digit
16 })
17 if (mem === 1) {
18 digits.push(1)
19 }
20 return digits.reverse()
21}

Comments

Loading comments...

Tags

leetcode

array

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Longest Harmonious Subsequence

HoningJS

Search Posts