-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJudgeRouteCircle.java
More file actions
74 lines (66 loc) · 1.98 KB
/
JudgeRouteCircle.java
File metadata and controls
74 lines (66 loc) · 1.98 KB
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
63
64
65
66
67
68
69
70
71
72
73
/**
* Created by devpriyadave on 2/19/18.
* Initially, there is a Robot at position (0, 0).
* Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.
The move sequence is represented by a string. And each move is represent by a character.
The valid robot moves are R (Right), L (Left), U (Up) and D (down).
The output should be true or false representing whether the robot makes a circle.
Input: "UD"
Output: true
Input: "LL"
Output: false
*/
public class JudgeRouteCircle {
public boolean myjudgeCircle(String moves) {
if(moves.length() == 0)
return false;
char [] stack = new char[moves.length()];
int ud = 0;
int rl = 0;
for(int i = 0; i < moves.length(); i++) {
char nextMove = moves.charAt(i);
switch (nextMove) {
case 'U':
ud++;
break;
case 'D':
ud--;
break;
case 'L':
rl++;
break;
case 'R':
rl--;
break;
default:
break;
}
}
return ud == 0 && rl ==0;
}
public boolean judgeCircle(String moves) {
char m[] = moves.toCharArray();
int U = gs('U', m);
int D = gs('D', m);
int L = gs('L', m);
int R = gs('R', m);
boolean Re;
if(U==D&&L==R)
Re = true;
else
Re = false;
return Re;
}
public int gs(char zm, char zfsz[]){
int n = 0;
for(int i = 0; i < zfsz.length; i++){
if(zfsz[i] == zm)
n++;
}
return n;
}
public static void main(String[] args) {
JudgeRouteCircle judgeRouteCircle = new JudgeRouteCircle();
System.out.println(judgeRouteCircle.myjudgeCircle("RLUURDDDLU"));
}
}