-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution.js
More file actions
45 lines (40 loc) 路 1.01 KB
/
Copy pathSolution.js
File metadata and controls
45 lines (40 loc) 路 1.01 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
/**
* @param {string} moves
* @return {boolean}
*/
var judgeCircle = function(moves) {
var length = moves.length, x = 0, y = 0;
for (i = 0; i < length; i++) {
switch(moves[i]) {
case 'L':
x--;
break;
case 'R':
x++;
break;
case 'U':
y++;
break;
case 'D':
y--;
break;
}
}
return x === 0 && y === 0;
};
/**
* @param {string} moves
* @return {boolean}
*/
var judgeCircle = function(moves) {
var countL, countR, countU, countD;
countL = moves.match(/L/g);
countL = countL === null ? 0 : countL.length;
countR = moves.match(/R/g);
countR = countR === null ? 0 : countR.length;
countU = moves.match(/U/g);
countU = countU === null ? 0 : countU.length;
countD = moves.match(/D/g);
countD = countD === null ? 0 : countD.length;
return countL === countR && countD === countU;
};