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

Commit 96fb569

Browse files
committed
Add solution #657
1 parent f6c09b3 commit 96fb569

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

0657-robot-return-to-origin.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 657. Robot Return to Origin
3+
* https://leetcode.com/problems/robot-return-to-origin/
4+
* Difficulty: Easy
5+
*
6+
* There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence
7+
* of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
8+
*
9+
* You are given a string moves that represents the move sequence of the robot where moves[i]
10+
* represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down).
11+
*
12+
* Return true if the robot returns to the origin after it finishes all of its moves, or false
13+
* otherwise.
14+
*
15+
* Note: The way that the robot is "facing" is irrelevant. 'R' will always make the robot move
16+
* to the right once, 'L' will always make it move left, etc. Also, assume that the magnitude
17+
* of the robot's movement is the same for each move.
18+
*/
19+
20+
/**
21+
* @param {string} moves
22+
* @return {boolean}
23+
*/
24+
/**
25+
* @param {string} moves
26+
* @return {boolean}
27+
*/
28+
var judgeCircle = function(moves) {
29+
let x = 0;
30+
let y = 0;
31+
32+
for (const move of moves) {
33+
x += move === 'R' ? 1 : move === 'L' ? -1 : 0;
34+
y += move === 'U' ? 1 : move === 'D' ? -1 : 0;
35+
}
36+
37+
return x === 0 && y === 0;
38+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@
494494
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv-input-is-a-bst.js)|Easy|
495495
654|[Maximum Binary Tree](./0654-maximum-binary-tree.js)|Medium|
496496
655|[Print Binary Tree](./0655-print-binary-tree.js)|Medium|
497+
657|[Robot Return to Origin](./0657-robot-return-to-origin.js)|Easy|
497498
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
498499
684|[Redundant Connection](./0684-redundant-connection.js)|Medium|
499500
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|

0 commit comments

Comments
 (0)