Day51 | 3. 无重复字符的最长子串、12. 整数转罗马数字、49. 字母异位词分组、73. 矩阵置零

  1. 无重复字符的最长子串

题目链接:3. 无重复字符的最长子串 - 力扣(LeetCode)

题目难度:中等

代码:

java 复制代码
class Solution {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> set=new HashSet<>();
        int n=s.length();
        int r=-1,res=0;
        for(int i=0;i<n;i++){
            if(i!=0)
                set.remove(s.charAt(i-1));
            while(r+1<n&&!set.contains(s.charAt(r+1)))
                set.add(s.charAt(++r));
            res=Math.max(res,r-i+1);
        }
        return res;
    }
}
  1. 整数转罗马数字

题目链接:12. 整数转罗马数字 - 力扣(LeetCode)

题目难度:中等

代码:

java 复制代码
class Solution {
    String[] thousands = {"", "M", "MM", "MMM"};
    String[] hundreds  = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    String[] tens      = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    String[] ones      = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
    public String intToRoman(int num) {
        StringBuffer roman = new StringBuffer();
        roman.append(thousands[num / 1000]);
        roman.append(hundreds[num % 1000 / 100]);
        roman.append(tens[num % 100 / 10]);
        roman.append(ones[num % 10]);
        return roman.toString();
    }
}
  1. 字母异位词分组

题目链接:49. 字母异位词分组 - 力扣(LeetCode)

题目难度:中等

代码:

java 复制代码
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map=new HashMap<>();
        for(String str:strs){
            int[] counts=new int[26];
            int length=str.length();
            for(int i=0;i<length;i++){
                counts[str.charAt(i)-'a']++;
            }
            StringBuffer sb=new StringBuffer();
            for(int i=0;i<26;i++){
                if(counts[i]!=0){
                    sb.append((char)('a'+i));
                    sb.append(counts[i]);
                }
            }
            String key=sb.toString();
            List<String> list=map.getOrDefault(key,new ArrayList<String>());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}
  1. 矩阵置零

题目链接:73. 矩阵置零 - 力扣(LeetCode)

题目难度:中等

代码:

java 复制代码
class Solution {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        boolean flagCol0 = false;
        for (int i = 0; i < m; i++) {
            if (matrix[i][0] == 0) {
                flagCol0 = true;
            }
            for (int j = 1; j < n; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = matrix[0][j] = 0;
                }
            }
        }
        for (int i = m - 1; i >= 0; i--) {
            for (int j = 1; j < n; j++) {
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
            if (flagCol0) {
                matrix[i][0] = 0;
            }
        }
    }
}
相关推荐
理想奋斗中1 小时前
【LeetCode Hot100 | 每日刷题】二叉树的层序遍历
算法·leetcode·bfs
JCBP_2 小时前
C++(1)
开发语言·c++·算法
数据与人工智能律师2 小时前
互联网法院在NFT、元宇宙等新兴领域的规则创新
大数据·网络·人工智能·算法·区块链
知识漫步3 小时前
代码随想录算法训练营第60期第二十八天打卡
算法
chao_7893 小时前
手撕算法(1)
算法
zhangxueyi3 小时前
Java实现堆排序算法
java·数据结构·算法
C灿灿数模3 小时前
2025五一杯数学建模A题:支路车流量推测问题,思路分析+模型代码
大数据·人工智能·算法
Non importa4 小时前
【初阶数据结构】树——二叉树——堆(中)
java·c语言·数据结构·学习·算法
代码程序猿RIP5 小时前
【C语言干货】野指针
c语言·开发语言·数据结构·c++·算法
jz_ddk5 小时前
[学习]RTKLib详解:rtkcmn.c与rtkpos.c
c语言·学习·算法