1.移位实现
public static int GetIndex(string str, int count) { int hash = str.Aggregate(23, (current, c) => (current << 5) - current + c); if (hash < 0) { hash = Math.Abs(hash); } return hash % count; }
2.GetHashCode()
public static int GetIndex2(string str, int count) { int hash =str.GetHashCode(); if (hash < 0) { hash = Math.Abs(hash); } return hash % count; }
时间: 2024-10-12 20:23:07