Java 查找给定移动是否存在一个圆

Java 查找给定移动是否存在一个圆,java,algorithm,geometry,computational-geometry,Java,Algorithm,Geometry,Computational Geometry,我被问到一个类似于HackerRank的问题: 不同之处在于F被G取代(即G表示向前一步) 我实现了一个类似于公认答案中描述的算法,但有一个测试用例失败。有人能帮我找到窃听器吗 static String checkIfCircleExists(String s) { int x = 0; int y = 0; int dir = 0; for (char c : s.toCharArray()) { sw

我被问到一个类似于HackerRank的问题:

不同之处在于F被G取代(即G表示向前一步)

我实现了一个类似于公认答案中描述的算法,但有一个测试用例失败。有人能帮我找到窃听器吗

static String checkIfCircleExists(String s) {
        int x = 0;
        int y = 0;
        int dir = 0;

        for (char c : s.toCharArray()) {
            switch (c) {
                case 'G':
                    switch (dir) {
                        case 0:
                            y++;
                            break;
                        case 1:
                            x++;
                            break;
                        case 2:
                            y--;
                            break;
                        case 3:
                            x--;
                            break;
                    }
                    break;
                case 'L':
                    dir = Math.abs((dir - 1) % 4);
                    break;
                case 'R':
                    dir = Math.abs((dir + 1) % 4);
                    break;
            }
        }

        if (x == 0 && y == 0) {
            return "YES";
        }
        return "NO";
    }
编辑

这是一个助手方法。此帮助器方法的输入是原始输入字符串,它与原始输入字符串的三个副本连接在一起。例如,对于输入“G”,将在此帮助程序中传递“GGGG”。

代码
(dir-1)%4
是否按您的语言预期工作

如果没有,请将其替换为
(dir+3)%4

编辑问题后不实际,但可能有助于减少运行时间:

请注意,移动仅限于两种情况:

  • 最终位移为零(您的解决方案检查此情况)
  • 最终方向与初始方向不同(在本例中,对象在两轮或四轮后返回初始位置)
  • 看来你把第二个解雇了

    静态字符串[]doesCircleExist(字符串[]命令){
    
    static String[] doesCircleExist(String[] commands) {
    
        int initialX = 0;
        int initialY = 0;
    
        int x = 0;
        int y = 0;
        String direction = "north";
        ArrayList<String> res = new ArrayList<String>();
        for (int i = 0; i < commands.length; i++) {
            for (int j = 0; j < commands[i].length(); j++) {
                if (direction.equals("north")) {
                    if (commands[i].charAt(j) == 'G') {
                        y++;
                    } else if (commands[i].charAt(j) == 'L') {
                        direction = "west";
                    } else if (commands[i].charAt(j) == 'R') {
                        direction = "east";
                    } else {
                        System.out.println("Wrong command");
                    }
                } else if (direction.equals("east")) {
                    if (commands[i].charAt(j) == 'G') {
                        x++;
                    } else if (commands[i].charAt(j) == 'L') {
                        direction = "north";
                    } else if (commands[i].charAt(j) == 'R') {
                        direction = "south";
                    } else {
                        System.out.println("Wrong command");
                    }
                } else if (direction.equals("south")) {
                    if (commands[i].charAt(j) == 'G') {
                        y--;
                    } else if (commands[i].charAt(j) == 'L') {
                        direction = "east";
                    } else if (commands[i].charAt(j) == 'R') {
                        direction = "west";
                    } else {
                        System.out.println("Wrong command");
                    }
                } else if (direction.equals("west")) {
                    if (commands[i].charAt(j) == 'G') {
                        x--;
                    } else if (commands[i].charAt(j) == 'L') {
                        direction = "south";
                    } else if (commands[i].charAt(j) == 'R') {
                        direction = "north";
                    } else {
                        System.out.println("Wrong command");
                    }
                }
            }
    
            if (direction.equals("north")
                    && (((x - initialX) * (x - initialX) + (y - initialY) * (y - initialY)) > 0)) {
                res.add("NO");
            } else {
                res.add("YES");
            }
        }
        return res.toArray((new String[res.size()]));
    
    }
    
    int initialX=0; int initialY=0; int x=0; int y=0; 字符串方向=“北”; ArrayList res=新的ArrayList(); for(int i=0;i0)){ 决议添加(“否”); }否则{ 决议添加(“是”); } } 返回res.toArray((新字符串[res.size()]); }
    很抱歉,我忘了添加,我已经修改了输入字符串,使其成为原始输入字符串+3个副本。i、 e.对于输入字符串G,将检查GGGG。为什么最终方向必须与初始方向不同?是的,在4倍字符串的情况下,方向并不重要。代码
    (dir-1)%4
    是否按照您的语言预期工作?如果不是,则替换为
    (dir+3)%4