What's the problem with the bool?


  • 0
    A

    Hi all,

    I just figured out how to solve a runtime error in Q345 but I don't know why this happened.

    The error one:

    string reverseVowels(string s) {
        int len = s.length();
        bool sf = false, ef = false;
        int i = 0,j = 0;
        int sv = 0;
        int ev = len;
        while(true){
            char a = s[i];
            char b = s[len-1-j];
            if(!sf){
                if(a == 'a'||a =='e'||a=='i'||a=='o'||a=='u'||a == 'A'||a =='E'||a=='I'||a=='O'||a=='U'){
                    sv = i;
                    sf = true;
                }
                i++;
            }
            
            if(!ef){
            if(b == 'a'||b =='e'||b=='i'||b=='o'||b=='u'||b == 'A'||b =='E'||b=='I'||b=='O'||b=='U'){
                ev = len-1-j;
                ef = false;
            }
            j++;
            }
            
            if(sf&&ef){
                if(sv>=ev) break;
                char c = s[sv];
                s[sv]= s[ev];
                s[ev]= c;
                sf = false;
                ef = false;
                //sv = 0;
                //ev = len;
            }
            
        }
        return s;
    

    The problem is in the two bool variables 'sf' and 'ef'. Use 'int' instead of 'bool' and then it works.

    Could anyone tell me exactly the reason I can't use 'bool' here?

    Thank you!!!


Log in to reply
 

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