LeetCode 66. Plus One Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Your thinking and code are clear. I would take issue with repeatedly calling things easy and simple; they are to you as an expert level, but not to those first encountering these problems. Better to say "short" or "concise"

rdubb
Автор

what a great solution brother keep making such amazing leet code videos and helping out the community.

satyamgupta
Автор

thank you Nick. You always have the best solutions at least to me.

SHSelect
Автор

Great solution, thank you! We would also need the following two lines to cover single digit test cases:

if (dLen == 1 && digits[0] == 9) return new int[] { 1, 0 };
if (dLen == 1) return new int[] { digits[0] + 1 };

varghahokmran
Автор

Nice solution! But don't we need to copy the digits array to new_number starting at new_number[1]?

anant
Автор

class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int n = digits.size() - 1;
for (int i = n; i >= 0; --i) {
if (digits[i] == 9) {
digits[i] = 0;
} else {
digits[i]++;
return digits;
}
}
digits.push_back(0);
digits[0] = 1;
return digits;
}
};
without extra space

salmakhaled
Автор

I do like the way u make code efficient . Keep it up

devanshimadhani
Автор

Mannn do not worry about the video I personally love your videos.

ParsiaZahedi
Автор

what do we do if the output cannot have 0s before the most significant digit: 0 1 2 3 (not valid) ?

abhinavtyagi
Автор

a little tricky to understand at the start but as watched it twice, it became clearer. Damn bro u smart

ramanenb
Автор

I think you don't have to put [n+1] in the 14th line. Because it's just putting a 1 in the beginning of the output. All the work is done before. You can just assign it with size 1. Correct me if I am wrong.

peregrine
Автор

the code is very simple and easy to understand.thanks man

abirami
Автор

So simple and easy to understand. Thank You!

shantanugupta
Автор

bro said 100 times that was easy and I was like:"we gotta work harder"

gamerneutro
Автор

how does the system itself assume that there are all 9s, when we go out of the for loop?

ridj
Автор

I have a Doubt, the new array which we made we are not connecting it to the default array in the code, so how does it store the previous values of digits like, digits = [9, 9, 9] and new array new = [1] how are the remaining elements getting stored here.

samarthshiramshetty
Автор

Int [] to int then adding one then converting it to array not passing all case

_Fitness.Band_
Автор

thanks man, ur explanation are just wow

swapnikapandey
Автор

when i saw his previous submitted 10 month earlier my confidence increased. Every expert you see today has been a beginner yesterday. Thank you nick

wahbibaklouti
Автор

I wrote an implementation in Python

class Solution:
def plusOne(self, digits: List[int]) -> List[int]:

for i in range(len(digits)-1, -1, -1):
if digits[i]<9:
digits[i]+=1
return digits
else:
digits[i] = 0

if digits[0]==0:
digits = [1]+digits

return digits

humblesadism
visit shbcf.ru