相似算法 ,Java实例9 - 汉明距离 Hamming Distance

Java实例9 - 汉明距离 Hamming Distance

http://blog.csdn.net/kindterry/article/details/6581344

/**在信息理论中,两个等长字符串之间的汉明距离
* 是两个字符串对应位置上不同字符的个数,
* 换句话说,汉明距离就是将一个字符串替换成另外一个字符串所需要替换的字符长度。
*例如,1011101和1001001之间的汉明距离是2,
*toned和roses之间的汉明距离是3.
*汉明重量是字符串相对于同样长度的零字符串的汉明距离,
*也就是说,它是字符串中非零的元素个数:对于二进制字符串来说,就是 1 的个数,
*所以 11101 的汉明重量是 4。
*下面的代码展示了在Java中如何计算汉明距离和汉明重量。
*/
package re;
public class HammingDistance {
public static void main(String[] args) {
String str1 = "abcdefg";
String str2 = "aacceeg";
HammingDistance hd = new HammingDistance();
int distance = hd.getDistance(str1, str2);
System.out.println("distance is " + distance);
int weight = hd.getWeight(255);
System.out.println("weight is " + weight);
}
/**
* calculate Hamming Distance between two strings
*
* @author
* @param str1 the 1st string
* @param str2 the 2nd string
* @return Hamming Distance between str1 and str2
*/
public int getDistance(String str1, String str2) {
int distance;
if (str1.length() != str2.length()) {
distance = -1;
} else {
distance = 0;
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) != str2.charAt(i)) {
distance++;
}
}
}
return distance;
}
/**
* calculate Hamming weight for binary number
* @author
* @param i the binary number
* @return Hamming weight of the binary number
*/
public int getWeight(int i) {
int n;
for (n = 0; i > 0; n++) {
i &= (i - 1);
}
return n;
}
}

时间: 2024-10-13 23:03:22

相似算法 ,Java实例9 - 汉明距离 Hamming Distance的相关文章

461.求两个数字转成二进制后的“汉明距离” Hamming Distance

public class Solution { public int HammingDistance(int x, int y) { int distance = 0; string sX = Convert.ToString(x, 2); string sY = Convert.ToString(y, 2); int maxLength = Math.Max(sX.Length, sY.Length); //填充0,使两个字符串右对齐 sX = sX.PadLeft(maxLength, '0

[LeetCode] 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

Java - 35 Java 实例

Java 实例 本章节我们将为大家介绍 Java 常用的实例,通过实例学习我们可以更快的掌握 Java 的应用. Java 环境设置实例 Java 实例 – 如何编译一个Java 文件? Java 实例 – Java 如何运行一个编译过的类文件? Java 实例 - 如何执行指定class文件目录(classpath)? Java 实例 – 如何查看当前 Java 运行的版本? Java 字符串 Java 实例 – 字符串比较 Java 实例 - 查找字符串最后一次出现的位置 Java 实例 -

HDU - 4712 Hamming Distance(坑爹的随机数算法 + 暴力求解)

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1728    Accepted Submission(s): 680 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equa

LeetCode 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

[LeetCode] 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

RTree算法Java实现 JSI RTree Library的调用实例 标签:jsi-rtree-library

1. [代码]jsi-rtree-library /** *  */package com.mycompany.project; //package net.sourceforge.jsi.examples; import java.util.ArrayList;import java.util.List; import org.apache.log4j.Logger; //import org.slf4j.*;import com.infomatiq.jsi.*; import gnu.tro

[Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Output: 6 Explanat

Leetcode 461. Hamming Distance JAVA语言

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. PS:求海明距离. 思路:就是求x和y二进制的异或中的1的个数 public class Solution {     public int ham