[KamaCoder] 哈希表理论基础
哈希表(散列表)是一个可以将对象转换成关键码, 通过关键码获取到对应值的数据结构. 常见的实现方式为数组.
将对象转换成关键码需要 hash 函数, hash(obj) % tableSize 后就是对应的关键码索引的一种实现.
当我们需要判断一个元素是否在集合中的时候, 就可以考虑使用哈希表. 例如 java 里的 Set、Map.
[LeetCode] 242. 有效的字母异位词
一开始没关注到输入的两个字符串中的字符只包含小写字母, 因此用一个 Map<Character, Integer> 来存储对应的字符出现的次数, 效率稍微低了一些.
class Solution {
public boolean isAnagram(String s, String t) {
HashMap<Character, Integer> chCount = new HashMap<>();
if (s != null) {
for (int i = 0; i < s.length(); i++) {
int count = chCount.getOrDefault(s.charAt(i), 0);
chCount.put(s.charAt(i), count + 1);
}
}
if (t != null) {
for (int i = 0; i < t.length(); i++) {
int count = chCount.getOrDefault(t.charAt(i), 0);
count--;
if (count < 0) {
return false;
}
chCount.put(t.charAt(i), count);
}
}
for (Map.Entry<Character, Integer> entry : chCount.entrySet()) {
if (entry.getValue() > 0) {
return false;
}
}
return true;
}
}
通过数组 int[] chCount = new int[26] 优化:
class Solution {
public boolean isAnagram(String s, String t) {
if (s == null && t == null) {
return true;
} else if (s == null || t == null || s.length() != t.length()) {
return false;
}
int[] count = new int[26];
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
}
for (int i = 0; i < t.length(); i++) {
count[t.charAt(i) - 'a']--;
if (count[t.charAt(i) - 'a'] < 0) {
return false;
}
}
return true;
}
}
[LeetCode] 349. 两个数组的交集
使用 Set 来去重是我没想到的, 这样做简单了很多. 当然如果限制数字在 1-1000 范围内的话, 就可以不用 Set, 用 int[] numbers = new int[1001], 查找的效率会高很多.
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int[] numbers = new int[1001];// 用来去重的数据结构
for (int i = 0; i < nums1.length; i++) {
numbers[nums1[i]] = 1;
}
List<Integer> intersectionNumbers = new ArrayList<>();
for (int i = 0; i < nums2.length; i++) {
if (numbers[nums2[i]] == 1) {
numbers[nums2[i]] = 0;
intersectionNumbers.add(nums2[i]);
}
}
int[] result = new int[intersectionNumbers.size()];
for (int i = 0; i < intersectionNumbers.size(); i++) {
result[i] = intersectionNumbers.get(i);
}
return result;
}
}
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null) {
return new int[0];
}
Set<Integer> nums1Set = new HashSet<>();// 使用 hashset 来去重
for (int i = 0; i < nums1.length; i++) {
nums1Set.add(nums1[i]);
}
List<Integer> intersection = new ArrayList<>();
for (int i = 0; i < nums2.length; i++) {
if (nums1Set.remove(nums2[i])) {
intersection.add(nums2[i]);
}
}
int[] result = new int[intersection.size()];
for (int i = 0; i < intersection.size(); i++) {
result[i] = intersection.get(i);
}
return result;
}
}
[LeetCode] 202. 快乐数
快乐数为什么不会变成无限不循环的状态, 这个属于数学范畴了, 权当这是真的吧.
class Solution {
public boolean isHappy(int n) {
Set<Integer> existNumber = new HashSet<>();
int newNumber = n;
while (true) {
newNumber = getNumber(newNumber);
if (newNumber == 1) {
return true;
} else if (!existNumber.add(newNumber)) {
return false;
}
}
}
private int getNumber(int number) {
int newNumber = 0;
int current = 0;
while (number > 0) {
current = number%10;
newNumber += current * current;
number = number/10;
}
return newNumber;
}
}
[LeetCode] 1. 两数之和
虽然是 LeetCode 第一题, 不用暴力解法的话还是需要一下脑经转个弯的(可以理解为基本的算法思维).
现在让我手撕三数之和, 我也是撕不出来的... 但是面试会考啊...
同理四数之和也需要掌握... 头疼.
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numberIndex = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (numberIndex.containsKey(target - nums[i])) {
return new int[]{i, numberIndex.get(target - nums[i])};
}
if (!numberIndex.containsKey(nums[i])) {
numberIndex.put(nums[i], i);
}
}
return new int[0];
}
}