1. 两数之和:https://leetcode-cn.com/problems/two-sum/

    class Solution {
     public int[] twoSum(int[] nums, int target) {
         Map<Integer,Integer> map = new HashMap<>();
         for(int i=0;i<nums.length;i++){
             //target - nums[i]
             int sum = target - nums[i];
             //如果map中有sum,返回索引
             if(map.containsKey(sum)){
                 return new int[]{i,map.get(sum)};
             }
             map.put(nums[i],i);
         }
         return new int[0];
     }
    }
  2. 无重复最长子串:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/submissions/

    class Solution {
     public int lengthOfLongestSubstring(String s) {
         Map<Character,Integer> map = new HashMap<>();
         int left = 0;
         char[] str = s.toCharArray();
         int result = 0;
    
         for(int right=0;right<str.length;right++){
             //如果map中有str[right],更新左边界
             if(map.containsKey(str[right])){
                 left = Math.max(left,map.get(str[right])+1);
             }
             map.put(str[right],right);
             result = Math.max(result,right-left+1);
         }
         return result;
     }
    }
  3. 最长连续序列:https://leetcode-cn.com/problems/longest-consecutive-sequence/

    class Solution {
     public int longestConsecutive(int[] nums) {
         Set<Integer> set = new HashSet<>();
         for(int i : nums){
             set.add(i);
         }
    
         int result = 0;
         //遍历
         for(int i=0;i<nums.length;i++){
             int cur = nums[i];
             int count = 1;
             //如果set中没有cur-1,可能出现连续,while循环判断cur+1
             if(!set.contains(cur-1)){
                 while(set.contains(cur+1)){
                     cur++;
                     count++;
                 }
             }
             result = Math.max(result,count);
         }
         return result;
     }
    }
  1. 字母异位分组:https://leetcode-cn.com/problems/group-anagrams/

    class Solution {
     public List<List<String>> groupAnagrams(String[] strs) {
         Map<String,List<String>> map = new HashMap<>();
         List<List<String>> result = new ArrayList<>();
    
         for(int i=0;i<strs.length;i++){
             String temp = strs[i];
             //转化为字符数组,排序
             char[] cstr = temp.toCharArray();
             Arrays.sort(cstr);
             //转回
             String key = String.valueOf(cstr);
             //判断map中是否有key,如果有,说明是同组,存入map。否则新开一行存储
             if(map.containsKey(key)){
                 map.get(key).add(temp);
             }else{
                 List<String> list = new ArrayList<>();
                 //存入temp字符串
                 list.add(temp);
                 //存入key和list
                 map.put(key,list);
                 //存入list
                 result.add(list);
             }
         }
         return result;
     }
    }
  2. 第一个缺失的正数:https://leetcode-cn.com/problems/first-missing-positive/

    class Solution {
     public int firstMissingPositive(int[] nums) {
         Set<Integer> set = new HashSet<>();
         for(int i : nums){
             set.add(i);
         }
    
         //第一种情况:在数组中间缺失
         for(int i=1;i<=nums.length;i++){
             if(!set.contains(i)){
                 return i;
             }
         }
    
         //第二种情况:在数组尾部缺失
         return nums.length+1;
     }
    }