Can any one tell me why I am getting runtime error while submitting solution?(while it is working good while running custom testcase)


  • 0
    S
    int maximumSwap(int num) {
            vector<int> d;
            while(num){
                d.push_back(num%10);
                num = num/10;
            }
            reverse(d.begin(),d.end());
            int index,max = INT_MIN; 
            int k;
            for(k = 0;k < d.size()-1;k++){
                if(d[k] < d[k+1]) break;
            }
            for(int i = d.size()-1;i > k;i--){
                if(max < d[i]){
                    max = d[i];
                    index = i;
                }
            }
          // printf("max = %d index = %d k = %d\n",max,index,k);
           for(int i = 0;i < index && index >= 0;i++){
                if(d[index] > d[i]){
                    int temp = d[i];
                    d[i] = d[index];
                    d[index] = temp;
                    break;
                }
            }
            int ten=10;
            for(int i = 0;i < d.size();i++){
                num = d[i] + num*ten;
            }
            d.clear();
            return num;
        }
    

Log in to reply
 

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