码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 力扣热题100——一刷day01


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

    文章目录

    • 前言
    • 一、力扣1. 两数之和
    • 二、力扣49. 字母异位词分组
    • 三、力扣128. 最长连续序列
    • 四、力扣283. 移动零


    前言


    一、力扣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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    二、力扣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());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

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

    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());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    三、力扣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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    四、力扣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;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    您的连接不是私密连接 攻击者可能会试图从 github.com 窃取您的信息(例如:密码、通讯内容或信用卡信息)
    RBCM-PDA-CUR@PLGA红细胞膜包裹聚多巴胺涂覆PLGA/细胞膜包覆纳米拓扑结构阵列
    c++运算符重载的几个例子记录
    说说Elasticsearch索引文档的过程
    机器人能否返回原点
    基于Java毕业设计新纪元大酒店管理系统源码+系统+mysql+lw文档+部署软件
    [激光器原理与应用-13]: 2022年中国激光行业产业链全景梳理
    Git创建仓库、并同步本地项目到远程
    基于SVM的功率分类,基于支持向量机SVM的功率分类识别,Libsvm工具箱详解
    聊聊零拷贝技术原理和应用
  • 原文地址:https://blog.csdn.net/ResNet156/article/details/133904375
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号