HTMLify
LeetCode - Robot Bounded In Circle - Dart
Views: 283 | Author: abh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | class Solution { bool isRobotBounded(String instructions) { List<int> direction = [0, 1], cordinates = [0, 0]; instructions.runes.forEach((int r) { var c = new String.fromCharCode(r); if (c == 'G') { cordinates[0] += direction[0]; cordinates[1] += direction[1]; } if (c == 'L') { List<int> nd = [0, 0]; if (direction[0] == 0 && direction[1] == 1) { nd[0] = -1; nd[1] = 0; } if (direction[0] == 1 && direction[1] == 0) { nd[0] = 0; nd[1] = 1; } if (direction[0] == 0 && direction[1] == -1) { nd[0] = 1; nd[1] = 0; } if (direction[0] == -1 && direction[1] == 0) { nd[0] = 0; nd[1] = -1; } direction[0] = nd[0]; direction[1] = nd[1]; } if (c == 'R'){ List<int> nd = [0, 0]; if (direction[0] == 0 && direction[1] == 1) { nd[0] = 1; nd[1] = 0; } if (direction[0] == 1 && direction[1] == 0) { nd[0] = 0; nd[1] = -1; } if (direction[0] == 0 && direction[1] == -1) { nd[0] = -1; nd[1] = 0; } if (direction[0] == -1 && direction[1] == 0) { nd[0] = 0; nd[1] = 1; } direction[0] = nd[0]; direction[1] = nd[1]; } }); if (((direction[0] != 0 || direction[1] != 1) || (cordinates[0] == 0 && cordinates[1] == 0))) { return true; } return false; } } |