classSolution:def totalHammingDistance(self, nums: List[int])->int:a =len(nums) # 总个数iflen(nums)==0:return0a2 =max(nums)c =[0for i in range(0,a2)]for i in nums:if i==0:continues=bin(i)[2:]# print(s)for i in range(len(s)):if s[len(s)-i-1]=='1':c[i]+=1# print(c)ans=0for i in c:ans+=i*(len(nums)-i)return ans
2.看题解,差不多,原来是自己把列表大小开错了,o(╥﹏╥)o:
classSolution:def totalHammingDistance(self, nums: List[int])->int:a =len(nums) # 总个数iflen(nums)==0:return0a2 =max(nums)c =[0for i in range(0,len(bin(a2))-2)]for i in nums:if i==0:continues=bin(i)[2:]# print(s)for i in range(len(s)):if s[len(s)-i-1]=='1':c[i]+=1# print(c)ans=0for i in c:ans+=i*(len(nums)-i)return ans
问题: The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Output:…
Codeforces Round #477
C. Stairs and Elevators
题目链接
题意:给你一栋高n层,每一层由m个部分组成,可以看成是一个矩阵划分成行和列。给你一cl,ce分别表示有个楼梯和电梯,然后给你一个v表示电梯的速度。接下来行表示每个电梯…