Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 715d5af

Browse files
add 657
1 parent 3877930 commit 715d5af

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|657|[Judge Route Circle](https://leetcode.com/problems/judge-route-circle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_657.java) | O(n) | O(1) | Easy |
2526
|654|[Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_654.java) | O(n^2) | O(n) | Medium | Tree
2627
|653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_653.java) | | | Easy | Tree
2728
|652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_652.java) | O(n) |O(n) | Medium | Tree
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 657. Judge Route Circle
5+
*
6+
* Initially, there is a Robot at position (0, 0).
7+
* Given a sequence of its moves, judge if this robot makes a circle,
8+
* which means it moves back to the original place.
9+
10+
The move sequence is represented by a string.
11+
And each move is represent by a character.
12+
The valid robot moves are R (Right), L (Left), U (Up) and D (down).
13+
The output should be true or false representing whether the robot makes a circle.
14+
15+
Example 1:
16+
Input: "UD"
17+
Output: true
18+
19+
Example 2:
20+
Input: "LL"
21+
Output: false
22+
*/
23+
24+
public class _657 {
25+
public boolean judgeCircle(String moves) {
26+
int hori = 0;
27+
int verti = 0;
28+
for (char c : moves.toCharArray()) {
29+
if (c == 'U') {
30+
verti++;
31+
} else if (c == 'D') {
32+
verti--;
33+
} else if (c == 'L') {
34+
hori--;
35+
} else if (c == 'R') {
36+
hori++;
37+
}
38+
}
39+
return verti == 0 && hori == 0;
40+
}
41+
}

0 commit comments

Comments
 (0)