[C++] [Java] Clean Code


  • 13

    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;
        }
    }
    

  • 3

    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.


Log in to reply
 

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