I keep runtime error when the input is all zeros


  • 0
    V

    I keep getting a runtime error when the input is {0,0,0,0,....}. Can someone help me pin point a mistake ?

    class Solution {
    public:
        static bool comparator_lex(int k,int j){
            if(k==j)return true;
            else{
                string temp1=to_string(k);
                string temp2=to_string(j);
                int i;
                for(i=0;i<min(temp1.size(),temp2.size());i++){
                    if(temp1[i]>temp2[i]){
                        return true;
                    }
                    else if(temp1[i]<temp2[i]){
                        return false;
                    }
                }
                if(temp1.size()==temp2.size()){
                    return true;
                }
                else if(temp1.size()>temp2.size()){
                    if(temp1[i]>=temp1[i-1]){
                        return true;
                    }
                    else{
                        return false;
                    }
                }
                else if(temp1.size()<temp2.size()) {
                    if(temp2[i]>=temp2[i-1]){
                        return false;
                    }
                    else {
                        return true;
                    }
                }
            }
        }
        
        string largestNumber(vector<int> &num) {
            sort(num.begin(),num.end(),comparator_lex);
            string ans;
            for(int i=0;i<num.size();i++){
                ans+=to_string(num[i]);
            }
            return ans;
        }
    };

  • 1
    D

    First, change true to false in this statement:
    if(temp1.size()==temp2.size()){
    return true;
    }
    std::sort() will run into dead loop if you return true to equal value.

    Second, there are other problem with rest of your compare function, leave it to yourself to figure out.


  • 0
    S

    thank you !!!!!


Log in to reply
 

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