LeetCode 热题100道-Day07

74 阅读1分钟

LeetCode 热题100道-Day07

接雨水

  • 利用栈的特性进行判断是否积水,并记录积了多少个方块的水
class Solution {
    public int trap(int[] height) {
        int ans = 0;
        Deque<Integer> stack = new LinkedList<Integer>();
        int n = height.length, top = 0, left = 0, currWidth = 0, currHeight = 0;
        for (int i = 0; i < n; ++i) {
            while (!stack.isEmpty() && height[i] > height[stack.peek()]) {
                top = stack.pop();
                if (stack.isEmpty()) {
                    break;
                }
                left = stack.peek();
                currWidth = i - left - 1;
                currHeight = Math.min(height[left], height[i]) - height[top];
                ans += currWidth * currHeight;
            }
            stack.push(i);
        }
        return ans;
    }
}

全排列

  • 利用递归不断的创造排列
class Solution {
    public List<List<Integer>> permute(int[] nums) {
        // 定义存放排序的集合
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        
        // 定义存放不同排列的集合
        List<Integer> output = new ArrayList<Integer>();
        // 对集合添加数据
        for (int num : nums) {
            output.add(num);
        }

        // 定义循环次数
        int n = nums.length;
        // 进行排序处理
        backtrack(n, output, res, 0);
        // 返回集合
        return res;
    }

    public void backtrack(int n, List<Integer> output, List<List<Integer>> res, int first) {
        // 所有数都填完了
        if (first == n) {
            res.add(new ArrayList<Integer>(output));
        }
        for (int i = first; i < n; i++) {
            // 动态维护数组
            Collections.swap(output, first, i);
            // 继续递归填下一个数
            backtrack(n, output, res, first + 1);
            // 撤销操作
            Collections.swap(output, first, i);
        }
    }
}

旋转图像

  • 利用扩大目标新数组,进行重新填充
class Solution {
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        int[][] matrix_new = new int[n][n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                matrix_new[j][n - i - 1] = matrix[i][j];
            }
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                matrix[i][j] = matrix_new[i][j];
            }
        }
    }
}

字母异位词分组

  • 利用排序,再将排序后的字符进行 hash 处理
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        for (String str : strs) {
            char[] array = str.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            List<String> list = map.getOrDefault(key, new ArrayList<String>());
            list.add(str);
            map.put(key, list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}

最大子序和

  • 利用循环和 max 方法不断的判断子序列
class Solution {
    public int maxSubArray(int[] nums) {
        int pre = 0, maxAns = nums[0];
        for (int x : nums) {
            pre = Math.max(pre + x, x);
            maxAns = Math.max(maxAns, pre);
        }
        return maxAns;
    }
}