Untitled
unknown
plain_text
2 years ago
1.1 kB
6
Indexable
This is a similar question to https://leetcode.com/problems/robot-bounded-in-circle/ The only difference is our input for the function is a List and we need to return "YES" or "NO" for reach command based on if we go in a circle or not. My current code which has some bugs. The assesment is over but what is the correct solution? public static List<String> doesCircleExist(List<String> commands) { // Write your code here int x = 0; int y = 0; int[][] coordinates = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int direction = 0; for(int i = 0; i < commands.size(); ++i) { if(commands[i] == 'L') { direction = (direction + 3) % 4; } else if(commands[i] == 'R') { direction = (direction + 1) % 4; } else { x += coordinates[direction][0]; y += coordinates[direction][1]; } } if((x == 0 & y == 0) || (direction != 0)) { return "YES"; } return "NO";
Editor is loading...