Open azl397985856 opened 2 years ago
模拟上下左右 两个变量记录
class Solution {
public boolean judgeCircle(String moves) {
int x=0,y=0;
for (char c : moves.toCharArray()) {
if(c == 'R') x++;
if(c == 'L') x--;
if(c == 'U') y++;
if(c == 'D') y--;
}
return x == 0 && y== 0;
}
}
/*
* @lc app=leetcode.cn id=657 lang=javascript
*
* [657] 机器人能否返回原点
*/
// @lc code=start
/**
* @param {string} moves
* @return {boolean}
*/
var judgeCircle = function(moves) {
let x = 0
let y = 0
for (let i = 0; i < moves.length; i++) {
const move = moves[i];
if (move === 'U') {
y++
}
if (move === 'D') {
y--
}
if (move === 'R') {
x++
}
if (move === 'L') {
x--
}
}
return x === 0 && y === 0
};
class Solution {
public boolean isRobotBounded(String instructions) {
// north 0, east 1, south 2, west = 3
// index 0
// R -> (status + 1) / 4
// l -> (status + 3) / 4
int[][] directions = new int[][] {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int x = 0, y = 0, index = 0;
for (char i : instructions.toCharArray()) {
if (i == 'L') index = (index + 3) % 4;
else if (i == 'R') index = (index + 1) % 4;
else {
x += directions[index][0];
y += directions[index][1];
}
}
return (x == 0 && y == 0) || index != 0;
}
}
代码
class Solution {
public:
bool judgeCircle(string moves) {
int x = 0;
int y = 0;
for (const auto& move : moves)
{
switch (move)
{
case 'U':
y--;
break;
case 'D':
y++;
break;
case 'R':
x++;
break;
case 'L':
x--;
break;
}
}
return x == 0 && y == 0;
}
};
复杂度分析
时间复杂度:O(n)
空间复杂度:O(1)
class Solution {
public:
bool judgeCircle(string moves) {
int left = 0, up = 0;
for(auto& m : moves){
if (m == 'U')
up++;
else if (m == 'D')
up--;
else if (m == 'L')
left++;
else
left--;
}
if(!left && !up)
return true;
return false;
}
};
模拟
class Solution:
def judgeCircle(self, moves: str) -> bool:
x = y = 0
for move in moves:
if move == 'U': y -= 1
elif move == 'D': y += 1
elif move == 'L': x -= 1
elif move == 'R': x += 1
return x == y == 0
简单模拟
class Solution:
def judgeCircle(self, moves: str) -> bool:
move_map = {'R':(1,0),'L':(-1,0),'U':(0,1),'D':(0,-1)}
start = (0,0)
for move in moves:
start = (start[0]+move_map[move][0],start[1]+move_map[move][1])
if start[0]!=0 or start[1]!=0:
return False
return True
复杂度分析
模拟
class Solution {
public boolean judgeCircle(String moves) {
char[] chars = moves.toCharArray();
int ud = 0;
int lr = 0;
for (int i = 0; i < moves.length(); i++) {
char c = moves.charAt(i);
switch(c){
case 'U':
ud=ud+1;
break;
case 'D':
ud--;
break;
case 'L':
lr--;
break;
case 'R':
lr++;
break;
}
}
return ud==0 && lr==0;
}
}
模拟
/**
* @param {string} moves
* @return {boolean}
*/
var judgeCircle = function(moves) {
let x=0,y=0
for(let i=0;i<moves.length;i++) {
let move = moves[i]
if(move === 'U') y++
if(move === 'D') y--
if(move === 'R') x++
if(move === 'L') x--
}
return x === 0 && y=== 0
};
时间复杂度:O(n)
空间复杂度: O(1)
class Solution:
def judgeCircle(self, moves: str) -> bool:
dic= {"R": 0,"L":0,"U":0,"D":0}
for i in moves:
dic[i] += 1
if dic['R'] == dic['L'] and dic['U'] == dic['D']:
return True
else:
return False
class Solution:
def judgeCircle(self, moves: str) -> bool:
x, y = 0, 0
dct = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1)}
for i in moves:
a, b = dct[i]
x, y = x+a, y+b
return x == 0 and y == 0
如果机器人L和R方向走的步数一样,U和D走的步数一样,机器人回到原点
class Solution:
def judgeCircle(self, moves: str) -> bool:
l, r, u, d = 0, 0, 0, 0
for chara in moves:
if chara == "L":
l += 1
elif chara == "R":
r += 1
elif chara == "U":
u += 1
else:
d += 1
return l == r and u == d
时间复杂度:O(n) 空间复杂度:O(1)
657. 机器人能否返回原点
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/robot-return-to-origin/
前置知识
- 模拟
题目描述
在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。 移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。 注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。 示例 1: 输入: "UD" 输出: true 解释:机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。 示例 2: 输入: "LL" 输出: false 解释:机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回原点。
它怎么走我怎么干
class Solution: def judgeCircle(self, moves: str) -> bool: pos=[0,0] for i in range(len(moves)): if moves[i]=='U': pos[0]-=1 elif moves[i]=='D': pos[0]+=1 elif moves[i]=='L': pos[1]-=1 elif moves[i]=='R': pos[1]+=1 if pos[0]==0 and pos[1]==0: return True else: return False
时间复杂度:O(n) 空间复杂度:O(1)
class Solution(object):
def judgeCircle(self, moves):
x = y = 0
for move in moves:
if move == 'U': y -= 1
elif move == 'D': y += 1
elif move == 'L': x -= 1
elif move == 'R': x += 1
return x == y == 0
class Solution:
def judgeCircle(self, moves: str) -> bool:
x, y = 0, 0
dct = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1)}
for i in moves:
a, b = dct[i]
x, y = x+a, y+b
return x == 0 and y == 0
上下y轴+-左右x轴+-看最终位置
class Solution:
def judgeCircle(self, moves: str) -> bool:
if moves == None:
return True
row = 0
col = 0
for move in moves:
if move == 'U':
col += 1
if move == 'D':
col -= 1
if move == 'L':
row -= 1
if move == 'R':
row += 1
if row == 0 and col == 0:
return True
return False
复杂度 时间:O(n) 空间:O(1)
(mark)
const judgeCircle = function (moves) {
const map = new Map();
const movesArr = moves.split("");
for (let el of movesArr) {
map.has(el) ? map.set(el, map.get(el) + 1) : map.set(el, 1);
}
let udEval = map.get("U") === map.get("D") ? true : false;
let lrEval = map.get("L") === map.get("R") ? true : false;
return udEval && lrEval;
};
class Solution {
public boolean judgeCircle(String moves) {
if(moves==null||moves.length()==0) {
return true;
}
int right=0,down=0;
for(int i=0;i<moves.length();i++) {
char c = moves.charAt(i);
if(c=='U') {
down--;
}else if(c=='D'){
down++;
}else if(c=='L') {
right--;
}else {
right++;
}
}
return right==0&&down==0;
}
}
bool judgeCircle(string moves) {
int x = 0;
int y = 0;
for (const auto& move : moves) {
switch (move) {
case 'U':
y--;
break;
case 'D':
y++;
break;
case 'R':
x++;
break;
case 'L':
x--;
break;
}
}
return x == 0 && y == 0;
}
657. 机器人能否返回原点
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/robot-return-to-origin/
前置知识
题目描述
移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。
注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。
示例 1:
输入: "UD" 输出: true 解释:机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。
示例 2:
输入: "LL" 输出: false 解释:机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回原点。