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
  • 相关阅读:
    怎么防止重复提交
    JSP三大指令是什么?
    python中字符串的编码和解码
    什么是 JavaConfig?
    Spring Boot 有哪些优点?
    如何重新加载 Spring Boot 上的更改,而无需重新启动服务器?
    Mybatis 动态 sql 是做什么的?都有哪些动态 sql?能简述一下动态 sql 的执行原理不?
    如果你也用过 struts2.简单介绍下 springMVC 和 struts2 的区别有哪些?
    学习笔记——命令模式Command
    学习笔记——中介者模式Mediator
  • 原文地址:https://www.cnblogs.com/findneo/p/6833188.html
Copyright © 2011-2022 走看看