今天在复习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. 尝试一下用MARKDOWN嵌入代码

    public void test(){ // }

  2. 负载均衡-多台机子session不起效:把php.ini中file改为memcache存储

    一 开启memcache服务 二 修改php.ini中session配置 php/lib/php.ini session.save_handler = memcache session.save_pa ...

  3. Eyeshot Ultimate 学习笔记(2)

    导入模型 一般情况下,我们自己搭建模型的功力还不够,大多都是在3Dmax中做好模型,导出成模型文件,然后再导入Eyeshot视图中.导入的代码包括: OpenFileDialog openFileDi ...

  4. ecshop在nginx下实现负载均衡

    ecshop在负载方面的功能是十分弱小的.当你的IP每个小时到达了一万IP.如果在带宽和服务器硬件有限的情况下.你的服务器很快就会崩溃的.网站直接挂掉.为了增强ecshop在负载均衡方面的能力.我们可 ...

  5. C# WPF 从网络加载图片到byte[]数组中 Stream转byte[]代码

    折腾一中午 因为NetworkStream不支持Length属性 private byte[] GetImageFromResponse(WebResponse response) { using ( ...

  6. MYSQL ERROR 1130: Host is not allowed to connect to this MySQL server

    今天安装MYSQL遇到MYSQL ERROR 1130: Host is not allowed to connect to this MySQL server, 试了很多办法都不行 skip-gra ...

  7. bzoj 1845: [Cqoi2005] 三角形面积并 扫描线

    1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 848  Solved: 206[Submit][Statu ...

  8. [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】

    题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的 ...

  9. Codeforces Round #198 (Div. 2) —— C

    C题很容易看懂题目,不过两个循环肯定会TLE,所以得用点小聪明: 首先排好序,因为是全排列,乱序和顺序的结果是一样的: 然后呢···· 如果是数列 1 2 3 4 5 元素1 被 2 3 4 5每个减 ...

  10. POJ 1286 Necklace of Beads(Polya定理)

    点我看题目 题意 :给你3个颜色的n个珠子,能组成多少不同形式的项链. 思路 :这个题分类就是polya定理,这个定理看起来真的是很麻烦啊T_T.......看了有个人写的不错: Polya定理: ( ...