Judge Route Circle

    这道题为简单题,网上通过率接近70%,我就不怎么仔细讲了

  题目:

    

  思路:

    题目太简单,我也没怎么多想,直接暴力解决,然后就导致时间运行太慢 = =

  代码:

    1、这是我的写的,很菜

 1 class Solution(object):
 2     def judgeCircle(self, moves):
 3         """
 4         :type moves: str
 5         :rtype: bool
 6         """
 7         a = 0
 8         b = 0
 9         for i in moves:
10             if i == 'R': a += 1
11             if i == 'L': a -= 1
12             if i == 'U': b += 1
13             if i == 'D': b -= 1
14         if a == 0 and b == 0: return True
15         else: return False

    2、大神的简洁代码:

1 def judgeCircle(self, moves):
2     c = collections.Counter(moves)
3     return c['L'] == c['R'] and c['U'] == c['D']

 

posted @ 2017-09-10 21:35  唐僧洗发爱飘柔  阅读(111)  评论(0)    收藏  举报