leetcode-pp / 91alg-9-daily-check

5 stars 0 forks source link

【Day 32 】2022-12-02 - 657. 机器人能否返回原点 #39

Open azl397985856 opened 1 year ago

azl397985856 commented 1 year ago

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,因为它在移动结束时没有返回原点。

Ryanbaiyansong commented 1 year ago

··· class Solution: def judgeCircle(self, moves: str) -> bool: dirs = { "R": (0, 1), "L":(0, -1), "D":(1, 0), "U":(-1, 0) } start = [0, 0] for v in moves: dx, dy = dirs[v] start[0] += dx start[1] += dy return start == [0, 0]

···

steven72574 commented 1 year ago

思路:简单模拟。

class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0,y=0;
        char[] c = moves.toCharArray();
        for(char each:c){
            if(each-'U' == 0){
                y--;
            }else if(each - 'D' == 0){
                y++;
            }else if(each - 'L' == 0){
                x--;
            }else if(each - 'R' == 0){
                x++;
            }
        }

        if(x == 0 && y ==0){
            return true;
        }
        return false;
    }
}

time O(n),n字符串长度 space O(n)

wtdcai commented 1 year ago

代码

        if len(moves)%2 == 1:
            return False
        y = 0
        x = 0 
        for i in moves:
            if i == "L":
                x += 1
            elif i == "R":
                x -= 1
            elif i == "U":
                y += 1
            else:
                y -= 1
        if x == 0 and y == 0:
            return True
        else:
            return False

复杂度分析

O(n)
O(1)

whoam-challenge commented 1 year ago

思路

模拟

代码

`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

`

复杂度分析

-时间复杂度 O(N) -空间复杂度O(1)

testplm commented 1 year ago
class Solution(object):
    def judgeCircle(self, moves):
        return moves.count('L') == moves.count('R') and moves.count('U') == moves.count('D')
Frederickfan commented 1 year ago

class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0,y=0;
        char[] c = moves.toCharArray();
        for(char each:c){
            if(each-'U' == 0){
                y--;
            }else if(each - 'D' == 0){
                y++;
            }else if(each - 'L' == 0){
                x--;
            }else if(each - 'R' == 0){
                x++;
            }
        }

        if(x == 0 && y ==0){
            return true;
        }
        return false;
    }
}
gsw9818 commented 1 year ago

代码

class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0, y = 0;
        for (const auto& move: moves){
            if (move == 'U'){
                y--;
            }
            else if (move == 'D'){
                y++;
            }
            else if (move == 'L'){
                x--;
            }
            else if (move == 'R'){
                x++;
            }
        }
        return x == 0 && y == 0;
    }
};
Elsa-zhang commented 1 year ago
class Solution:
    def judgeCircle(self, moves: str):
        begin = [0,0]

        for m in moves:
            if m == 'R':
                begin[0]+=1
            if m == 'L':
                begin[0]+=-1
            if m == 'U':
                begin[1]+=1
            if m == 'D':
                begin[1]+=-1

        return begin == [0,0]
heng518 commented 1 year ago

class Solution { public: bool judgeCircle(string moves) { int RC = 0, LC = 0, UC = 0, DC = 0; for(auto c:moves) { if(c == 'L') LC++; else if(c == 'R') RC++; else if(c == 'U') UC++; else if(c == 'D') DC++; }

    return LC == RC && DC == UC;
}

};

Alyenor commented 1 year ago
function judgeCircle(moves: string): boolean {
    let x=0,y=0
    for(let i=0;i<moves.length;i++){
        const c=moves[i]
        if(c==='L'){
            x++
        }else if(c==='R'){
            x--
        }else if(c==='U'){
            y++
        }else if(c==='D'){
            y--
        }
    }
    return !x&&!y
};
chenming-cao commented 1 year ago

解题思路

模拟。最后看返回的坐标是否为(0, 0)。

代码

class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0, y = 0;
        int length = moves.length();
        for (char move: moves.toCharArray()) {
            if (move == 'U') {
                y--;
            } else if (move == 'D') {
                y++;
            } else if (move == 'L') {
                x--;
            } else if (move == 'R') {
                x++;
            }
        }
        return x == 0 && y == 0;
    }
}

复杂度分析

yuxiangdev commented 1 year ago

class Solution { public boolean judgeCircle(String moves) { int x = 0, y = 0; int length = moves.length(); for (char move: moves.toCharArray()) { if (move == 'U') { y--; } else if (move == 'D') { y++; } else if (move == 'L') { x--; } else if (move == 'R') { x++; } } return x == 0 && y == 0; } }

Abby-xu commented 1 year ago

思路

检查上/下,左/右的数量是否对等

代码

