Runtime Error, but is Ok on my local machine


  • 0
    J

    Hello, leedcode says I have a runtime error with input [0,1,2], but is OK for my local machine.
    Can anyone tell me what's the problem? Thank you very much.

    class Solution {
        public:
            void sortColors(int A[], int n) {
                vector<int> red;
                vector<int> white;
                vector<int> blue;
                for (int i = 0; i < n; i++){
                    if( A[i]==0){
                        red.push_back(A[i]);
                    }else if( A[i]==1){
                        white.push_back(A[i]);
                    }else if( A[i] ==2){
                        blue.push_back(A[i]);
                    }
                }
                for(int i = 0; i <= 2; i++){
                    switch(i){
                        case 0:{//red
                        if(!red.empty())
                            copy(red.begin(),red.end()+1,A);
                            break;
                        }
                        case 1:{//white
                        if(!white.empty())
                            copy(white.begin(),white.end()+1,&A[red.size()]);
                            break;
                        }
                        default:{//blue
                        if(!blue.empty())
                            copy(blue.begin(),blue.end()+1,&A[red.size()+white.size()]);
                            }
                    }
                }
            }
        };

  • 0

    Please read the FAQ, especially the question: "Why does my code produce a different output compared to my local environment?"


Log in to reply
 

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