Why two loop is faster than one?


  • 0
    M
    int toRet = 0;
    for(char c: s.toCharArray()) {
        toRet ^= c;
    }
    for(char c: t.toCharArray()) {
        toRet ^= c;
    }
    return (char) toRet;
    

    I submit twice, both run time are 6ms.

    char c = 0;
    for (int i = 0; i < s.length(); ++i) {
        c ^= s.charAt(i);
        c ^= t.charAt(i);
    }
    c ^= t.charAt(t.length() - 1);
    return c;
    

    Also submit twice, both are 8ms.

    WHYWHYWHYWHWY?!?!?!?


Log in to reply
 

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