class Solution:
    def judgeCircle(self, moves: str) -> bool:
        return moves.count('U')==moves.count('D') and moves.count('R')==moves.count('L') if len(moves)%2 == 0 else False

复杂度

O(n)/O(1)

guowei0223 commented 1 year ago

思路: x是水平移动,y是上下移动

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

TC O(n) SC O(1)

bookyue commented 1 year ago

code

    public boolean judgeCircle(String moves) {
        int[] coordinate = new int[2];
        for (int i = 0; i < moves.length(); i++) {
            char move = moves.charAt(i);
            if (move == 'U') coordinate[0]++;
            else if (move == 'D') coordinate[0]--;
            else if (move == 'L') coordinate[1]++;
            else if (move == 'R') coordinate[1]--;
        }

        return coordinate[0] == 0 && coordinate[1] == 0;
    }
zjsuper commented 1 year ago

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
yoco323 commented 1 year ago

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
xuanaxuan commented 1 year ago

思路 so easy 代码

var judgeCircle = function (moves) {
  let p = [0, 0];
  for (let i = 0; i < moves.length; i++) {
    if (moves[i] === "U") p[0] += 1;
    if (moves[i] === "D") p[0] -= 1;
    if (moves[i] === "L") p[1] -= 1;
    if (moves[i] === "R") p[1] += 1;
  }
  return p[0] === 0 && p[1] === 0;
};

复杂度 时间:O(n) 空间:O(1)

StaringWhere commented 1 year ago
class Solution {
public:
    bool judgeCircle(string moves) {
        int UD = 0, LR = 0;
        for (auto c : moves) {
            switch(c) {
                case 'L': {
                    LR++;
                    break;
                }
                case 'R': {
                    LR--;
                    break;
                }
                case 'U': {
                    UD++;
                    break;
                }
                default: {
                    UD--;
                }
            }
        }
        return !LR && !UD;
    }
};
zhiyuanpeng commented 1 year ago
class Solution:
    def judgeCircle(self, moves: str) -> bool:
        res = collections.Counter(moves)
        return res["L"]==res["r"] and res["U"]==res["D"]

time O(N) space O(N)

zywang0 commented 1 year ago

class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0,y=0;
        char[] c = moves.toCharArray();
        for(char each:c){
            if(each-'U' == 0){
                y--;
            }else if(each - 'D' == 0){
                y++;
            }else if(each - 'L' == 0){
                x--;
            }else if(each - 'R' == 0){
                x++;
            }
        }

        if(x == 0 && y ==0){
            return true;
        }
        return false;
    }
}
FlipN9 commented 1 year ago
/**
    TC: O(N)    SC: O(1)
*/
class Solution {
    public boolean judgeCircle(String moves) {
        int horiz = 0;
        int verti = 0;

        for (char i : moves.toCharArray()) {
            if (i == 'U') verti++;
            else if (i == 'D') verti--;
            else if (i == 'L') horiz--;
            else if (i == 'R') horiz++;
        } 

        return horiz == 0 && verti == 0;
    }
}
mayloveless commented 1 year ago

思路

统计走动的方向的值,相反方向相等则回回到原点

代码

var judgeCircle = function(moves) {
    let up = 0;
    let down = 0;
    let left = 0;
    let right = 0;
    for(let i=0;i<moves.length;i++) {
        const move = moves[i];
        switch(move) {
            case 'U':
                up++;
                break;
            case 'D':
                down++;
                break;
            case 'L':
                left++;
                break;
            case 'R':
                right++;
                break;
        }
    }

    return up === down && left === right;
};

复杂度

时间O(n) 空间O(1)

yuexi001 commented 1 year ago

思路

代码

C++ Code:

class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0, y = 0;
        for(const auto& str: moves){
            if(str == 'R')  x++;
            if(str == 'L')  x--;
            if(str == 'U')  y++;
            if(str == 'D')  y--;
        }
        return (x==0)&&(y==0) ;
    }
};

复杂度分析

tiandao043 commented 1 year ago

思路

模拟

代码

class Solution {
public:
    bool judgeCircle(string moves) {
        int x=0,y=0;
        for(auto a:moves){
            if(a=='U')y++;
            else if(a=='D')y--;
            else if(a=='L')x--;
            else x++;
        }
        return x==0&&y==0;
    }
};

O(n) O(1)

