[C++] [Java] Clean Code


  • 19

    C++

    class Solution {
    public:
        bool judgeCircle(string moves) {
            int v = 0;
            int h = 0;
            for (char ch : moves) {
                switch (ch) {
                    case 'U' : v++; break;
                    case 'D' : v--; break;
                    case 'R' : h++; break;
                    case 'L' : h--; break;
                }
            }
            return v == 0 && h == 0;
        }
    };
    

    Java

    public class Solution {
        public boolean judgeCircle(String moves) {
            int x = 0;
            int y = 0;
            for (char ch : moves.toCharArray()) {
                if (ch == 'U') y++;
                else if (ch == 'D') y--;
                else if (ch == 'R') x++;
                else if (ch == 'L') x--;
            }
            return x == 0 && y == 0;
        }
    }
    

  • 0
    F

    Same idea

    class Solution {
        public boolean judgeCircle(String moves) {
            int x = 0, y = 0;
            for (char c : moves.toCharArray()) {
                if(c == 'U') {
                    x++;
                }else if(c == 'D') {
                    x--;
                }else if(c == 'L') {
                    y--;
                }else {
                    y++;
                }
            }
            return x == 0 && y == 0;
        }
    }
    

  • 6

    Why didn't you guys use switch statement in Java code? I think having those multiple if-else statements is a bad practice and the switch statement fits the best in this problem.

        public boolean judgeCircle(String moves) {
            int v = 0, h = 0;
            for (char move : moves.toCharArray()) {
                switch (move) {
                    case 'U': v++; break;
                    case 'D': v--; break;
                    case 'R': h++; break;
                    case 'L': h--; break;
                }
            }
            return v == 0 && h == 0;
        }
    
    

  • 0
    Y

    Thanks for sharing the solutions. It seems like switch is more readable. Performance wise, it beats 90% but "else if" statements are > 60%, I was wondering the reason why switch is faster. Could anyone explain that? Thanks.


  • 1
    Y

    @alexander Nice. Looks like it will be better to add "else if" or "break" in the java solution to avoid unnecessary checks.


  • 0
    B

    @Alexander Hello, for the input case "RLUURDDDLU" my code does not work correctly according to solution, but I have tested many times the output should be also true according to my code but it gives false. Is there any problem could you please help me since this solution is pretty straightforward

    Thank you

    bool judgeCircle(string moves) {
        
        int u, l, r, d = 0 ;
        for(int i = 0 ; i < moves.size() ; i++)
        {   
            
            if(moves[i] == 'U')
                u++;
            else if(moves[i] =='D')
                d++;
            else if(moves[i] == 'L')
                l++;
            else if(moves[i] == 'R')
                r++;
        }
        
        
        if (u == d &&  l==r)
            return true;
        return false;
    }

  • 0

  • 1
    K

    @Yueyi-Wang The history of Java switch statement has a problem (prior to jdk 1.7) like only valid for non-long type. Even after jdk 1.7, If you pass a String type variable, you cannot pass the variable like Object str = "foo" to switch statement. It has other minor issues you need be careful when you want to use it. I guess that's the main reason for people like me refuse to use Java switch.


  • 0
    W

    I think you did not consider the case when the input contains invalid parameters, such as "UDp", your code will return true, but it should return false.


  • 0

    @kenan3 I'm not familiar with Java switch, but generally speaking, switch is faster than if-else(even though it's small tho) and make it logically clear - All cases have the same access time; It comes with the same order.

    Not I'm not sure if most modern compilers optimize deep if/else..if else point as a switch/jump table.


  • 0
    R

    @Yueyi-Wang maybe they don't like break.


  • 0
    K

    @new2500 Thanks for pointing this out. You are right. When the size of if-else statements are big, we can get benefits from changing from if-else to switch-case.

    I should've said "I prefer to use if-else when the if-else statements are smaller than 5 or something".

    A minor problem of your post "All cases have the same access time" is not always true. Since CPU has prediction and pre-fetch instruction mechanism. Branches are always the performance bottleneck, so it cannot guarantee the same access time (especially when those instructions are supposed to be executed multiple times).

    For your last statement, I think most static compilers for non-VM (it's not a term) languages can do this optimization very easily. But for java, it would leave optimization until JIT at runtime.


  • 0
    H

    you guys all seem to forget that if you go left, left, left, left (LLLL), you're back to the original spot. But the code would not work for this.


  • 0

    @huyuci I think in here left means not making a turn and turn left, but just literally left.


  • 0
    H

    @alexander Gotcha. Silly me :-) I didn't read carefully


Log in to reply
 

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