原题:
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就是看看是不是走了一个圈哦
代码如下:
bool judgeCircle(char* moves) {
int len=strlen(moves);
int sumhorizontal=0;
int sumvertical=0;
for(int n=0;n<len;n++)
{
if(*(moves+n)=='U')
sumvertical++;
if(*(moves+n)=='D')
sumvertical--;
if(*(moves+n)=='L')
sumhorizontal--;
if(*(moves+n)=='R')
sumhorizontal++;
}
if(sumhorizontal==0&&sumvertical==0)
return true;
return false;
}
算下横纵坐标就ok了。