Simple one line code get the result (java)


  • 13
    X
    public boolean canWinNim(int n) {
        return n % 4 != 0;
    }
    

    P.s. It passed test case, but it didn't cover negative input

    canWinNim(-1);
    

    I guess we don't have to care for illegal input when submit solution here.


  • 0
    A

    One question,in the example it says if there are 4 stones,you will never win ,so let say i have 4 stones,i removed 1 then my friend removed 2 and then I will be the winner as I will remove the last 1 stone


  • 0
    H

    The question said : assume your friend knows the rules and is clever. :)


  • 2
    L

    return n & 3; // how about this?


  • 0
    B

    hahahahahaahahaha


  • -6
    S

    how about
    public boolean canWinNim(int n) {
    return n % 2 != 0;
    }

    what i see is all the odd numbers there is a chance to win ...


Log in to reply
 

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