Day1:两数之和 + 字母异位词分组
Hot1:两数之和
方法一:复杂度
class Solution {
public int[] twoSum(int[] nums, int target) {
int index1 = 0;
int index2 = 0;
outer:
for(int i = 0; i < nums.length; i++)
{
for(int j = i + 1; j < nums.length; j++)
{
int temp1 = nums[i];
int temp2 = nums[j];
if(temp1 + temp2 == target)
{
index1 = i;
index2 = j;
break outer;
}
}
}
return new int[]{index1, index2};
}
}
1、break只会跳过内层循环,要用outer:
2、动态数组 new int[]{,,,}的语法
方法二:HashMap
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++)
{
int temp = nums[i];
int temp2 = target-temp;
if(map.containsKey(temp2))
{
return new int[]{i, map.get(temp2)};
}
map.put(temp, i);
}
// 如果没有找到,抛出异常
throw new IllegalArgumentException("No solution found");
}
}
1、HashMap<Integer, Integer> map = new HashMap<>() 的哈希表初始化语法
2、map.put(K, V), map.containsKey(), map.containsValue()
3、异常:throw new IllegalArgumentException(" ")
4、此题主要是遍历数组,然后看target-temp值在不在集合中,在的话返回该值对应的索引
Hot49:字母异位词分组
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> map = new HashMap<>();
for(String str : strs)
{
char[] word = str.toCharArray();
Arrays.sort(word);
String word_key = new String(word);
List<String> word_per_group = map.getOrDefault(word_key, new ArrayList<String>());
word_per_group.add(str);
map.put(word_key, word_per_group);
}
return new ArrayList<List<String>>(map.values());
}
}
1、增强for用法: for(String str : strs)
2、string与char之间的工具函数:toCharArray(), Arrays.sort()以及字符数组转String
3、HashMap的函数: map.getOrDefault( ,)
4、不能使用return new List<List<String>>(map.values());,因为List是一个接口,不能直接实例化。只有具体的实现类(如 ArrayList
、LinkedList
等)才能被实例化。
5、此题主要是看重新排序后的字符串Key(通过char排序后重置为字符串)是否在HashMap中存在,存在的话依据Key取出包含的原始字符串记录并追加。
Day2:最长连续序列+移动零
Hot128:最长连续序列
public class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int currentMax = 0;
for (int num : set) {
if (!set.contains(num-1)) {
int seqSize = 0;
int currentNum = num;
while (set.contains(currentNum)) {
currentNum++;
seqSize++;
}
currentMax = Math.max(seqSize, currentMax);
}
}
return currentMax;
}
}
1、Hashset的定义,以及它可以去重,相关功能:set.contains()
2、数组元素转变为HashSet
3、hashset查找元素复杂度O(1),
4、此题主要是找到一个序列的起点(if (!set.contains(num-1)))然后才开始计算逐次增加的数在不在set中( while (set.contains(currentNum)) {
currentNum++;
seqSize++;
})
Hot283:移动零
public void moveZeroes(int[] nums) {
int leftZeroIndex = 0;
int rightSearchIndex = 0;
while (rightSearchIndex < nums.length){
if (nums[rightSearchIndex] != 0){
int temp = nums[rightSearchIndex];
nums[rightSearchIndex] = nums[leftZeroIndex];
nums[leftZeroIndex] = temp;
leftZeroIndex++;
}
rightSearchIndex++;
}
}
1、对数组操作最重要就是其索引
2、此题主要是一个leftZeroIndex记录最左边的零的位置,一个 rightSearchIndex执行向右探索,一旦探索到非0,就要和最左边的0互换(leftZeroIndex位置),互换后,leftZeroIndex右移,因为此位置已经被换成非0了(leftZeroIndex右移后所指不一定是0,但是一定会指到0,因为右移不是0的话,说明leftZeroInde==rightSearchIndex,例如1 2 3 0 1这个集合)
Day2:最长连续序列+移动零
Hot11:盛水最多的容器
public int maxArea(int[] height) {
int left = 0, right = height.length - 1;
int maxArea = 0;
while (left < right){
if (height[left] < height[right]){
maxArea = Math.max(Math.min(height[left], height[right]) * (right - left), maxArea);
left++;
}
else {
maxArea = Math.max(Math.min(height[left], height[right]) * (right - left), maxArea);
right--;
}
}
return maxArea;
}
1、此题主要是:双指针分别指向两端,在假设每次只能移动一个指针额前提下,要总是移动低的那一端(因为,移动高的那一端只会更小,高度的min以及由低的那一端决定了)。