Runtime error with working code.


  • 0
    S

    Hi!
    I get "Runtime error" with "Last executed input: [0]".
    When I run next code on my machine, everything is ok:

    #include <iostream>
    #include <set>
    #include <vector>
    
    using namespace std;
    class Solution {
    public:
        vector<int> plusOne(vector<int> &digits) {
            int origin = 0;
            int degree = 1;
            for (int i = digits.size() - 1; i>-1; --i)
            {
                origin+=digits[i]*degree;
                degree *= 10;
            }
            origin++;
    
            vector<int> result;
            if (origin % degree == 0) {
                result.push_back(1);
                int i = 1;
                while (i <= digits.size()) {
                    result.push_back(0);
                    ++i;
                }
            } else {
                degree /= 10;
                while (degree != 0)
                {
                    int integ = origin/degree;
                    origin = origin%degree;
                    degree /= 10;
                    result.push_back(integ);
                }
            }
            return result;
        }
    };
    int main()
    {
        vector<int> vec;
        vec.push_back(0);
        Solution solution;
        vector<int> res = solution.plusOne(vec);
        for (int i = 0; i<res.size(); ++i)
        {
            cout << res[i] << endl;
        }
        return 0;
    }
    

    Class is definitely the same as in my solution.
    Compiler: gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)
    What is wrong with my code?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.