谷歌上机题
google 公司的一个上机题: 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id, 性别, 年龄,住址…),当输入该员工的 id 时,要求查找到该员工的所有信息 要求:不使用数据库,尽量节省内存,速度越快越好 => 哈希表(散列)
Hash表介绍
散列表(Hash table, 也叫哈希表(对照java的HashMap)
- 它通过把关键码值映射到表中一个位置来访问记录, 以加快查找的速度。 这个映射函数叫做散列函数, 存放记录的数组叫做散列表。
- 实现思路
根据对象的 hashCode 值,找到对应的数组下标,其实就是找到存储对象的链表
Hash表作用
- 缓存
- 快速定位查找
代码实现
员工emp。也就是每一个链表节点
static class Emp {
public int id;
public String name;
public Emp next;
public Emp(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
链表
static class EmpLikendList {
private Emp head;
public void addEmp(Emp emp) {
if (head == null) {
head = emp;
return;
}
Emp temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = emp;
}
public void listEmp() {
if (head == null) {
System.out.println("emp链表为空~");
return;
}
Emp temp = head;
while (true) {
System.out.println("id:" + temp.id + "---name:" + temp.name);
if (temp.next == null) {
break;
}
temp = temp.next;
}
}
public Emp findByEmpId(int id){
if (head == null){
return null;
}
Emp temp = head;
while (temp != null){
if (temp.id == id){
break;
}
temp = temp.next;
}
return temp;
}
public void delById(int id){
if (head == null){
System.out.println("链表为空~");
return;
}
if (head.id == id){
if (head.next == null){
head = null;
}else {
Emp temp = head.next;
head.id = temp.id;
head.name = temp.name;
head.next = temp.next;
}
return;
}
boolean flag = false;
Emp temp = head;
while (true){
if (temp.next == null){
break;
}
if (temp.next.id == id){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.next = temp.next.next;
}else {
System.out.println("未找到要删除的员工=>id:"+id);
}
}
}
Hash表,多个链表组成的数组
static class HashTable {
private EmpLikendList[] empLikendLists;
private int size;
public HashTable(int size) {
this.size = size;
empLikendLists = new EmpLikendList[size];
for (int i = 0; i < empLikendLists.length; i++) {
empLikendLists[i] = new EmpLikendList();
}
}
public void add(Emp emp) {
int hash = getHash(emp.id);
empLikendLists[hash].addEmp(emp);
}
public void listHashTable() {
for (int i = 0; i < size; i++) {
System.out.println("=====第" + (i + 1) + "个链表=====");
empLikendLists[i].listEmp();
}
}
public void findById(int id){
int hash = getHash(id);
Emp emp = empLikendLists[hash].findByEmpId(id);
if (emp == null){
System.out.println("未找到id为:"+id+"的员工");
}else {
System.out.println("id:" + emp.id + "---name:" + emp.name);
}
}
public void delById(int id){
int hash = getHash(id);
empLikendLists[hash].delById(id);
}
public int getHash(int id) {
return id % size;
}
}
完整代码
package com.ghl.hash;
import java.util.Scanner;
public class HashTableDemo {
public static void main(String[] args) {
HashTable table = new HashTable(6);
String opera = "";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("add:添加员工");
System.out.println("list:显示");
System.out.println("find:查找");
System.out.println("del:id");
System.out.println("exit:退出");
opera = scanner.next();
switch (opera) {
case "add":
System.out.println("输入id");
int id = scanner.nextInt();
System.out.println("输入name");
String name = scanner.next();
Emp emp = new Emp(id, name);
table.add(emp);
break;
case "list":
table.listHashTable();
break;
case "find":
System.out.println("输入要查找的员工id");
int findId = scanner.nextInt();
table.findById(findId);
break;
case "del":
System.out.println("输入要删除的员工id");
int delId = scanner.nextInt();
table.delById(delId);
break;
default:
scanner.close();
System.exit(0);
}
}
}
static class HashTable {
private EmpLikendList[] empLikendLists;
private int size;
public HashTable(int size) {
this.size = size;
empLikendLists = new EmpLikendList[size];
for (int i = 0; i < empLikendLists.length; i++) {
empLikendLists[i] = new EmpLikendList();
}
}
public void add(Emp emp) {
int hash = getHash(emp.id);
empLikendLists[hash].addEmp(emp);
}
public void listHashTable() {
for (int i = 0; i < size; i++) {
System.out.println("=====第" + (i + 1) + "个链表=====");
empLikendLists[i].listEmp();
}
}
public void findById(int id){
int hash = getHash(id);
Emp emp = empLikendLists[hash].findByEmpId(id);
if (emp == null){
System.out.println("未找到id为:"+id+"的员工");
}else {
System.out.println("id:" + emp.id + "---name:" + emp.name);
}
}
public void delById(int id){
int hash = getHash(id);
empLikendLists[hash].delById(id);
}
public int getHash(int id) {
return id % size;
}
}
static class EmpLikendList {
private Emp head;
public void addEmp(Emp emp) {
if (head == null) {
head = emp;
return;
}
Emp temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = emp;
}
public void listEmp() {
if (head == null) {
System.out.println("emp链表为空~");
return;
}
Emp temp = head;
while (true) {
System.out.println("id:" + temp.id + "---name:" + temp.name);
if (temp.next == null) {
break;
}
temp = temp.next;
}
}
public Emp findByEmpId(int id){
if (head == null){
return null;
}
Emp temp = head;
while (temp != null){
if (temp.id == id){
break;
}
temp = temp.next;
}
return temp;
}
public void delById(int id){
if (head == null){
System.out.println("链表为空~");
return;
}
if (head.id == id){
if (head.next == null){
head = null;
}else {
Emp temp = head.next;
head.id = temp.id;
head.name = temp.name;
head.next = temp.next;
}
return;
}
boolean flag = false;
Emp temp = head;
while (true){
if (temp.next == null){
break;
}
if (temp.next.id == id){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.next = temp.next.next;
}else {
System.out.println("未找到要删除的员工=>id:"+id);
}
}
}
static class Emp {
public int id;
public String name;
public Emp next;
public Emp(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
}
|