今天在复习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的更多相关文章

  1. [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. 这道 ...

  2. [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 ...

  3. 【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 ...

  4. 【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 ...

  5. 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 ...

  6. [CareerCup] 8.10 Implement a Hash Table 实现一个哈希表

    8.10 Design and implement a hash table which uses chaining (linked lists) to handle collisions. 这道题让 ...

  7. 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 ...

  8. Convert Sorted Array to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  9. 37. leetcode 108. Convert Sorted Array to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...

随机推荐

  1. 页面mask css

    <html> <head> <style type="text/css"> .share_mask { position: fixed; top ...

  2. ubuntu基本使用

    sudo nautilus xxx指定目录去打开 这个命令就是以root权限打开一个窗口,来管理文件

  3. 『奇葩问题集锦』Ruby 切换淘宝源报错WARNING: Error fetching data: SSL_connect returned=1 errno=0 state=SSLv3 read s erver certificate B: certificate verify failed

    ===>首先需要使用https<===https://ruby.taobao.org/ 第一步 下载http://pan.baidu.com/s/1kU0rxtH 复制到ruby安装的根目 ...

  4. jinja2 宏的简单使用总结(macro)

    Table of Contents 1. 简介 2. 用法 3. 参数和变量 4. 注意事项 4.1. macro的变量只能为如下三种: 4.2. 和block的关系: 5. 参考文档 1 简介 ji ...

  5. 在ubuntu12.0.4上搭建samba服务器以实现文件共享

    在安装之前samba服务器之前,先进行以下配置和测试. <壹> 准备工作 一.NAT联网方式 (1)硬件连接 无需网线,无需路由器 (2)虚拟机选择NAT连接方式 (3)测试网络通不通 在 ...

  6. why app_start start

    Add following code for your class: [assembly: WebActivatorEx.PostApplicationStartMethod(typeof(WeCha ...

  7. Log4j与common-logging

    Log4j与common-logging 总网上搜了些Log4j与common-logging的介绍,记录下. 一.Log4j  1.简介 Log4j是Apache的一个开放源代码项目 使用Log4j ...

  8. uva 1335 - Beijing Guards

    竟然用二分,真是想不到: 偶数的情况很容易想到:不过奇数的就难了: 奇数的情况下,一个从后向前拿,一个从前向后拿的分配方法实在太妙了! 注: 白书上的代码有一点点错误 代码: #include< ...

  9. Codeforces Round #197 (Div. 2) : B

    也是水题一个,不过稍微要细心点.... 贴代码: #include<iostream> using namespace std; long long n,m; ; int main() { ...

  10. javascript 字符串转数字

    //把str转换为数字的方式,想起很久以前的一个面试题,说字符转数字的方式有哪些,现在想了想 var str1='4.88',str2='4.8xx'; console.log(parseInt(st ...