355-II56-II1512

Posted 保护眼睛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了355-II56-II1512相关的知识,希望对你有一定的参考价值。

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

class Solution3 {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> hashMap = new HashMap<>();
        int res = 0;
        int left = 0;
        int len = s.length();
        for (int i = 0; i < len; i++) {
            if (hashMap.containsKey(s.charAt(i))) {
                left = Math.max(left, hashMap.get(s.charAt(i)) + 1);
            }

            hashMap.put(s.charAt(i), i);
            res = Math.max(res, i - left + 1);
        }

        return res;
    }

    public int lengthOfLongestSubstring2(String s) {
        HashSet<Character> hashSet = new HashSet<>();
        int len = s.length();
        int res = 0;
        int left = -1;
        for (int i = 0; i < len; i++) {
            if (i != 0) {
                hashSet.remove(s.charAt(i - 1));
            }
            while (left + 1 < len && !hashSet.contains(s.charAt(left + 1))) {
                hashSet.add(s.charAt(left + 1));
                left++;
            }
            res = Math.max(res, left - i + 1);
        }

        return res;
    }
}

剑指 Offer 55 - II. 平衡二叉树

class Solution55_II {
    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        if (Math.abs(getHOfTree(root.left) - getHOfTree(root.right)) > 1) return false;
        return isBalanced(root.left) && isBalanced(root.right);
    }

    public int getHOfTree(TreeNode root) {
        if (root == null) return 0;
        return Math.max(getHOfTree(root.left), getHOfTree(root.right)) + 1;
    }
}

剑指 Offer 56 - II. 数组中数字出现的次数 II

class Solution56_II {

    public int singleNumber(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1)
                return entry.getKey();
        }
        return -1;
    }

}

剑指 Offer 15. 二进制中1的个数

class Solution15 {
    public int hammingWeight(int n) {
        int ret = 0;
        for (int i = 0; i < 32; i++) {
            if ((n & (1 << i)) != 0) {
                ret++;
            }
        }
        return ret;
    }

    public int hammingWeight2(int n) {
        int res = 0;
        for (int i = 0; i < 32; i++) {
            if ((n & 1) == 1) {
                res++;
            }
            n >>= 1;
        }
        return res;
    }
}

剑指 Offer 12. 矩阵中的路径

class Solution12 {
    public boolean exist(char[][] board, String word) {
        char[] words = word.toCharArray();
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[0].length; j++) {
                if(dfs(board,i,j,0,words)) return true;
            }
        }
        return false;

    }

    public boolean dfs(char[][] board, int i, int j, int k, char[] words) {
        if (i >= board.length || j >= board[0].length || i < 0 || j < 0 || board[i][j] != words[k]) return false;
        if (k == words.length - 1) return true;

        board[i][j] = '\\0';

        boolean res = dfs(board, i - 1, j, k+1, words) || dfs(board, i + 1, j, k+1, words)
                || dfs(board, i, j - 1, k+1, words) || dfs(board, i, j + 1, k+1, words);

        board[i][j] = words[k];

        return res;
    }
}

以上是关于355-II56-II1512的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode(剑指 Offer)- 56 - II. 数组中数字出现的次数 II

剑指Offer面试题56 - II. 数组中数字出现的次数 II

剑指offer56-II 数组中数字出现的次数

剑指offer56-II 数组中数字出现的次数

剑指 Offer 56 - II. 数组中数字出现的次数 II

剑指 Offer 56 - I 至 56 - II 数组中数字出现的次数 I && II 题解