657. Judge Route Circle

来源:互联网 发布:淘宝老瑞贸易可信吗 编辑:程序博客网 时间:2024/06/03 02:27
class Solution {public:    bool judgeCircle(string moves) {                int x = 0, y = 0;          for (int i = 0; i < moves.length(); i++)          {            if (moves.[i]=="R")                 x++;            if (moves.[i]=="L")                 x--;            if (moves.[i]=="U")                y++;            if (moves.[i]=="D")                 y--;          }        if (x+y==0)            return true;        else            return false;    }};




显然取字符方式不对。“L”这样是字符串,‘L’才是字符

beat 80%多

class Solution {public:    bool judgeCircle(string moves) {                int x = 0, y = 0;          for (int i = 0; i < moves.length(); i++)          {            if (moves[i]=='R')                 x++;            if (moves[i]=='L')                 x--;            if (moves[i]=='U')                y++;            if (moves[i]=='D')                 y--;          }        if (x+y==0)            return true;        else            return false;    }};



原创粉丝点击