mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-04 04:02:48 +08:00
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
56 lines
983 B
Go
56 lines
983 B
Go
// File: simple_hash.go
|
|
// Created Time: 2023-06-23
|
|
// Author: Reanon ([email protected])
|
|
|
|
package chapter_hashing
|
|
|
|
import "fmt"
|
|
|
|
/* 덧셈 해시 */
|
|
func addHash(key string) int {
|
|
var hash int64
|
|
var modulus int64
|
|
|
|
modulus = 1000000007
|
|
for _, b := range []byte(key) {
|
|
hash = (hash + int64(b)) % modulus
|
|
}
|
|
return int(hash)
|
|
}
|
|
|
|
/* 곱셈 해시 */
|
|
func mulHash(key string) int {
|
|
var hash int64
|
|
var modulus int64
|
|
|
|
modulus = 1000000007
|
|
for _, b := range []byte(key) {
|
|
hash = (31*hash + int64(b)) % modulus
|
|
}
|
|
return int(hash)
|
|
}
|
|
|
|
/* XOR 해시 */
|
|
func xorHash(key string) int {
|
|
hash := 0
|
|
modulus := 1000000007
|
|
for _, b := range []byte(key) {
|
|
fmt.Println(int(b))
|
|
hash ^= int(b)
|
|
hash = (31*hash + int(b)) % modulus
|
|
}
|
|
return hash & modulus
|
|
}
|
|
|
|
/* 회전 해시 */
|
|
func rotHash(key string) int {
|
|
var hash int64
|
|
var modulus int64
|
|
|
|
modulus = 1000000007
|
|
for _, b := range []byte(key) {
|
|
hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus
|
|
}
|
|
return int(hash)
|
|
}
|