哈希表相关题集
Posted *平芜尽处是春山*
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了哈希表相关题集相关的知识,希望对你有一定的参考价值。
哈希表相关题集
剑指 Offer II 016. 不含重复字符的最长子字符串
class Solution
public static int lengthOfLongestSubstring(String s)
HashMap<Character,Integer> hashMap = new HashMap<> ();
int max = 0;
int left = 0;
for(int i = 0;i < s.length();i++)
if(hashMap.containsKey(s.charAt(i)))
left = Math.max(left,hashMap.get(s.charAt(i)) + 1);
hashMap.put(s.charAt(i),i);
max = Math.max(max,i - left + 1);
return max;
运行截图:
468. 验证IP地址
class Solution
public String validIPAddress(String queryIP)
return isIPv4(queryIP) ? "IPv4" : isIPv6(queryIP) ? "IPv6" : "Neither";
public boolean isIPv4(String s)
String t[] = s.split("\\\\.", -1);
if(t.length != 4)
return false;
for(int i = 0;i < 4;i++)
if(t[i].length() == 0 || t[i].length() > 3 || t[i].length() > 1 && t[i].charAt(0) == '0')
return false;
int sum = 0;
for(char c : t[i].toCharArray())
if(!Character.isDigit(c))
return false;
sum = sum * 10 + c -'0';
if(sum > 255)
return false;
return true;
public boolean isIPv6(String s)
s = s.toLowerCase();
String t[] = s.split(":", -1);
if(t.length != 8)
return false;
for(int i = 0;i < 8;i++)
if(t[i].length() == 0 || t[i].length() > 4)
return false;
for(char c : t[i].toCharArray())
if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f'))
return false;
return true;
运行截图:
705. 设计哈希集合
class MyHashSet
private static final int BASE = 769;
private LinkedList[] data;
public MyHashSet()
data = new LinkedList[BASE];
for (int i = 0; i < BASE; ++i)
data[i] = new LinkedList<Integer>();
public void add(int key)
int h = hash(key);
Iterator<Integer> iterator = data[h].iterator();
while (iterator.hasNext())
Integer element = iterator.next();
if (element == key)
return;
data[h].offerLast(key);
public void remove(int key)
int h = hash(key);
Iterator<Integer> iterator = data[h].iterator();
while (iterator.hasNext())
Integer element = iterator.next();
if (element == key)
data[h].remove(element);
return;
public boolean contains(int key)
int h = hash(key);
Iterator<Integer> iterator = data[h].iterator();
while (iterator.hasNext())
Integer element = iterator.next();
if (element == key)
return true;
return false;
private static int hash(int key)
return key % BASE;
运行截图:
706. 设计哈希映射
class MyHashMap
private class Pair
private int key;
private int value;
public Pair(int key,int value)
this.key = key;
this.value = value;
public int getKey()
return key;
public int getValue()
return value;
public void setValue(int value)
this.value = value;
public static final int BASE = 769;
private LinkedList[] data;
public MyHashMap()
data = new LinkedList[BASE];
for(int i = 0;i < BASE; ++i)
data[i] = new LinkedList<Pair> ();
public void put(int key, int value)
int h = hash(key);
Iterator<Pair> iterator = data[h].iterator();
while(iterator.hasNext())
Pair pair = iterator.next();
if(pair.getKey() == key)
pair.setValue(value);
return;
data[h].offerLast(new Pair(key,value));
public int get(int key)
int h = hash(key);
Iterator<Pair> iterator = data[h].iterator();
while(iterator.hasNext())
Pair pair = iterator.next();
if(pair.getKey() == key)
return pair.value;
return -1;
public void remove(int key)
int h = hash(key);
Iterator<Pair> iterator = data[h].iterator();
while(iterator.hasNext())
Pair pair = iterator.next();
if(pair.getKey() == key)
data[h].remove(pair);
return;
private static int hash(int key)
return key % BASE;
运行截图:
以上是关于哈希表相关题集的主要内容,如果未能解决你的问题,请参考以下文章