zoukankan      html  css  js  c++  java
  • leetcode/2017-1-1

    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
  • 相关阅读:
    Qt下如何修改文件的时间(全平台修改)
    Qt在windows 平台操作保存execel的表格(通过QAxObject来操作)
    VirtualTreeView控件
    VS2013设置release版本可调试
    工程脚本插件方案
    decode函数
    一个消息调度框架构建
    数据访问模式之Repository模式
    Angular.js Services
    OpenCascade简介
  • 原文地址:https://www.cnblogs.com/findneo/p/6833188.html
Copyright © 2011-2022 走看看