• 657. Judge Route Circle


    题目描述:

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

    The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L(Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

    Example 1:

    Input: "UD"
    Output: true
    

    Example 2:

    Input: "LL"
    Output: false

    解题思路:
    统计输入字符串中字符U和D的个数是否一样,L和R个数是否一样,若都一样,则返回到起点。

    代码:
     1 class Solution {
     2 public:
     3     bool judgeCircle(string moves) {
     4         
     5         int u = 0;
     6         int d = 0;
     7         int l = 0;
     8         int r = 0;
     9         u = std::count(moves.begin(), moves.end(), 'U');
    10         d = std::count(moves.begin(), moves.end(), 'D');
    11         l = std::count(moves.begin(), moves.end(), 'L');
    12         r = std::count(moves.begin(), moves.end(), 'R');
    13         return (u == d) && (l == r);   
    14     }
    15 };
  • 相关阅读:
    浮点数
    opencv笔记-GFTTDetector
    有向图与关联矩阵
    亚像素角点
    字符串格式化输出
    字符串表示与转换
    Bresenham算法
    罗德里格斯公式
    模型调参
    jave 逻辑运算 vs 位运算 + Python 逻辑运算 vs 位运算
  • 原文地址:https://www.cnblogs.com/gsz-/p/9385508.html
Copyright © 2020-2023  润新知