242.有效的字母异位词
知识点补充:
1.遍历HashMap中的值:
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(Integer num:map.values()){ }
2.遍历HashMap的键:
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(Integer num:map.keySet()){ }
3.t.charAt(i)可以获取到字符串索引为i的字符
4.字符串获取长度方法:
str.length();
5.在Java中,字符类型(char)实际上是一个无符号的16位整数,它表示的是字符在Unicode字符集中的编码。对于ASCII字符集,字符’a’的Unicode编码是97,字符’b’的编码是98,以此类推,直到字符’z’的编码是122。当你执行s.charAt(i) - ‘a’时,你实际上是在计算字符和字符’a’之间的差值。这个差值将是一个整数,它表示字符在字母表中的位置。例如:
如果s.charAt(i)是’a’,那么s.charAt(i) - ‘a’的结果是0。
如果s.charAt(i)是’b’,那么s.charAt(i) - ‘a’的结果是1。
如果s.charAt(i)是’c’,那么s.charAt(i) - ‘a’的结果是2。
以此类推,直到
如果s.charAt(i)是’z’,那么s.charAt(i) - 'a’的结果是25。
这个计算结果(0到25的整数)可以直接用作数组索引,因为小写英文字母的数量正好是26个
法一:
1.将两个字符串预处理,转成字符数组
2.用HashMap存储出现的字符以及对应字符出现的次数
3.先往hashmap存其中一个字符数组的单词出现个数,再遍历另一个字符数组,出现map中的字母就–
4.判断map中每一个value是否为0,如果不为0就返回false
class Solution {
public boolean isAnagram(String s, String t) {
boolean res = true;
char[] s_chars = s.toCharArray();
char[] t_chars = t.toCharArray();
HashMap<Character,Integer> existNumMap = new HashMap<Character,Integer>();
for(char s_char:s_chars){
if(existNumMap.containsKey(s_char)){
int existNum = existNumMap.get(s_char);
existNumMap.put(s_char,++existNum);
} else{
existNumMap.put(s_char,1);
}
}
for(char t_char:t_chars){
if(existNumMap.containsKey(t_char)){
int existNum = existNumMap.get(t_char);
existNumMap.put(t_char,--existNum);
}else{
res = false;
}
}
//hashmap中value和key的遍历方式,总结一下
for(Integer num:existNumMap.values()){
if(num != 0) res = false;
}
return res;
}
}
法二:
1.创建一个长度为26的数组record
2.record[s.charAt(i)-‘a’]++;记录字符串s中字符出现的次数
3.record[t.charAt(i)-‘a’]–;t字符串中字符出现一次对应减一次
4.最后判断record数组中是否全为0,全为0两个字符串中字符出现次数一样
class Solution {
public boolean isAnagram(String s, String t) {
int[] record = new int[26];//记录每一个字母出现的次数
for(int i=0;i<s.length();i++){//字符串的长度方法.length()
record[s.charAt(i)-'a']++;//出现一次就加一
}
for(int i=0;i<t.length();i++){
record[t.charAt(i)-'a']--;//出现一次就减一
}
//如果两个字符串的字母出现次数一致,那么record中的所有值都是0
for(int r:record){
if(r != 0) return false;
}
return true;
}
}
方法对比:
1.方法二相比于方法一,不需要创建两个字符数组,不需要创建HashMap,只要创建一个数组,消耗内存空间更小。
2.法一需要对每一个字符的value取值–>操作–>放回,而法二直接对数组进行修改,时间消耗更小。
3.时间和空间复杂度都是O(n),但是法二相比于法一时间和空间上都提升很多。
349. 两个数组的交集
知识点补充:
HashSet类型的集合和数组之间的转换:
int i = 0;
int[] result = new int[res.size()];
for(int num:res){
result[i] = num;
i++;
}
题解:
思路就是先遍历一个数组,将出现过的数组存入具有元素唯一特性的HashSet中,以收集该数组出现过的所有值。再遍历另一个数组,判断HashSet中是否有这个数字,如果出现就是两个数组的交集。
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set = new HashSet<Integer>();
HashSet<Integer> res = new HashSet<Integer>();
for(int num:nums1){
set.add(num);
}
for(int num:nums2){
if(set.contains(num)){
res.add(num);
}
}
int i = 0;
int[] result = new int[res.size()];
for(int num:res){
result[i] = num;
i++;
}
return result;
}
}
202. 快乐数
这道题的关键是一个方法一个判断:一个方法是每次按照题目要求变换数字的方法,一个判断是判断是否进入循环以及是否为1的判断:
1.判断快乐数的方法:如果num如果大于等于0,就%10取余,得到的结果平方加入total;num = num/10得到下一个用来取余的数---->指导num小于10
2.创建一个set集合,存储每一个按照规则算出来的数,一旦出现之前已经加入到set的数了,说明开始循环了,就跳出while,res置为false,这个数不是快乐数。
如果算出来的数是1了,也会跳出while,返回res=true。
class Solution {
public boolean isHappy(int n) {
boolean res = true;
HashSet<Integer> set = new HashSet<Integer>();
set.add(n);
while(n != 1){
n = judgeHappy(n);
if(set.contains(n)){
res = false;
break;
}else{
set.add(n);
}
}
return res;
}
public int judgeHappy(int num){
int total = 0;
while(num >= 10){
int a = num%10;
total += a*a;
num = num/10;
}
total += num*num;
return total;
}
}
1. 两数之和
**关键:**如果map中没有target-nums[i],说明在之前没有出现可以和当前数配对相加为target的数,那么就先把他加进map,等到那个输出现,再将他们的索引成对返回。
class Solution{
public int[] twoSum(int[] nums,int target){
HashMap<Integer,Integer> map = new HashMap<>();
//创建一个map
for(int i = 0;i<nums.length;i++){
//如果map中没有target-nums[i],说明在之前没有出现可以和当前数配对相加为target的数,那么就先把他加进map,等到那个输出现,再将他们的索引成对返回。
if(map.containsKey(target-nums[i])){
return new int[]{map.get(target-nums[i]),i};
}
map.put(nums[i],i);
}
return new int[0];
}
}