Provide and explain the Time Complexity function (T(n)) and the Time Complexity Order of the following code:
Fantastic news! We've Found the answer you've been seeking!
Question:
Provide and explain the Time Complexity function (T(n)) and the Time Complexity Order of the following code:
class plusOne {
public:
vector plusOne(vector& digits) {
int n = digits.size();
if(digits[n-1] != 9) {
digits[n-1] += 1;
} else {
for(int i=n-1; i>=0; i--) {
if(digits[i] == 9 && i != 0) {
digits[i] = 0;
}else if(digits[i] == 9 && i == 0) {
digits[i] = 0;
digits.insert(digits.begin(), 1);
}else{
digits[i] += 1;
break;
}
}
}
return digits;
}
};
Related Book For
Posted Date: