文章目录
Leetcode 242. 有效的字母异位词
题目:242. 有效的字母异位词
解析:2.5 练习时长两年半
解题思路
使用长度为26的数组作为hash数组。
代码
class Solution {
public boolean isAnagram(String s, String t) {
int[] record = new int[26];
for (char c : s.toCharArray())
record[c - 'a']++;
for (char c : t.toCharArray())
record[c - 'a']--;
for (int i = 0; i < record.length; i++)
if (record[i] != 0)
return false;
return true;
}
}
总结
暂无
Leetcode 349. 两个数组的交集
题目:349. 两个数组的交集
解析:2.5 练习时长两年半
解题思路
使用Hash数组,整体方法同上题,额外使用ArrayList来动态存储结果,最终转化为数组返回结果。
代码
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int[] hash1 = new int[1001];
int[] hash2 = new int[1001];
for (int num : nums1)
hash1[num] = 1;
for (int num : nums2)
hash2[num] = 1;
List<Integer> resList = new ArrayList<>();
for (int i = 0; i < 1001; i++)
if (hash1[i] == 1 && hash2[i] == 1)
resList.add(i);
int[] res = new int[resList.size()];
int i = 0;
for (int num : resList)
res[i++] = num;
return res;
}
}
总结
当没有限定元素大小时,需要使用HashSet容器
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> resSet = new HashSet<>();
for (int num : nums1)
set1.add(num);
for (int num : nums2)
if (set1.contains(num))
resSet.add(num);
int[] res = new int[resSet.size()];
int i = 0;
for (int num : resSet)
res[i++] = num;
return res;
}
}
//一般方法
int[] res = new int[resSet.size()];
int i = 0;
for (int num : resSet)
res[i++] = num;
return res;
//resSet.stream():将Set集合resSet转换为一个Stream流对象。
//mapToInt(x -> x):使用mapToInt方法将Stream流中的每个元素映射为int类型。这里的x -> x表示将每个元素直接作为int类型返回。
//toArray():将Stream流中的元素收集到一个int类型的数组中。
return resSet.stream().mapToInt(x -> x).toArray();
Leetcode 202. 快乐数
题目:202. 快乐数
解析:2.5 练习时长两年半
解题思路
使用HashSet来记录是否有重复,有重复但没到1,说明再也到不了1
代码
class Solution {
public boolean isHappy(int n) {
Set<Integer> record = new HashSet<>();
while (n != 1 && record.contains(n) == false){
record.add(n);
n = getNextNum(n);
}
return n == 1;
}
private int getNextNum(int n){
int res = 0;
while (n > 0){
int tmp = n % 10;
res += tmp * tmp;
n = n / 10;
}
return res;
}
}
总结
暂无
Leetcode 1. 两数之和
题目:1. 两数之和
解析:2.5 练习时长两年半
解题思路
使用HashMap,记录当前元素。寻找target-nums[i]是否出现过。
代码
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 tmp = target - nums[i];
if (map.containsKey(tmp)){
res[1] = i;
res[0] = map.get(tmp);
}
map.put(nums[i], i);
}
return res;
}
}
总结
对HashMap的使用还是比较生疏
HashSet基本使用:
//创建HashSet:
HashSet<String> set = new HashSet<>();
//添加元素:
set.add("A");
set.add("B");
set.add("C");
//获取 HashSet 的大小:
int size = set.size(); // 如果加入了三个元素,则返回 3
//检查是否包含某个元素:
boolean hasElement = set.contains("A"); // 如果 "A" 在集合中,则返回 true
//删除元素:
set.remove("A");
//清空 HashSet:
set.clear();
//判断 HashSet 是否为空:
boolean isEmpty = set.isEmpty();
//遍历 HashSet:
// 使用迭代器
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
// ... do something with element
}
// 使用 forEach 和 lambda 表达式
set.forEach(element -> {
// ... do something with element
});
// 使用 for-each 循环
// 使用迭代器
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
// ... do something with element
}
// 使用 forEach 和 lambda 表达式
set.forEach(element -> {
// ... do something with element
});
// 使用 for-each 循环
for (String element : set) {
// ... do something with element
}
//转换为数组:
String[] array = set.toArray(new String[0]);
HashMap基本使用:
//创建HashMap:
HashMap<String, Integer> map = new HashMap<>();
//添加元素:
map.put("key1", 1);
map.put("key2", 2);
//获取元素:
Integer value = map.get("key1"); // returns 1
//检查是否包含某个键:
boolean hasKey = map.containsKey("key1"); // returns true
//删除元素:
map.remove("key1");
//遍历HashMap:
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
// ... do something with key and value
}
//判断HashMap是否为空:
boolean isEmpty = map.isEmpty();
//获取HashMap的大小:
int size = map.size();
//清空HashMap:
map.clear();
//其他静态和实例方法
//clone、putAll、removeAll