leetcode/2017-1-1
来源:互联网 发布:淘宝客佣金分成 编辑:程序博客网 时间:2024/04/20 13:47
461. Hamming Distance
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 < 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
- leetcode/2017-1-1
- Leetcode 1
- leetcode(1)
- leetcode 1
- leetcode 1
- leetcode-1
- leetcode 1
- LeetCode(1)
- leetcode 1
- LeetCode(1)
- LeetCode 1
- leetcode-1
- leetcode(1)
- leetcode-1
- LeetCode (1)
- leetcode-1
- leetcode 1
- leetcode(1)
- BZOJ2959——长跑
- Chrome NET::ERR_CERT_AUTHORITY_INVALID 错误分析及处理
- maven发布web项目时报错ClassNotFoundException
- unity 设置帧数
- 函数方程
- leetcode/2017-1-1
- PHP 基础五
- .net窗体或控件透明效果
- 魅族手机权限开启方法5
- 史上最难oracle数据库练习题(附答案)
- 水位下降
- 数据库设计 从入门至初级
- Openstack 架构简述
- Android Studio: You need to use a Theme.AppCompat theme (or descendant) with this activity.