博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode/2017-1-1
阅读量:6890 次
发布时间:2019-06-27

本文共 2296 字,大约阅读时间需要 7 分钟。

461. Hamming Distance

The 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 < 231.

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.

int hammingDistance(int x, int y) {    int tmp=x^y;    int hamming=0;    while(tmp>0){        if(tmp&1)hamming++;        tmp>>=1;    }    return hamming;}

注:

In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In another way, it measures the minimum number of substitutions required to change one string into the other, or the minimum number of errors that could have transformed one string into the other.
汉明间距:等长字符串对应位置字符不同的位置的个数


191. Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11’ has binary representation 00000000000000000000000000001011, so the function should return 3.

int hammingWeight(uint32_t n) {    int weight=0;    while(n>0){        if(n&1)weight++;        n>>=1;    }    return weight;}

hamming weight :汉明权,二进制串中1的位数

448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Example:
Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]

class Solution(object):    def findDisappearedNumbers(self, nums):        """        :type nums: List[int]        :rtype: List[int]        """        if nums == []:            return []        list=[]        nums.sort()        oldi=nums[0]        for s in range(1,nums[0]):            list.append(s)        for i in nums[1:]:            for s in range(oldi+1,i):                list.append(s)            oldi=i        for s in range(nums[-1]+1,len(nums)+1):            list.append(s)        return list

转载于:https://www.cnblogs.com/findneo/p/6833188.html

你可能感兴趣的文章
海康威视 - 萤石云开放平台 js 版
查看>>
关于分销平台
查看>>
剑指offer---12-**--数值的整数次方
查看>>
PAT - L2-010. 排座位(并查集)
查看>>
Python 学习笔记 - 线程(线程锁,信标,事件和条件)
查看>>
大数据技术服务商个推获4亿人民币D轮融资
查看>>
Git的详细使用教程
查看>>
iOS实现类似苹果手机原生的锁屏界面(数字密码)
查看>>
[vue] 表单输入格式化,中文输入法异常
查看>>
Observer观察者模式与OCP开放-封闭原则
查看>>
如何搭建高级工程师知识框架?推荐两种方式
查看>>
BAT的医疗春秋大梦
查看>>
Pulsar本地单机(伪)集群 (裸机安装与docker方式安装) 2.2.0
查看>>
利用H5的css3制作动画
查看>>
Android View 事件分发源码分析
查看>>
vue 2.0 - props
查看>>
RustCon Asia 实录 | Rust 在国内某视频网站的应用
查看>>
Vue遇上Analytics
查看>>
修改max_allowed_packet(允许执行的sql最大长度)
查看>>
node js 处理时间分析
查看>>