Skip to content

Commit 27bbda5

Browse files
committed
657. 机器人能否返回原点
1 parent 1dd4db2 commit 27bbda5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
|628|[三个数的最大乘积](https://leetcode.cn/problems/maximum-product-of-three-numbers/)|[JavaScript](./algorithms/maximum-product-of-three-numbers.js)|Easy|
110110
|637|[二叉树的层平均值](https://leetcode.cn/problems/average-of-levels-in-binary-tree/)|[JavaScript](./algorithms/average-of-levels-in-binary-tree.js)|Easy|
111111
|645|[错误的集合](https://leetcode.cn/problems/set-mismatch/)|[JavaScript](./algorithms/set-mismatch.js)|Easy|
112+
|657|[机器人能否返回原点](https://leetcode.cn/problems/robot-return-to-origin/)|[JavaScript](./algorithms/robot-return-to-origin.js)|Easy|
112113
|687|[最长同值路径](https://leetcode.cn/problems/longest-univalue-path/)|[JavaScript](./algorithms/longest-univalue-path.js)|Medium|
113114
|700|[二叉搜索树中的搜索](https://leetcode.cn/problems/search-in-a-binary-search-tree/)|[JavaScript](./algorithms/search-in-a-binary-search-tree.js)|Easy|
114115
|704|[二分查找](https://leetcode.cn/problems/binary-search/)|[JavaScript](./algorithms/binary-search.js)|Easy|

Diff for: algorithms/robot-return-to-origin.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {string} moves
3+
* @return {boolean}
4+
*/
5+
var judgeCircle = function (moves) {
6+
// 思路:
7+
// 要回到原点,'L' 出现的次数 等于 'R' 且 'U' 出现的次数等于 'D'
8+
// 空间复杂度: O(n)
9+
10+
let o = {};
11+
12+
for (let char of moves) {
13+
o[char] = (o[char] || 0) + 1;
14+
}
15+
if (o["L"] === o["R"] && o["U"] === o["D"]) {
16+
return true;
17+
} else {
18+
return false;
19+
}
20+
21+
// 思路:
22+
// 将物体的前后左右移动看成是在 xy 坐标轴上的移动
23+
// 空间复杂度: O(1)
24+
25+
// let x = 0,
26+
// y = 0;
27+
28+
// for (let char of moves) {
29+
// if (char === "R") x++;
30+
// if (char === "L") x--;
31+
// if (char === "U") y++;
32+
// if (char === "D") y--;
33+
// }
34+
35+
// return x === 0 && y === 0;
36+
};

0 commit comments

Comments
 (0)