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 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
随机推荐
- 【ADO.NET】6、SQLHelper简单封装
using System.Data.SqlClient;using System.Configuration;引用:System.Configuration 连接字符串放到配置文件中 新建一个类,写如 ...
- centos 6.5 安装jenkins
Installation sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.rep ...
- 通过telnet使用smtp协议发送邮件
smtp协议是一个简单的邮件传输协议,利用它我们可以将邮件发送给别人,这里将通过telnet这个程序利用smtp协议从网易向gmail发送一封邮件 网上不少有说使用telnet发送邮件的文章,我也看过 ...
- React组件三
<script> <!-- getDefalutPros 设置组件的默认值--> <!--var Mytitle=React.createClass({ getDefau ...
- scp命令使用
从 本地 复制到 远程 scp /home/daisy/full.tar.gz root@172.19.2.75:/home/root (然后会提示你输入另外那台172.19.2.75主机的root用 ...
- python实现雅虎拍卖后台自动回复卖家消息
前些时间,公司让做一个自动回复卖家信息的程序,现在总结下(用python实现的) 1.登陆雅虎拍卖后台手动获取cookie文件 #coding=utf-8 import sqlite3 import ...
- linux 中 ‘|’的作用
利用Linux所提供的管道符“|”将两个命令隔开,管道符左边命令的输出就会作为管道符右边命令的输入.连续使用管道意味着第一个命令的输出会作为 第二个命令的输入,第二个命令的输出又会作为第三个命令的输入 ...
- 从头搭建Spring MVC
1.拷贝jar文件 2.填充Web.xml 在/WEB-INF/web.xml中写入如下内容: <?xml version="1.0" encoding="UTF- ...
- 解决iOS应用内购买报错:invalidProductIdentifiers
当写完IAP业务过程后,点击测试却发现没有返回成功的商品Id,反而返回了无效的商品:response.invalidProductIdentifiers 这种情况下考虑以下因素: 创建的App ID是 ...
- IOS在后台每隔一段时间执行一下
步骤: 1.在info.plist里加入UIBackgroundModes键,其值为数组,数组之一为voip字符串: <key>UIBackgroundModes</key>& ...