哈希表
1. 哈希表理论基础
哈希表起始就是根据关键码的值而直接进行访问的数据结构。数组就是一张哈希表
哈希表一般用来快速判断一个元素是否出现在集合里面。例如要在学校里面查询一个名字,要是枚举的话时间复杂度就是O(n),但使用hashtable的话,只需要O(1)就可以做到。
- 哈希表的类型:
- 数组
- set集合
- map集合
什么时候使用哈希法:当我们需要查询一个元素是否出现过,或者一个元素是否在集合里面的时候,就要在第一时间想到哈希法。
242 有效的字母异位词
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。
class Solution {
public boolean isAnagram(String s, String t) {
int[] record = new int[26];
for(int i = 0; i < s.length(); i++){
record[s.charAt(i) - 'a']++;
}
for(int i = 0; i < t.length(); i++){
record[t.charAt(i) - 'a']--;
}
for(int count : record){
if(count != 0){
return false;
}
}
return true;
}
}
349 两个数字的交集
给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if(nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0){
return new int[0];
}
Set<Integer> set1 = new HashSet<>();
Set<Integer> resSet = new HashSet<>();
//遍历数组1
for(int i : nums1){
set1.add(i);
}
for(int i : nums2){
if(set1.contains(i)){
resSet.add(i);
}
}
return resSet.stream().mapToInt(x -> x).toArray();
}
}
202 快乐数
class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while(n != 1 && !set.contain(n)){
set.add(n);
n = getNextNumber(n);
}
return n == 1;
}
private int getNextNumber(int n){
int res = 0;
while(n > 0){
int temp = n % 10;
res += temp;
n = n /10;
}
return res;
}
}
1 两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
这道题我们不仅要找出和为target的两个数,同时还要获取对应的下标
所以此题,我们使用map,key存放值,value存放下标
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
if(nums == null || nums.length == 0){
return res;
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
break;
}
map.put(nums[i], i);
}
return res;
}
}
454.四数相加Ⅱ
给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<>();
int temp = 0;
int res = 0;
//统计两个数组中的元素之和,同时统计出现的次数,放入map
for(int i : nums1){
for(int j : nums2){
temp = i + j;
if(map.containsKey(temp)){
map.put(temp, map.get(temp) + 1);
}else{
map.put(temp, 1);
}
}
}
//统计剩余两组的元素之和,在map中寻找是否存在相加为0 的情况,同时记录次数
for(int i : nums3){
for(int j : nums4){
temp = i + j;
if(map.containsKey(0 - temp)){
res += map.get(0 - temp);
}
}
}
return res;
}
}
383 赎金信
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] temp = new int[26];
char[] char1 = ransomNote.toCharArray();
char[] char2 = magazine.toCharArray();
for(int i = 0; i < char1.length; i++){
temp[char1[i] - 'a']++;
}
for(int i = 0; i < char2.length; i++){
temp[char2[i] - 'a']--;
}
for(int i = 0; i < temp.length; i++){
if(temp[i] > 0){
return false;
}
}
return true;
}
}
15 三数之和
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
//找出 a + b + c = 0;
//a = nums[i], b = nums[left], c = nums[right]
for(int i = 0; i < nums.length; i++){
//排序之后如果第一个元素大于零,那直接返回
if(nums[i] > 0){
return result;
}
//去重a
if(i > 0 && nums[i] == nums[i - 1]){
continue;
}
int left = i + 1;
int right = nums.length - 1;
while(right > left){
int sum = nums[i] + nums[left] + nums[right];
if(sum > 0){
right--;
}else if(sum < 0){
left++;
}else{
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
//去重
while(right > left && nums[right] == nums[right - 1]) right--;
while(right > left && nums[left] == nums[left + 1]) left++;
right--;
left++;
}
}
}
return result;
}
}
18 四数之和
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0; i < nums.length; i++){
if(nums[i] > 0 && nums[i] > target){
return result;
}
//对nums[i] 去重
if(i > 0 && nums[i - 1] == nums[i]){
continue;
}
for(int j = i + 1; j < nums.length; j++){
if(j > i + 1 && nums[j - 1] == nums[j]){
continue;
}
int left = j + 1;
int right = nums.length - 1;
while(right > left){
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if(sum > target){
right--;
}else if(sum < target){
left++;
}else{
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while(right > left && nums[right] == nums[right - 1]) right--;
while(right > left && nums[left] == nums[left + 1]) left++;
left++;
right--;
}
}
}
}
return result;
}
}