原题链接:https://leetcode-cn.com/problems/robot-return-to-origin/
这道题是真简单:
------2019/1/23--------
我重新做了一下,在LeetCode上执行了一下:
public class Solution {
public bool JudgeCircle(string moves) {
int temp = 0;bool res = false;
for (int i = 0; i < moves.Length; i++)
{
switch (moves[i].ToString())
{
case "U":
temp += 10;
break;
case "D":
temp -= 10;
break;
case "L":
temp += 1;
break;
case "R":
temp -= 1;
break;
default:
break;
}
}
if (temp==0)res = true;
else res = false;
return res;
}
}
------2019/1/23--------
下面是以前的未在LeetCode上执行。
应该没什么难得,静下心来想一下,设计一个完成解题的思路:上 - 下 =0 ;左-右=0;即可 。纯原创
static void Main(string[] args)
{
string str="UULLDRLRRD";
bool answer = JudgeCircle(str);
Console.WriteLine(answer);
Console.ReadKey();
}
public static bool JudgeCircle(string str)
{
int r = 0, l = 0, u = 0, d = 0;
for (int i = 0; i < str.Length; i++)
{
switch (str[i])
{
case 'R':
r++;
break;
case 'L':
l++;
break;
case 'U':
u++;
break;
case 'D':
d++;
break;
default:
break;
}
}
if (r + (-l) != 0 || u + (-d) != 0)
{
return false;
}
else
{
return true;
}
}
}