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

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

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

题目难度:中等

代码:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        Set set=new HashSet<>();
        int n=s.length();
        int r=-1,res=0;
        for(int i=0;i

12. 整数转罗马数字

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

题目难度:中等

代码:

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();
    }
}

49. 字母异位词分组

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

题目难度:中等

代码:

class Solution {
    public List> groupAnagrams(String[] strs) {
        Map> map=new HashMap<>();
        for(String str:strs){
            int[] counts=new int[26];
            int length=str.length();
            for(int i=0;i list=map.getOrDefault(key,new ArrayList());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList>(map.values());
    }
}

73. 矩阵置零

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

题目难度:中等

代码:

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;
            }
        }
    }
}

你可能感兴趣的:(哈希表,算法)