简体   繁体   English

哈希表索引设计

[英]hash table index design

I want to use a hash table to store words. 我想使用哈希表来存储单词。 For example, I have two words aba and aab ,because they are made up of the same elements just in different order , so I want to store them with the same index, and plug a link list at that link list. 例如,我有两个单词aba和aab,因为它们由不同顺序的相同元素组成,所以我想用相同的索引存储它们,并在该链接列表中插入链接列表。 It's easy for me to search in a certain way. 我很容易以某种方式搜索。 The elements of words are just 26 letters. 单词的元素只有26个字母。 How to design a proper index of the hash table? 如何设计哈希表的正确索引? How to organize the table? 如何组织表?

So the questions you want to answer with your hash table is: What words can be built with the letters I have? 因此,您想要使用哈希表回答的问题是:可以用我的字母构建哪些单词?

I assume you are reading some dictionary and want to put all the values in the hash table. 我假设您正在阅读一些字典并希望将所有值都放在哈希表中。 Then you could use an int array with the count how many times each letter occurs as key (eg 'a' would be index 0 and 'z' index 25) and for the value you would have to use a list, so that you can add more than one word to that entry. 然后你可以使用一个int数组,计算每个字母作为键出现的次数(例如'a'将是索引0和'z'索引25),并且对于你必须使用列表的值,这样你就可以在该条目中添加多个单词。

But the simplest solution is probably just to use the sorted word as key (eg 'aba' gets key 'aab' and 'aab' obviously too), because words are not very long the sort isn't expensive (avoid creating new strings all the time by working with the character array). 但最简单的解决方案可能只是使用排序后的单词作为键(例如'aba'显然也是键'aab'和'aab'),因为单词不是很长,排序并不昂贵(避免创建所有新字符串)通过使用字符数组的时间)。

So in Java you could get the key like this: 所以在Java中你可以得到这样的关键:

char[] key = word.toCharArray();
Arrays.sort(key);
// and if you want a string
String myKey = new String(key);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM