算法刷题打卡(十二)

78 子集

78. 子集 - 力扣(LeetCode) (leetcode-cn.com)

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
示例 2:

输入:nums = [0]
输出:[[],[0]]

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> befor = new ArrayList<>();
	    process(nums, 0, befor, ans);
		return ans;
    }
    //当前位置 之前的答案 返回
    public static void process(int nums[], int index, List<Integer> befor, List<List<Integer>> ans){
        if(index == nums.length){
            ans.add(new ArrayList<>(befor));
        }else {
			process(nums, index + 1, befor, ans);
			befor.add(nums[index]);
			process(nums, index + 1, befor, ans);
			befor.remove(befor.size()-1);
		}
    }

79 单词搜索

79. 单词搜索 - 力扣(LeetCode) (leetcode-cn.com)

给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

示例 1:

img

输入:board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
输出:true
示例 2:

img

输入:board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “SEE”
输出:true
示例 3:

img

输入:board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCB”
输出:false

    public boolean exist(char[][] board, String word) {
        char[] w = word.toCharArray();
        for(int i = 0; i < board.length; i++){
            for(int j =0;j < board[0].length; j++){
                if(process(board,i,j,w,0)){
                    return true;
                }
            }
        }
        return false;
    }
    //board 从 ij 位置 开始 试   搞定 word[k...]   不能走回头路
    public static boolean process(char[][] b, int i, int j, char[] w, int k) {
        if(k == w.length){
            return true;
        }
        if(i < 0 || j < 0 || i == b.length || j ==b[0].length){
            return false;
        }
        if(b[i][j] != w[k]){
            return false;
        }
        char temp = b[i][j];
        b[i][j] = 0;
        boolean ans =  process(b, i - 1, j, w, k + 1) //上下左右
				    || process(b, i + 1, j, w, k + 1) 
				    || process(b, i, j - 1, w, k + 1)
				    || process(b, i, j + 1, w, k + 1);
        b[i][j] = temp;
        return ans;            
    }

84 住状图种最大的矩形

84. 柱状图中最大的矩形 - 力扣(LeetCode) (leetcode-cn.com)

给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

求在该柱状图中,能够勾勒出来的矩形的最大面积。

示例 1:

img

输入:heights = [2,1,5,6,2,3]
输出:10
解释:最大的矩形为图中红色区域,面积为 10
示例 2:

img

输入: heights = [2,4]
输出: 4

//单调栈 当前位置 左右离我最近 比我小的
    public int largestRectangleArea(int[] heights) {
        if(heights.length == 0){
            return 0;
        }
        int maxArea = 0;
        Stack<Integer> stack = new Stack<>();
        for(int i = 0; i < heights.length; i++){//遍历数组
        //栈不为空 当前值 <= 栈顶的值  进行结算
            while(!stack.isEmpty() && heights[i] <= heights[stack.peek()]){
                int j = stack.pop();
                int s = stack.isEmpty() ? -1 : stack.peek();
                maxArea = Math.max((i - s -1) * heights[j],maxArea);
            }
            stack.push(i);
        }
        while(!stack.isEmpty()){//栈中 可能还有值
            int j = stack.pop();
            int s = stack.isEmpty() ? -1 : stack.peek();
            maxArea = Math.max((heights.length - s -1) * heights[j],maxArea);
         }
        return maxArea;
    }

88 合并两个有序数组

88. 合并两个有序数组 - 力扣(LeetCode) (leetcode-cn.com)

给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。

请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。

注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。

示例 1:

输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出:[1,2,2,3,5,6]
解释:需要合并 [1,2,3] 和 [2,5,6] 。
合并结果是 [1,2,2,3,5,6] ,其中斜体加粗标注的为 nums1 中的元素。
示例 2:

输入:nums1 = [1], m = 1, nums2 = [], n = 0
输出:[1]
解释:需要合并 [1] 和 [] 。
合并结果是 [1] 。
示例 3:

输入:nums1 = [0], m = 0, nums2 = [1], n = 1
输出:[1]
解释:需要合并的数组是 [] 和 [1] 。
合并结果是 [1] 。
注意,因为 m = 0 ,所以 nums1 中没有元素。nums1 中仅存的 0 仅仅是为了确保合并结果可以顺利存放到 nums1 中。

    //让把 nums2 的填进 nums1 , 所以比较大小 从最后一个开始填, 一个没了 另外直接都填进去
	public void merge(int[] nums1, int m, int[] nums2, int n) {
        int index = nums1.length - 1;
        while(m > 0 && n > 0){
            if(nums1[m-1] >= nums2[n-1]){
                nums1[index--] = nums1[--m];
            }else{
                nums1[index--] = nums2[--n];
            }
        }
        while(m > 0){
            nums1[index--] = nums1[--m];
        }
        while(n > 0){
            nums1[index--] = nums2[--n];
        }
    }

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值