Plus One @LeetCode
PROBLEM :
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int carry=1 ;
int size=digits.size() ;
for(int j=size-1;j>=0;j--){
digits[j]=digits[j]+carry ;
carry=digits[j]/10 ;
digits[j]=digits[j]%10 ;
}
if(carry)
digits.insert(digits.begin(),carry) ;
return digits ;
}
};
--------------------------------------------------------------------------------
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int carry=1 ;
int size=digits.size() ;
for(int j=size-1;j>=0;j--){
digits[j]=digits[j]+carry ;
carry=digits[j]/10 ;
digits[j]=digits[j]%10 ;
}
if(carry)
digits.insert(digits.begin(),carry) ;
return digits ;
}
};
--------------------------------------------------------------------------------
Comments
Post a Comment