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;
            }
        }
    }
}
相关推荐
汉克老师5 小时前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(5、机甲战士)
c++·算法·蓝桥杯·01背包·蓝桥杯c++·c++蓝桥杯
Mr_Xuhhh6 小时前
项目需求分析(2)
c++·算法·leetcode·log4j
c++bug7 小时前
六级第一关——下楼梯
算法
Morri37 小时前
[Java恶补day53] 45. 跳跃游戏Ⅱ
java·算法·leetcode
林木辛7 小时前
LeetCode热题 15.三数之和(双指针)
算法·leetcode·双指针
AndrewHZ7 小时前
【3D算法技术】blender中,在曲面上如何进行贴图?
算法·3d·blender·贴图·三维建模·三维重建·pcg
Jared_devin8 小时前
二叉树算法题—— [蓝桥杯 2019 省 AB] 完全二叉树的权值
数据结构·c++·算法·职场和发展·蓝桥杯
AI 嗯啦9 小时前
数据结构深度解析:二叉树的基本原理
数据结构·算法
和光同尘@10 小时前
66. 加一 (编程基础0到1)(Leetcode)
数据结构·人工智能·算法·leetcode·职场和发展
CHEN5_0210 小时前
leetcode-hot100 11.盛水最多容器
java·算法·leetcode