一 读懂题目
二. 分析,推导解法,产生思路。
解题思路:
(1)直接考虑位置。左右抵消,上下抵消。
(2)通过统计直接计算次数
三 代码实现
class Solution(object):
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
# 左右抵消,上下抵消。
r_l = 0
u_d = 0
for i in range(len(moves)):
print(i)
if moves[i] == 'R':
r_l += 1
elif moves[i] == 'L':
r_l -= 1
elif moves[i] == 'U':
u_d += 1
else:
u_d -= 1
if r_l == 0 and u_d == 0:
return True
return False
# 通过统计直接计算
def judgeCircle1(self, moves):
return moves.count('R')==moves.count('L') and moves.count('U')==moves.count('D')
# 第一种执行时间长些:80ms与32ms