fastest C++ code


  • 0
    S

    class Solution {
    public:
    bool isPowerOfFour(int num) {
    switch(num){
    case 1:
    return true;
    case 4:
    return true;
    case 16:
    return true;
    case 64:
    return true;
    case 256:
    return true;
    case 1024:
    return true;
    case 4096:
    return true;
    case 16384:
    return true;
    case 65536:
    return true;
    case 262144:
    return true;
    case 1048576:
    return true;
    case 4194304:
    return true;
    case 16777216:
    return true;
    case 67108864:
    return true;
    case 268435456:
    return true;
    case 1073741824:
    return true;
    default:
    return false;
    }
    return false;
    }
    };


  • 0
    X

    lol good point!!! That would be really fast in O(1). !(^o^)b


  • 0

    Strange... it does get accepted despite the dafault typo.


  • 0
    S

    @StefanPochmann i am sorry to make this mistake. I've corrected it.


  • 0

    @shiyi001 No need to be sorry for that, like I said it does work. I'm just wondering why. I'm used to g++ throwing cryptic walls of error messages at me for the slightest mistake, but it doesn't care about the spelling of default? It's strange :-) Maybe one of their developers is a cool gangsta who likes "dafault".


  • 1

    Ah, no, I get it. The dafault: was simply a label, one you could goto. And since it was after a return, it and its return were just useless, were never reached. The solution just still worked because you had the return false; after the switch.


  • 0
    S

    @StefanPochmann But what will happen if I input a '3'? Where return false?Iside switch or outside switch? Why the compiler can not find the spelling error?


  • 0
    S

    @StefanPochmann maybe #define dafault default?


  • 0
    S

    @StefanPochmann I've learnt it. Thank you.


Log in to reply
 

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