找出是否存在给定移动的圆圈

时间:2016-08-12 03:48:03

标签: java algorithm geometry computational-geometry

我在HackerRank上被问到一个与此类似的问题:Check If there exists a Circle

不同之处在于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”。

2 个答案:

答案 0 :(得分:1)

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()]));

}

答案 1 :(得分:-1)

代码(dir - 1) % 4是否按照您的语言运行?

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

问题编辑后

不实际,但可能有助于缩短运行时间:

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

  1. 最终排量为零(您的解决方案会检查此情况)
  2. 最终方向与初始方向不同(在这种情况下,对象在两轮或四轮后返回初始位置)
  3. 似乎你驳回了第二个。