力扣热题100——一刷day01

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


一、力扣1. 两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        for(int i = 0; i < nums.length; i ++){
            map.put(nums[i], i);
        }
        for(int i = 0; i <nums.length; i ++){
            if(map.containsKey(target-nums[i])){
                int index = map.get(target-nums[i]);
                if(index != i){
                    res[0] = i;
                    res[1] = index;
                    break;
                }
            }
        }
        return res;
    }
}

二、力扣49. 字母异位词分组

寻找字母易位词的共同点作为哈希表的key

第一种情况对每一个字符串排序,排序结果作为key

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<>();
        for(String str :strs){
            char[] ch = str.toCharArray();
            Arrays.sort(ch);
            String key = new String(ch);
            List<String> value = map.getOrDefault(key,new ArrayList<>());
            value.add(str);
            map.put(key,value);
        }
        return new ArrayList<List<String>>(map.values());
    }
}

第二种对每一个字符串中字母出现的次数计数

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for(String str : strs){
            int[] count = new int[26];
            for(int i = 0; i < str.length(); i ++){
                count[str.charAt(i)-'a'] ++;
            }
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < 26; i ++){
                if(count[i] != 0){
                    sb.append('a' + i);
                    sb.append(count[i]);
                }
            }
            String key = sb.toString();
            List<String> list = map.getOrDefault(key, new ArrayList<>());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}

三、力扣128. 最长连续序列

把数组中的所有数字都放进set中,遍历数组,判断每一个数在set中是否存在,下一个元素,存在说明连续,便让本元素的计数器++,不存在时,取本计数器和res的最大值,其中有一个去重操作,遍历过的元素不要再遍历了,在进入寻找下一个元素之前,先判断set中是否有其前一个元素,若有,说明本元素已被遍历过了,跳过即可,没有,则进入寻找下一个元素的环节

class Solution {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        int res = 0;
        for(int a : nums){
            set.add(a);
        }
        for(int num : set){
            if(!set.contains(num-1)){
                int cur = num;
                int count = 1;
                while(set.contains(cur+1)){
                    cur ++;
                    count ++;
                }
                res = Math.max(res,count);
            }
        }
        return res;
    }
}

四、力扣283. 移动零

class Solution {
    public void moveZeroes(int[] nums) {
        int n = nums.length;
        for(int i = 0; i < n;){
            while(i < n && nums[i] != 0){
                i ++;
            }
            if(i == n)return;
            int j = i + 1;
            while(j < n && nums[j] == 0){
                j ++;
            }
            if(j == n)return;
            nums[i] = nums[j];
            nums[j] = 0;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值