【LeetCode】【Python】461.Hamming Distance

461.Hamming Distance

question

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:

0 ≤ x, y < 2^31.

Example:

Input: x = 1, y = 4

Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different. 

solution:

class Solution(object):
    def hammingDistance(self, x, y):
        """ :type x: int :type y: int :rtype: int """
        return bin(x ^ y).count("1")

bin() 返回一个整数 int 或者长整数 long int 的二进制表示,类型为字符串。如bin(6)返回“0b101”,count()函数返回字符串中某个字符出现的个数.

全部评论

相关推荐

之前自己不懂事,投了字节,基本是自己第一次面试,一面就挂了
观水:前几天有个学化学的做前端,加上实习面了22次字节最后成功了
点赞 评论 收藏
分享
合适才能收到offe...:项目岗是什么岗?我看你有段好像跟策划运营相关,如果找运营的话第三段经历写详细点儿。 个人建议是把自我评价删了换成专业技能放在工作经验上或者下面。学生会那个也可以删,把第一个包装成店铺运营,写4-6给点。第三个也是写4-6个点。注意工作内容➕部分数据。 投递的时候BOS招呼用语改一下,换成我有xx工作经验,熟练掌握xx技能样式,也可以简历截图然后直接发送。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务