今天在复习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. 动态SQL实现批量删除指定数据库的全部进程

    动态SQL实现批量删除指定数据库的全部进程 DECLARE @DatabaseName nvarchar(100) SET @DatabaseName = N'Account_006_Kaikei_2 ...

  2. 理解Java中的协变返回类型

    在面向对象程序设计中,协变返回类型指的是子类中的成员函数的返回值类型不必严格等同于父类中被重写的成员函数的返回值类型,而可以是更 "狭窄" 的类型. Java 5.0添加了对协变返 ...

  3. siege 压力测试

    Ben: $ siege -u shemp.whoohoo.com/Admin.jsp -d1 -r10 -c25 ..Siege 2.65 2006/05/11 23:42:16 ..Prepari ...

  4. C++库编译

    C++库编译 1. POCO编译 进入源文件目录,选择相应的VS版本进行编译(build_vs140).    设置系统环境变量: POCO_BASE 2. Boost编译 ####简单编译    在 ...

  5. SQL SERVER 2008 使用TDE加密和解密

    SQL SERVER 2008 加密和解密,这样的文件在互联网上不胜枚举,本文的寓意还是一样,一为记录,二可以为开发者提供在实现过程中的注意事项. TDE: Transparent data encr ...

  6. 查看用户列表在Linux

    Linux下查看用户列表   原文地址:http://xiaod.in/read.php?77 俺的centos vps上面不知道添加了多少个账户,今天想清理一下,但是以前还未查看过linux用户列表 ...

  7. RegexKitLite 使用详解

    1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中. 2.工程中添加libicucore.dylib frameworks. 友情提醒:一般 ...

  8. 在开发项目中有些常用的的实用代码(ps:平时看着无关紧要的,却很容易忘记)

    1,在客户端使用Cookie document.cookie = "key=1"; document.cookie = "name=zhangsan"; coo ...

  9. C#中Socket编程解决应用程序直接的通信

    using System;using System.Collections.Generic;using System.Linq;using System.Text; using System.Net; ...

  10. linux线程(一)基本应用

    有感而发(可以直接忽略~):每次要用到线程,都要在网上重新学下基础,例子倒是不少:一种是排版好,讲的不全又不是自己想要的:一种是排版不好,直接略过了.两者兼有的又要苦苦寻找,所以还是自己总结了,觉得每 ...