æ£å
程序员文章站
2022-03-15 20:46:19
...
// éè¿å°å符串çASCIIç å¼çåæ¥è®¡ç®hashå¼
// 缺ç¹ï¼hashå¼åé
ä¸åå
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>é¾è¡¨</title>
</head>
<body>
<script>
// hash
function HashTable () {
this.table = new Array(137);
this.simpleHash = simpleHash;
this.showDistro = showDistro;
this.put = put;
this.get = get;
}
function simpleHash (data) {
var total = 0;
for (var i = 0; i < data.length;i++) {
total += data.charCodeAt(i);
}
return total % this.table.length;
}
// æå
¥
function put (data) {
var pos = this.simpleHash(data);
this.table[pos] = data;
}
function get (key) {
return this.table[this.simpleHash(data)];
}
function showDistro () {
var n = 0;
for (var i = 0; i < this.table.length; i++) {
if (this.table[i] != undefined) {
console.log('é®å¼æ¯->' + i + 'å¼æ¯ ã' + this.table[i] + 'ã')
}
}
}
var hTable = new HashTable();
hTable.put('china');
hTable.put('Japan');
hTable.put('America');
hTable.put('nicha');
hTable.put('131');
hTable.put('78');
hTable.put('39');
hTable.put('460');
hTable.showDistro();
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>é¾è¡¨</title>
</head>
<body>
<script>
// hash
function HashTable () {
this.table = new Array(137);
this.simpleHash = simpleHash;
this.betterHash = betterHash;
this.showDistro = showDistro;
this.put = put;
this.get = get;
}
function simpleHash (data) {
var total = 0;
for (var i = 0; i < data.length;i++) {
total += data.charCodeAt(i);
}
return total % this.table.length;
}
function betterHash (data) {
var H = 31;
var total = 0;
for (var i = 0; i < data.length; i++) {
total += H * total + data.charCodeAt(i);
}
if (total < 0) {
total += this.table.length - 1;
}
return total % this.table.length;
}
// æå
¥
function put (data) {
// var pos = this.simpleHash(data);
var pos = this.betterHash(data);
this.table[pos] = data;
}
function get (key) {
return this.table[this.simpleHash(data)];
}
function showDistro () {
var n = 0;
for (var i = 0; i < this.table.length; i++) {
if (this.table[i] != undefined) {
console.log('é®å¼æ¯->' + i + 'å¼æ¯ ã' + this.table[i] + 'ã')
}
}
}
var hTable = new HashTable();
hTable.put('china');
hTable.put('Japan');
hTable.put('America');
hTable.put('nicha');
hTable.put('131');
hTable.put('78');
hTable.put('39');
hTable.put('460');
hTable.showDistro();
</script>
</body>
</html>
// å¼é¾æ³ï¼æä¸ç»´æ°ç»è½¬æ¢ä¸ºäºç»´æ°ç»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>é¾è¡¨</title>
</head>
<body>
<script>
// hash
function HashTable () {
this.table = new Array(137);
this.buildChians = buildChians;
this.simpleHash = simpleHash;
this.betterHash = betterHash;
this.showDistro = showDistro;
this.put = put;
this.get = get;
}
function buildChians () {
for (var i = 0; i < this.table.length; i++) {
this.table[i] = new Array();
}
}
function simpleHash (data) {
var total = 0;
for (var i = 0; i < data.length;i++) {
total += data.charCodeAt(i);
}
return total % this.table.length;
}
function betterHash (data) {
var H = 31;
var total = 0;
for (var i = 0; i < data.length; i++) {
total += H * total + data.charCodeAt(i);
}
if (total < 0) {
total += this.table.length - 1;
}
return total % this.table.length;
}
// æå
¥
function put (data) {
var pos = this.simpleHash(data);
// var pos = this.betterHash(data);
// this.table[pos] = data;
var index = 0;
if (this.table[pos][index] == undefined) {
this.table[pos][index] = data;
index ++;
} else {
while (this.table[pos][index] != undefined) {
++index;
}
this.table[pos][index] = data;
}
}
function get (key) {
return this.table[this.simpleHash(data)];
}
function showDistro () {
var n = 0;
for (var i = 0; i < this.table.length; i++) {
if (this.table[i][0] != undefined) {
console.log('é®å¼æ¯->' + i + 'å¼æ¯ ã' + this.table[i] + 'ã')
}
}
}
var hTable = new HashTable();
hTable.buildChians();
hTable.put('china');
hTable.put('Japan');
hTable.put('America');
hTable.put('nicha');
hTable.showDistro();
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>é¾è¡¨</title>
</head>
<body>
<script>
// hash
function HashTable () {
this.table = new Array(137);
this.simpleHash = simpleHash;
this.showDistro = showDistro;
this.put = put;
this.get = get;
}
function simpleHash (data) {
var total = 0;
for (var i = 0; i < data.length;i++) {
total += data.charCodeAt(i);
}
return total % this.table.length;
}
// æå
¥
function put (data) {
var pos = this.simpleHash(data);
// this.table[pos] = data;
if (this.table[pos] == undefined) {
this.table[pos] = data;
} else {
while (this.table[pos] != undefined) {
pos++;
}
this.table[pos] = data;
}
}
function get (key) {
var hash = this.simpleHash(key);
console.log(hash);
for (var i = hash; i < this.table.length; i++) {
if (this.table[i] == key) {
return i;
}
}
return undefined;
}
function showDistro () {
var n = 0;
for (var i = 0; i < this.table.length; i++) {
if (this.table[i] != undefined) {
console.log('é®å¼æ¯->' + i + 'å¼æ¯ ã' + this.table[i] + 'ã')
}
}
}
var hTable = new HashTable();
hTable.put('china');
hTable.put('Japan');
hTable.put('America');
hTable.put('nicha');
console.log(hTable.get('nicha'))
hTable.showDistro();
</script>
</body>
</html>
上一篇: Android面试题之Activity篇