LeetCode 66 Plus One (Plus One) (vector)

Source: Internet
Author: User

LeetCode 66 Plus One (Plus One) (vector)
Translation

Given a non-negative number represented by a series of numbers, add them and convert them into numbers. The highest bit of digital storage is at the top of the column.
Original
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.
Analysis

As soon as I saw this question, I remembered the previous question about finding all the numbers contained in A number. Then I used this method here.

First, combine the number in the vector into a number, then add one to the number, and then convert the number into a vector for storage.

    int powTen(int num, int times) {        for (int i = 0; i < times; ++i) {            num *= 10;        }        return num;    }    int getNumFromVector(vector
  
   & digits) {        int num = 0;        for (int i = 0; i < digits.size(); ++i) {            num += powTen(digits[i], digits.size() - i - 1);        }        return num;    }    vector
   
     getVectorFromNum(int num) {        vector
    
      v;        stack
     
       s;        while (num >= 10) {            s.push(num % 10);            num /= 10;        }        s.push(num);        while (!s.empty()) {            v.push_back(s.top());            s.pop();        }        return v;    }    vector
      
        plusOne(vector
       
        & digits) { int num = getNumFromVector(digits); num += 1; return getVectorFromNum(num); }
       
      
     
    
   
  

However, it was later found that it could not be passed, so I changed the int to long. I still don't believe it. Well, I don't want to write this question ...... If it's okay, let's change it. There are some ideas.

For specific code, see the following article. First, add 1 to the last number of the vector. Then, start traversing and determine whether the number is greater than 10 to perform corresponding operations. OK, all you can do here.

If the first number is 9, then 1 is changed to 10, or if the other number is greater than 10, the following if judgment is enabled, which accounts for half of the entire code. Because the whole vector needs to be reorganized. In fact, I think this method is relatively simple to write code, but it is not very intuitive to understand, so I still prefer the above Code, even if it does not match the purpose of the question, but it is more abstract.

Code
class Solution {public:    vector
  
    plusOne(vector
   
    & digits) {        digits[digits.size() - 1] += 1;        for (int i = digits.size() - 1; i > 0; --i) {            if (digits[i] >= 10) {                digits[i] -= 10;                digits[i - 1] += 1;            }        }        if (digits[0] >= 10) {            vector
    
      newVec;            newVec.push_back(digits[0] - 9);            digits[0] -= 10;            for (auto i : digits) {                newVec.push_back(i);            }            return newVec;        }        return digits;    }};
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.