代码随想录算法训练营第6天 | 242.有效的字母异位词、349. 两个数组的交集、202. 快乐数、1. 两数之和
第5天是休息日
242.有效的字母异位词
题目:242.有效的字母异位词
文档讲解:代码随想录-242.有效的字母异位词
视频讲解:哔哩哔哩-242.有效的字母异位词
状态/时间:看完写出来了/二十分钟
思路:
通过哈希数组来解决这道题。
因为有26个字母,刚好创建一个26大小的数组hash。下标0-a,1-b,以此类推。
在遍历字符串的每个字符的时候,都减去A,得到的就是数组hash的下标,然后进行++跟–操作,经过这样的操作,就能得出:数组如果全为0,那么就是字母异位词,如果有一个不是0,那么久不是字母异位词了。
代码:
class Solution {
public boolean isAnagram(String s, String t) {
int hash[] = new int[26];
for (int i = 0; i < s.length(); i++) {
hash[s.charAt(i) - 'a']++;
}
for (int i = 0; i < t.length(); i++) {
hash[t.charAt(i) - 'a']--;
}
for (int record : hash) {
if (record != 0) {
return false;
}
}
return true;
}
}
注意:
暂无
349. 两个数组的交集
题目:349. 两个数组的交集
文档讲解:代码随想录-349. 两个数组的交集
视频讲解:哔哩哔哩-349. 两个数组的交集
状态/时间:法一没写出来/二十分钟
法一:HashSet实现
思路:
创建两个HashSet,一个为存储num1的set1,一个为结果集resSet。
先把数组一的加入到set1当中
然后判断set1当中是否包含num2数组的元素,包含就把该元素放入结果resSet。
最后通过转换,返回数组.
代码:
import java.util.HashSet;
import java.util.Set;
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
// 法一: HashSet
if (nums1.length == 0 || nums2.length == 0) {
return nums1;
}
Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> resSet = new HashSet<Integer>();
for (int num : nums1) {
set1.add(num);
}
// 如果数组2也存在数字,添加到resSet(结果集)
for (int num : nums2) {
if (set1.contains(num)) {
resSet.add(num);
}
}
// 直接转换成数组返回
return resSet.stream().mapToInt(x -> x).toArray();
}
}
注意:
暂无
法二:
思路:
还是上一道题的思想,利用数组的下表判断两个数组是否出现同一个元素。
代码:
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int temp[] = new int[1003];
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < nums1.length; i++) {
temp[nums1[i]] = 1;
}
for (int i = 0; i < nums2.length; i++) {
if (temp[nums2[i]] == 1) {
temp[nums2[i]] = 2;
}
}
for (int i = 0; i < temp.length; i++) {
if (temp[i] == 2) {
result.add(i);
}
}
int[] intArray = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
intArray[i] = result.get(i);
}
return intArray;
}
}
注意:
202. 快乐数
题目:202. 快乐数
文档讲解:代码随想录-202. 快乐数
视频讲解:哔哩哔哩-无
状态/时间:没写出来/二十分钟
思路:
所以这道题目使用哈希法,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止。
代码:
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;
}
}
注意:
暂无
1. 两数之和
题目:1. 两数之和
文档讲解:代码随想录-1. 两数之和
视频讲解:哔哩哔哩-1. 两数之和
状态/时间:没写出来/二十分钟
思路:
这道题因为要用到元素,还有其下标,因此就需要用到HashMap了。
HashMap有key,还有value,把元素做key,下标做value。
通过遍历数组,找到target-当前值的key,如果有,那么获取下标,还有当前的下标加入到结果数组当中,返回。
代码:
class Solution {
public int[] twoSum(int[] nums, int target) {
// HashMap的key存元素,value存下标
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
// 存两个数的下标
int res[] = new int[2];
// 遍历数组
for (int i = 0; i < nums.length; i++) {
// 剩下的值
int temp = target - nums[i];
// 从map里面找一下有没有这个数值
if (map.containsKey(temp)) {
// 有的话获取下标
int val = map.get(temp);
res[0] = val;
res[1] = i;
// 返回两个数字的下标
return res;
}
// 如果map里面没有,保存到map里面
map.put(nums[i], i);
}
return res;
}
}
注意:
暂无