文章目录
1. 两数之和-力扣 1 题
思路:
- 循环遍历数组,拿到每个数字x
- 以target-x作为key到map中查找
-
- 若没找到,将x 作为key,它的索引作为value 存入map
-
- 若找到了,返回 x 和它配对数的索引即可
-
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int x = nums[i];
int y = target-x;
if (map.containsKey(y)) {
return new int[]{i, map.get(y)};
} else {
map.put(x, i);
}
}
return null;
}
}
2. 无重复字符的最长字串-力扣 3 题
思路:滑动窗口解决:把握一个重点:end每次都+1,start需要调整,怎么更新start是关键
- 定义一个map,key为字符,value为其索引+1,表示从字符位置后一个位置才开始不重复
- 定义begin和end表示不重复字串的开始和结束,依次遍历字符串,伴随着end+1的过程,每次都将遍历到的字符存入或者更新到map中
- 在遍历过程中,会遇到与 [start, end] 区间内字符相同的情况,此时将字符作为 key 值,获取其 value 值,并更新 start,此时 [start, end] 区间内不存在重复字符。注意更新start时:判断当前字符是不是之前的即当前区间内没有的字符,如果是的话保持start不动,例如abba,此时start为第二个b,end为最后的a,此时map存的a的value为1,map存的是之前的,当前区间没有a,所以start不能倒回去,要保持start不动
- 注意:map中最后存的是字符串中去重后的字符
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length(), maxLenth = 0;
Map<Character, Integer> map = new HashMap<>();
for(int start = 0, end = 0; end < n; end++) {
char alpha = s.charAt(end);
if (map.containsKey(alpha)) { // 重复
start = Math.max(start, map.get(alpha));
}
// 每次都将end更新或者写入到map中,map中存的是所有不重复字符,当然还包括当前区
// 间没有的字符,所以在更新start时候要max,目的是:如果是当前区间没有的,表示之前的,
// 此时 保持start不动
maxLenth = Math.max(maxLenth, end-start+1);
map.put(s.charAt(end), end + 1);
}
return maxLenth;
}
}
3. 字母异位词分组-力扣 49 题
思路:
- 遍历字符串数组,每个字符串中的字符重新排序后作为 key
- 所谓分组,就是准备一个集合,把这些单词加入到对应的 key 相同的集合中
- 返回分组的结果
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> map = new HashMap<>();
for (String str : strs) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
String key = new String(chars);
List<String> list = map.get(key);
if (list == null) {
list = new ArrayList<>();
map.put(key, list);
}
list.add(str);
}
return new ArrayList<>(map.values());
}
}
优化
:
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> map = new HashMap<>();
for (String str : strs) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
String key = new String(chars);
List<String> list = map.computeIfAbsent(key, k -> new ArrayList<>());
list.add(str);
}
return new ArrayList<>(map.values());
}
}
4. 判断有没有重复元素-力扣 217
思路:HashSet解决,遍历数组,将数组元素放入set,如果操作成功表示没有重复,如果操作失败表示此时这个元素重复了,return返回true
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int key : nums) {
if (!set.add(key)) {
return true;
}
}
return false;
}
}
5. 找出出现一次的数字-力扣 136(找出没有重复的数字)
思路1:这道题有个前提,重复元素只出现两次,是成对存在的,用set求解
遍历数字,依次将每个元素放入set中,当放入成功时表示现在set中还没有,当放入失败时表示是重复元素,删掉set中这个元素,最后set中就只留下不重复的元素了。但是不符合题目要求:题目要求空间复杂度O(1)
思路2:读题目,返回只有一个数,其他数都是成对存在的,用异或运算
异或结论:
- 异或运算满足交换律,a ^ b ^ a =a ^ a ^ b=b
- 任何相同的数字异或,结果都是0
- 任何数字与 0 异或,结果是数字本身
思路1
class Solution {
public int singleNumber(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
if (!set.add(num)) {
set.remove(num);
}
}
return set.toArray(new Integer[0])[0]; //new Integer[0] 在这里的作用是为了提供一个空的
//Integer 数组,以便 toArray 方法知道我们想要得到的数组类型,并相应地创建并返回这个类型的数组。
}
}
思路2
class Solution {
public int singleNumber(int[] nums) {
int num = nums[0];
for (int i =1; i < nums.length; i++) {
num = num ^ nums[i];
}
return num;
}
}
6. 判断字母异位词-力扣 242
思路:字母异位词有个特性:排序后一样。将两个string转换为数组,排序,然后比较,看看一不一样。
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
char[] s1 = s.toCharArray();
char[] s2 = t.toCharArray();
Arrays.sort(s1);
Arrays.sort(s2);
for (int i =0; i < s1.length; i++) {
if (s1[i] != s2[i]) {
return false;
}
}
return true;
}
}
7. 第一个不重复字符-力扣 387
思路:创建一个map,依次遍历字符串中的每个字符,将每个字符存入map中,value为该字符出现的次数,最后map中就统计了所有字符出现的次数,要找到map中value为1的字符,可能有多个,题目要求返回字符串第一个不重复的索引,再遍历一次字符串,找到第一个字符(该字符在map中value为1)
优化
:题目中string只包含小写字母,对map优化为26长度的数组。
class Solution {
public int firstUniqChar(String s) {
int[] array = new int[26];
char[] chars = s.toCharArray();
for (char ch : chars) {
array[ch - 97]++;
}
for (int i = 0; i < chars.length; i++) {
char ch = chars[i];
if(array[ch - 97] == 1) {
return i;
}
}
return -1;
}
}