see-461. Hamming Distance

计算海明距离
本文介绍了一种计算两个整数在二进制表示下不同位数的方法——海明距离,并提供了一个C++实现示例,通过异或操作和迭代计算得出结果。

求海明距离:即二进制表示法中位置上二进制数不同的总个数,如0001和0100海明距离是2。

先求异或,再确定结果用二进制表示后,其中1的个数。

class Solution {
public:
    int hammingDistance(int x, int y) {
        int res=0;
        int Xor=x^y;
        while(Xor!=0)
        {
            res++;
            Xor=Xor&(Xor-1);
        }
        return res;
    }
};

 

"""Compute the distance matrix from a vector array X and optional Y. This method takes either a vector array or a distance matrix, and returns a distance matrix. If the input is a vector array, the distances are computed. If the input is a distances matrix, it is returned instead. If the input is a collection of non-numeric data (e.g. a list of strings or a boolean array), a custom metric must be passed. This method provides a safe way to take a distance matrix as input, while preserving compatibility with many other algorithms that take a vector array. If Y is given (default is None), then the returned matrix is the pairwise distance between the arrays from both X and Y. Valid values for metric are: - From scikit-learn: ['cityblock', 'cosine', 'euclidean', 'l1', 'l2', 'manhattan']. These metrics support sparse matrix inputs. ['nan_euclidean'] but it does not yet support sparse matrices. - From scipy.spatial.distance: ['braycurtis', 'canberra', 'chebyshev', 'correlation', 'dice', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'yule'] See the documentation for scipy.spatial.distance for details on these metrics. These metrics do not support sparse matrix inputs. .. note:: `'kulsinski'` is deprecated from SciPy 1.9 and will be removed in SciPy 1.11. .. note:: `'matching'` has been removed in SciPy 1.9 (use `'hamming'` instead). Note that in the case of 'cityblock', 'cosine' and 'euclidean' (which are valid scipy.spatial.distance metrics), the scikit-learn implementation will be used, which is faster and has support for sparse matrices (except for 'cityblock'). For a verbose description of the metrics from scikit-learn, see :func:`sklearn.metrics.pairwise.distance_metrics` function.
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值