-
两数之和
目标值-当前值 是否在map中,没有的话将当前值放进map继续循环
public int[] twoSum(int[] nums, int target) {
if (nums.length < 2) {
return new int[]{};
}
Map<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])){
return new int[]{i, map.get(target - nums[i])};
}
map.put(nums[i], i);
}
return new int[]{};
}
-
字母异位词分组
排序每个单词作为key,value为源单词组成的列表
public List<List<String>> groupAnagrams(String[] strs) {
if (strs == null || strs.length == 0) {
return new ArrayList<>();
}
Map<String, List<String>> map = new HashMap<>();
for (String str : strs) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
String charsSorted = new String(chars);
List<String> valueList = map.getOrDefault(charsSorted, new ArrayList<>());
valueList.add(str);
map.put(charsSorted, valueList);
}
return new ArrayList<>(map.values());
}
-
最长连续序列
HashSet去重;遍历,有i-1(是:跳过;否:长度=1);while(i+1)长度++;maxLength = currentLength;
public int longestConsecutive(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int maxLength = 0,currentLength = 0;
for (Integer i : set) {
if (set.contains(i-1)) {
continue;
}
currentLength = 1;
while (set.contains(i+1)) {
i++;
currentLength++;
}
if (currentLength > maxLength) {
maxLength = currentLength;
}
}
return maxLength;
}
-
移动0到末尾
左右指针都指向第一个;右不为0->左右交换 左++;右++(实际:左移非0)
public void moveZeroes(int[] nums) {
int left = 0, right = 0, temp;
while (right < nums.length) {
if (nums[right] != 0) {
// swap
temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left++;
}
right++;
}
}
-
盛水最多
双指针分别在左右两端,向中间移动较小的
public int maxArea(int[] height) {
int left = 0, right = height.length - 1, maxArea = 0;
while (left < right) {
int area = (right - left) * Math.min(height[left], height[right]);
maxArea = Math.max(area, maxArea);
if (height[left] > height[right]) {
right--;
} else {
left++;
}
}
return maxArea;
}
-
三数之和
排序,双指针向中间移动
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> sum = new ArrayList<>();
Arrays.sort(nums);
int length = nums.length;
//1.for循环左指针
for (int left = 0; left < length - 2; left++) {
if (left > 0 && nums[left] == nums[left - 1]) {
continue;
}
//2.mid、right初始化,然后while循环他俩,++ --
int mid = left + 1, right = length - 1;
while (mid < right) {
int target = nums[left] + nums[mid] + nums[right];
if (target == 0) {
List<Integer> list = new ArrayList<>();
list.add(nums[left]);
list.add(nums[mid]);
list.add(nums[right]);
sum.add(list);
//去重
while (mid < right && nums[mid] == nums[mid + 1]) {
mid++;
}
while (mid < right && nums[right] == nums[right - 1]) {
right--;
}
mid++;
right--;
} else if (target < 0) {
mid++;
} else {
right--;
}
}
}
return sum;
}
-
最长无重复字符串
HashMap记录元素最后出现的位置,遍历字符串,当前元素到他前一个所在的位置的距离 取最大值,最后一个要特别处理下
public int lengthOfLongestSubstring(String s) {
// 哈希表记录元素最后出现的位置
Map<Character, Integer> map = new HashMap<>();
int distance = 0;//距离差
int left = 0;//当前元素前一个位置
int i;
for (i = 0; i < s.length(); i++) {
char currentChar = s.charAt(i);
distance = Math.max(distance, i - left);//i - left:当前元素到他前一个所在的位置 距离
Integer v = map.get(currentChar);
left = (v != null) ? Math.max(left, v + 1) : left;
map.put(currentChar, i);
}
return Math.max(distance, i - left);//最后一个元素到他上次出现的位置的差值 取大
}