GG925407590 commented 1 year ago
class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0, y = 0;
        for (const auto& move: moves) {
            if (move == 'U') {
                y--;
            }
            else if (move == 'D') {
                y++;
            }
            else if (move == 'L') {
                x--;
            }
            else if (move == 'R') {
                x++;
            }
        }
        return x == 0 && y == 0;
    }
};
snmyj commented 1 year ago
class Solution {
public:
    bool judgeCircle(string moves) {
        int n=moves.length();
        int x=0,y=0;
        for(int i=0;i<n;i++){
            if(moves[i]=='R') x++;
            if(moves[i]=='L') x--;
            if(moves[i]=='U') y++;
            if(moves[i]=='D') y--;   
        }
        if(x==0&&y==0) return true;
        else return false;
    }
};
ggmybro commented 1 year ago

class Solution {
    public boolean judgeCircle(String moves) {
        int r = 0;
        int u = 0;
        for(char c : moves.toCharArray()){
            if(c == 'R'){
                r++;
            }else{
                if(c == 'L'){
                    r--;
                }else{
                    if(c == 'U'){
                        u++;
                    }else{
                        u--;
                    }
                }
            }
        }
        return r == 0 && u == 0;
    }
}
jackgaoyuan commented 1 year ago
func judgeCircle(moves string) bool {
    x := 0
    y := 0
    for i := 0; i < len(moves); i++ {
        switch string(moves[i]) {
            case "U":
                y += 1
            case "D":
                y -= 1
            case "L":
                x -= 1
            case "R":
                x += 1
        }
    }
    if x == 0 && y == 0 {
        return true
    }
    return false
}

TIme: O(n)

want2333 commented 1 year ago

【Day 32】657. 机器人能否返回原点

一条过
class Solution {
public:
    bool judgeCircle(string moves) {
        int row=0,list=0;
        for(auto move:moves){
            if(move == 'U') row++;
            if(move == 'D') row--;
            if(move == 'L') list--;
            if(move == 'R') list++;

        }
        if(row==0&&list==0) return true;
        else{
          return false;  
        }

    }
};
A-pricity commented 1 year ago
/**
 * @param {string} moves
 * @return {boolean}
 */
var judgeCircle = function(moves) {
  let up = 0;
  let left = 0;

  for (let index = 0; index < moves.length; index++) {
    switch (moves[index]) {
      case "R":
        left++;
        break;
      case "L":
        left--;
        break;
      case "U":
        up++;
        break;
      case "D":
        up--;
        break;
      default:
        break;
    }
  }

  if (up === 0 && left === 0) {
    return true;
  }
  return false;
};
luhaoling commented 1 year ago

Idea

定义res_x,res_y两个变量
分别根据字母对这两个变量进行操作,根据判断它们是否等于0来返回相应得boolean值

Code

public class day32 {
    public boolean judgeCircle(String moves) {
        int res_x=0,res_y=0;
        for(int i=0;i<moves.length();i++){
            if(moves.charAt(i)=='R'){
                res_x+=1;
            }else if(moves.charAt(i)=='L'){
                res_x-=1;
            }else if(moves.charAt(i)=='U'){
                res_y-=1;
            }else if(moves.charAt(i)=='D'){
                res_y+=1;
            }
        }
        if(res_x==0&&res_y==0)
            return true;
        return false;
    }

}

Complex

Time:O(n)
Space:O(1)
AstrKing commented 1 year ago

思路

我们只需按指令模拟机器人移动的坐标即可。起始时机器人的坐标为 (0,0)(0,0),在遍历完所有指令并对机器人进行移动之后,判断机器人的坐标是否为 (0,0)(0,0) 

代码

class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0, y = 0;
        int length = moves.length();
        for (int i = 0; i < length; i++) {
            char move = moves.charAt(i);
            if (move == 'U') {
                y--;
            } else if (move == 'D') {
                y++;
            } else if (move == 'L') {
                x--;
            } else if (move == 'R') {
                x++;
            }
        }
        return x == 0 && y == 0;
    }
}

复杂度分析

时间复杂度:O(n)

空间复杂度:O(1)

klspta commented 1 year ago
    public boolean judgeCircle(String moves) {
        int x = 0;
        int y = 0;
        for(int i = 0; i < moves.length(); i++){
            char c = moves.charAt(i);
            if(c == 'U'){
                x--;
            }else if(c == 'D'){
                x++;
            }else if(c == 'L'){
                y--;
            }else if(c == 'R'){
                y++;
            }
        }
        return x == 0 && y == 0;
    }
Moin-Jer commented 1 year ago

class Solution { public boolean judgeCircle(String moves) { int x = 0, y = 0; int length = moves.length(); for (int i = 0; i < length; i++) { char move = moves.charAt(i); if (move == 'U') { y--; } else if (move == 'D') { y++; } else if (move == 'L') { x--; } else if (move == 'R') { x++; } } return x == 0 && y == 0; } }

RestlessBreeze commented 1 year ago

代码

