码迷,mamicode.com
首页 > 其他好文 > 详细

781. Rabbits in Forest

时间:2018-09-11 23:59:32      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:bit   iter   self   lis   class   obj   res   def   sel   

class Solution(object):
def numRabbits(self, answers):
"""
:type answers: List[int]
:rtype: int
"""
cnt = {}
for answer in answers:
if answer not in cnt:
cnt[answer] = 1
else:
cnt[answer] += 1

s = 0
for k, v in cnt.iteritems():
if v <= k+1:
s += k+1
elif k > 0:
rest = v%(k+1)
if rest <>0:
s += v - rest
s += k+1
else:
s += v
elif k == 0:
s += v

return s

781. Rabbits in Forest

标签:bit   iter   self   lis   class   obj   res   def   sel   

原文地址:https://www.cnblogs.com/ffeng0312/p/9630927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!