LeetCode题解之Hamming Distance
1、题目描述

2、问题分析
使用C++ 标准库中的 bitset 类,将整数转换为二进制形式,然后再将其转换为字符串,最后比较字符串。
3、代码
int hammingDistance(int x, int y) {
bitset<> a(x);
bitset<> b(y);
string a_s = a.to_string();
string b_s = b.to_string();
string::iterator it1 = a_s.begin() ;
string::iterator it2 = b_s.begin() ;
int re = ;
while( it1 != a_s.end() && it2 != b_s.end() ){
if( *it1 != *it2 ){
re++;
}
it1++;
it2++;
}
return re;
}
LeetCode题解之Hamming Distance的更多相关文章
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
- [LeetCode] 477. Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 477. Total Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- 【LeetCode】461. Hamming Distance 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...
- 【leetcode】461. Hamming Distance
problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...
- LeetCode之461. Hamming Distance
------------------------------------------------------------------ AC代码: public class Solution { pub ...
- LeetCode算法题-Hamming Distance(Java实现)
这是悦乐书的第237次更新,第250篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第104题(顺位题号是461).两个整数之间的汉明距离是相应位不同的位置数.给定两个整数 ...
- 【LeetCode】汉明距离(Hamming Distance)
这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, ...
- LeetCode "477. Total Hamming Distance"
Fun one.. the punch line of this problem is quite common in Bit related problems on HackerRank - vis ...
随机推荐
- openssl生成RSA格式的公私钥,并转为pkcs8格式
第一步:生成私钥,这里我们指定私钥的长度为2048 openssl genrsa -out rsa_private_key.pem 2048 第二步:根据私钥生成对应的公钥: openssl rsa ...
- mybatis开启二级缓存小记
mybatis开启二级缓存小记 1.开启二级缓存 和一级缓存默认开启不一样,二级缓存需要我们手动开启 首先在全局配置文件 mybatis-configuration.xml 文件中加入如下代码: &l ...
- OpenGL12-shader(GLSL)着色语言2-(参数传递)(代码以上传)
上一篇中介绍了如何使用shader,用来一个最简单的shader,计算顶点的位置,调用了 OpenGL 顶点着色语言中的内置变量对顶点进行操作,这一例程中,将展示如何将应用层 的数据传递到shader ...
- ubuntu16.04安装cuda,无法定位软件包问题
为了学习深度学习,这几天在安装深度学习框架,cuda安装时却出现无法定位软件包的问题.cuda官网下载时提供了deb和run格式,今天只讲deb格式的安装包安装过程的问题. 按照官方教程,下载好cud ...
- docker 安装集群管理工具 docker-compose
compose将管理的容器分为3层, 工程, 服务, 容器. 一个工程中可包含多个服务, 每个服务中定义容器运行的镜像参数, 依赖, 一个服务中科包含多个容器实力, 并没有解决负载均衡的问题 dock ...
- Java 注解实例
package com.annotation; import java.lang.annotation.Retention; import java.lang.annotation.Target; i ...
- Windows 安装 MySQL 5.7.18
1. 在MySQL官网 http://dev.mysql.com/downloads/mysql/ 上面下载ZIP安装包(第二个:Windows (x86, 64-bit), ZIP Archive) ...
- Firebird 获取用户表及字段
select rdb$relation_fields.rdb$relation_name table_name, rdb$relations.rdb$description table_des, rd ...
- com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value:...
在使用mybatis的@Update注解的时候,报了一个这样的错 ### Error updating database. Cause: com.mysql.jdbc.MysqlDataTruncat ...
- MVC初级知识之——Routing路由
实例产品基于asp.net mvc 5.0框架,源码下载地址:http://www.jinhusns.com/Products/Download 我们注意到地址栏的URL是Home/Index 路由可 ...