class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0, y = 0;
        for (const auto& move: moves) {
            if (move == 'U') {
                y--;
            }
            else if (move == 'D') {
                y++;
            }
            else if (move == 'L') {
                x--;
            }
            else if (move == 'R') {
                x++;
            }
        }
        return x == 0 && y == 0;
    }
};

复杂度

时间:o(n) 空间:o(1)

BruceZhang-utf-8 commented 1 year ago

思路

关键点

代码

Java Code:


class Solution {
    public boolean judgeCircle(String moves) {
 int x=0;
        int y=0;
        for (char c : moves.toCharArray()) {
            if(c=='R'){
                x++;
            }else if(c=='L'){
                x--;
            }else if(c=='U'){
                y++;
            }else if(c=='D'){
                y--;
            }
        }
        return x==0 && y==0;
    }
}

复杂度分析

令 n 为数组长度。

paopaohua commented 1 year ago
class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0, y = 0;
        for(char m : moves.toCharArray()){
            if(m == 'R'){
                x++;
            }else if(m == 'L'){
                x--;
            }else if(m == 'U'){
                y++;
            }else if(m == 'D'){
                y--;
            }
        }
        return x == 0 && y == 0;
    }
}
sclihuiming commented 1 year ago
func judgeCircle(moves string) bool {
    point := []int{0, 0}
    for _, move := range moves {
        if move == 'U' {
            point[0] += 1
        } else if move == 'D' {
            point[0] -= 1
        } else if move == 'L' {
            point[1] -= 1
        } else if move == 'R' {
            point[1] += 1
        }
    }
    return point[0] == 0 && point[1] == 0
}
Jetery commented 1 year ago

657. 机器人能否返回原点

思路

模拟题

代码 (cpp)

class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0, y = 0;
        for (char c : moves) {
            if (c == 'U') y += 1;
            else if (c == 'D') y -= 1;
            else if (c == 'L') x -= 1;
            else if (c == 'R') x += 1;
        }
        return x == 0 && y == 0;
    }
};

复杂度分析

AtaraxyAdong commented 1 year ago
class Solution {
    public boolean judgeCircle(String moves) {

        if (moves == null || moves.length() == 0) {
            return true;
        }

        //int[] position = new int[]{0, 0};
        int x = 0, y = 0;

        for (char c : moves.toCharArray()) {

            switch (c) {
                case 'R' -> x++;
                case 'L' -> x--;
                case 'U' -> y++;
                case 'D' -> y--;
            }

        }
        return (x == 0 && y == 0);
    }

}
Albert556 commented 1 year ago

class Solution { public: bool judgeCircle(string moves) { int x = 0, y =0; for (const char move : moves) { if (move == 'R') { x++; } if (move == 'L') { x--; } if (move == 'U') { y++; } if (move == 'D') { y--; } } return x==0 && y==0; } };

Albert556 commented 1 year ago

class Solution { public: bool judgeCircle(string moves) { int x = 0, y =0; for (const char move : moves) { if (move == 'R') { x++; } if (move == 'L') { x--; } if (move == 'U') { y++; } if (move == 'D') { y--; } } return x==0 && y==0; } };

neado commented 1 year ago

思路

模拟

代码


class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0, y = 0;
        for(char move: moves.toCharArray()) {
            if(move == 'R') {
                x++;
            } else if(move == 'L') {
                x--;
            } else if(move == 'U') {
                y++;
            } else if(move == 'D') {
                y--;
            }
        }
boolean r=x == 0 && y == 0;
        return r;
    }
}
buer1121 commented 1 year ago

class Solution: def judgeCircle(self, moves: str) -> bool:

模拟的思想

    x,y=0,0
    for move in moves:
        if move=='L':
            x-=1
        elif move=='R':
            x+=1
        elif move=='U':
            y+=1
        elif move=='D':
            y-=1

    return x==0 and y==0
MaylingLin commented 1 year ago

思路


模拟字符串的移动。

代码

class Solution:
    def judgeCircle(self, moves: str) -> bool:
        x = y = 0
        for move in moves:
            if move == 'R':
                x += 1
            if move == 'L':
                x -= 1
            if move == 'U':
                y += 1
            if move == 'D':
                y -= 1
        return x == 0 and y == 0

复杂度


zrtch commented 1 year ago
/**
 * @param {string} moves
 * @return {boolean}
 */
var judgeCircle = function (moves) {
  let m = new Map([
    ["R", 0],
    ["L", 0],
    ["U", 0],
    ["D", 0],
  ])
  for (let i = 0; i < moves.length; i++) {
    m.set(moves[i], m.get(moves[i]) + 1)
  }
  return m.get("R") === m.get("L") && m.get("U") === m.get("D")
};