Skip to content

Commit

Permalink
solve da questão plusOne leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 committed Jul 27, 2024
1 parent 3beb1c0 commit acfda50
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions leetcode_problemset/problemsets/plus_one.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

vector<int> plusOne(vector<int> &digits) {

vector<int> ans;
reverse(digits.begin(),digits.end());

int sum = (digits[0] + 1);
int carry = sum/10;

ans.emplace_back(sum%10);

for(int i = 1; i < (int)digits.size(); ++i) {
sum = ((digits[i])+carry);
carry = sum/10;
ans.emplace_back(sum%10);
}

if(carry) ans.emplace_back(carry);

reverse(ans.begin(),ans.end());
return ans;

}

int main(){

vector<int> test = {9};
vector<int> res = plusOne(test);

for(auto x : res)
cout << x << " ";
cout << endl;

return 0;
}

0 comments on commit acfda50

Please sign in to comment.