Implement the hash table using array / binary search tree
今天在复习Arrays and String 时看到一个很有趣的问题。希望跟大家分享一下。
Implement the hash table using array / binary search tree
1. Using Array/LinkedList:
1) key需要通过hash 来得到array中的index
2) array的每一位都是放着一个list
3) 需要自己定义一个class,list 由这个class 类型组成
4) 要考虑到万一hashvalue 范围很大,需要把table的size压缩: %TABLE_SIZE;
class LinkedHashEntry {
String key;
int value;
LinkedHashEntry next;
LinkedHashEntry(String key, int value) {
this.key = key;
this.value = value;
this.next = null;
} } public class HashTable { private int TABLE_SIZE;
private int size;
private LinkedHashEntry[] table; // Constructor
HashTable(int ts) {
size = 0;
TABLE_SIZE = ts;
table = new LinkedHashEntry[TABLE_SIZE];
for(int i = 0; i < TABLE_SIZE; i ++) {
table[i] = null;
}
} // hash function
private int myhash(String key) {
int hashValue = key.hashCode();
hashValue %= TABLE_SIZE;
if(hashValue < 0) {
hashValue += TABLE_SIZE;
}
return hashValue;
}
// hash put function public void put(String key, int value) {
int hash = (myhash(key) % TABLE_SIZE);
if(table[hash] == null) {
table[hash] = new LinkedHashEntry(key, value);
} else {
LinkedHashEntry entry = table[hash];
while(entry.next != null && !entry.key.equals(key)) {
entry = entry.next;
}
if(entry.key.equals(key)) {
entry.value = value; // if we have already put this pair of key value
} else {
entry.next = new LinkedHashEntry(key, value);
}
}
size ++;
} public int getValue(String key) {
int hash = (myhash(key) % TABLE_SIZE);
if(table[hash] == null) {
return -1;
} else {
LinkedHashEntry entry = table[hash];
while(entry.next != null && !entry.key.equals(key)) { entry = entry.next;
}
if(entry.key.equals(key)) {
return entry.value;
} else {
return -1;
}
}
} public void remove(String key, int value) {
int hash = (myhash(key)% TABLE_SIZE);
if(table[hash] == null) {
return;
} else {
LinkedHashEntry preEntry = null;
LinkedHashEntry entry = table[hash];
while(entry.next != null && !entry.key.equals(key)) {
preEntry = entry;
entry = entry.next;
}
if(entry.key.equals(key)) {
if(preEntry == null) {
table[hash] = entry.next;
} else {
preEntry.next = entry.next;
}
size --;
}
}
} public int size() {
return size;
} }
2. Using BST
在craking coding interview 这本书上看到这个问题,但是并没有想清楚这个问题的解决方法,希望有大神可以解答。谢谢!
Implement the hash table using array / binary search tree的更多相关文章
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【leetcode】Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【LeetCode OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
- 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree
Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...
- [CareerCup] 8.10 Implement a Hash Table 实现一个哈希表
8.10 Design and implement a hash table which uses chaining (linked lists) to handle collisions. 这道题让 ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 37. leetcode 108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
随机推荐
- You have new mail in /var/spool/mail/root 烦不烦你(转)
转自(http://blog.csdn.net/yx_l128125/article/details/7425182) 有时在进入系统的时候经常提示You have new mail in /var/ ...
- 【转】oracle PLSQL常用方法汇总
原文:http://www.cnblogs.com/luluping/archive/2010/03/10/1682885.html 在SQLPLUS下,实现中-英字符集转换alter session ...
- oracle 查看表的定义
select t.table_name 表名, c.comments 字段名称, t.column_name 字段编码, t.data_type || '(' || to_char(t.data_le ...
- POJ 1860 Currency Exchange 毫无优化的bellman_ford跑了16Ms,spfa老是WA。。
题目链接: http://poj.org/problem?id=1860 找正环,找最长路,水题,WA了两天了.. #include <stdio.h> #include <stri ...
- kill -9杀掉nginx主进程、reload失败解决办法
前言: 无意间使用 kill -9 命令杀掉了nginx的主进程,当我再次使用 ./nginx -s reload 重新刷新nginx的时候,一直出现了下面的错误信息: nginx: [alert] ...
- 关于Action中ValidateXXX方法校验一次失败后\导致以后一直返回input视图的情况
问题: 使用struts2的validateXXX()进行验证,出错后自动返回之前的画面,但是Form中的数据全部丢失了,如何才能保存出错前的Form是要解决的问题. 调查: 调查后发 ...
- 根据WSDL生成代理类方式
方式一: 1.使用VS2010提供的工具wsdl.exe由WSDL文件生成cs文件 使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转 ...
- PS字体工具字体显示不出来
显示一个小点:搜索了各种答案,扩大字号(最大72)更改前景色和背景色,最正确的解释就是分辨率太低,我发现分辨率是1,一般的设置成300,1分辨率情况下可以在图层那选择文字图层,然后按ctrl+t,拉大 ...
- Cloud Insight 现在已经支持监控 Cassandra 啦!
Cassandra 是什么? Apache Cassandra 以其可扩展性和容错分布式数据库系统而被人所熟知.Cassandra 起源于Facebook 最初创建于 Amazon Dynamo 和谷 ...
- PYTHON--CLASS
class Robot: population = 0 def __init__(self, name): self.name = name print("(Initializing {0} ...