hashtable 哈希表的作用在一快速判断一个元素是否在表里面,利用哈希函时把传入数字化
class Solution {
public boolean isAnagram(String s, String t) {
// 最开始想的是最为char元素放入set中,但是set无序不能重复,所以就换map,记录s中
// 字母出现的次数,用t出现减去,之后迭代判断map中value是否为零,但是map迭代taimaf
// 所以最后选择利用数组模拟hashtable,只有小写字母所以数组长度26就可以了
// 这是map集合方法,效率太低了
// char[] sChar = s.toCharArray();
// char[] tChar = t.toCharArray();
// Map<Character,Integer> hashTable = new HashMap<Character,Integer>();
// // 先判断长度是否相等
// if(s.length() != t.length()) return false;
// // s加入map 并且自增
// for(int i = 0; i < sChar.length; i++){
// // int sum = 0;
// // if(hashTable.containsKey(sChar[i]))
// // sum = hashTable.get(sChar[i]);
// // hashTable.put(sChar[i] , sum == 0 ? 1 : sum+1);
// // 开始忘记了getordefault方法,上面重写了一个
// hashTable.put(sChar[i] , hashTable.getOrDefault(sChar[i],0) + 1);
// // int ss = hashTable.getOrDefault(hashTable.get(sChar[i]),0) + 1;
// }
// // t 加入hasntable 进行自减
// for(int i = 0; i < tChar.length; i++){
// if(!hashTable.containsKey(tChar[i])){
// // return false;
// }else{
// int num = hashTable.get(tChar[i]);
// if(num <= 0) return false;
// hashTable.put(tChar[i],num -1);
// }
// }
// //
// for(Character key : hashTable.keySet()){
// if(hashTable.get(key) == 0) continue;
// else return false;
// }
// return true;
// 数组构建hashtable方法
int[] tempArr = new int[26];
char[] sChar = s.toCharArray();
char[] tChar = t.toCharArray();
for(int i = 0; i < sChar.length; i++){
int num = tempArr[sChar[i] - 'a'] ;
tempArr[sChar[i] - 'a'] = num + 1;
}
for(int i = 0; i < tChar.length; i++){
int num = tempArr[tChar[i] - 'a'] ;
tempArr[tChar[i] - 'a'] = num - 1;
}
for(int i = 0; i < tempArr.length; i++){
if(tempArr[i] != 0) return false;
}
return true;
}
}
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
// set 就可以直接做,数组模拟hashtable也可以 map也可以 选择map
HashSet<Integer> set = new HashSet<>();
Map<Integer,Integer> resMap = new HashMap<>();
for(int i = 0; i < nums1.length; i++){
set.add(nums1[i]);
}
for(int i = 0; i < nums2.length; i++){
if(set.contains(nums2[i])){
resMap.put(nums2[i],resMap.getOrDefault(nums2[i],0));
}
}
int[] res = new int[resMap.size()];
int count = 0;
for (Integer i :
resMap.keySet()) {
res[count] = i;
count++;
}
return res;
}
}
class Solution {
public boolean isHappy(int n) {
Set<Integer> record = new HashSet<>();
while (n != 1 && !record.contains(n)) {
record.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 * temp;
n = n / 10;
}
return res;
}
}
class Solution {
public int[] twoSum(int[] nums, int target) {
// 暴力
// int[] res = new int[2];
// for(int one = 0;one < nums.length-1 ;one++){
// for(int two = one + 1; two < nums.length; two++){
// int temp = nums[one];
// int tar = target - temp;
// if(nums[two] == tar){
// res[0] = one;
// res[1] = two;
// return res;
// }
// }
// }
// return res;
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]; // 遍历当前元素,并在map中寻找是否有匹配的key
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
break;
}
map.put(nums[i], i); // 如果没找到匹配对,就把访问过的元素和下标加入到map中
}
return res;
}
}