I like to read this solution as a small machine: keep the useful information, throw away the noise. For 657. Judge Route Circle, the solution in this repository is mainly a two pointers solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are judgeCircle.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N)O(N), where NN is the length of moves. We iterate through the string.
- Space: O(1)O(1). In Java, our character array is O(N)O(N).
Guide
C++ Solution
Your submission
The accepted solution
01/**
02There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
03
04The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.
05
06Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
07
08Example 1:
09
10Input: "UD"
11Output: true
12Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
13
14
15Example 2:
16
17Input: "LL"
18Output: false
19Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.
20**/
21
22/**
23Complexity Analysis
24
25Time Complexity: O(N)O(N), where NN is the length of moves. We iterate through the string.
26
27Space Complexity: O(1)O(1). In Java, our character array is O(N)O(N).
28**/
29
30//Your runtime beats 50.93 % of cpp submissions.
31/**
32class Solution {
33public:
34 bool judgeCircle(string moves) {
35 int x = 0, y = 0;
36 for(int i = 0; i< moves.size(); i++){
37 switch(moves[i]){
38 case 'R':
39 x+=1;
40 break; //don't forget the break!
41 case 'L':
42 x-=1;
43 break;
44 case 'U':
45 y+=1;
46 break;
47 case 'D':
48 y-=1;
49 }
50 }
51 return (x==0 && y==0);
52 }
53};
54**/
55
56class Solution {
57public:
58 bool judgeCircle(string moves) {
59 int x = 0, y = 0;
60 for(int i = 0; i< moves.size(); i++){
61 switch(moves[i]){
62 case 'U':
63 case 'R':
64 x+=1;
65 break;
66 case 'L':
67 case 'D':
68 x-=1;
69 }
70 }
71 return x==0;
72 }
73};
Cost