leecode编写记录

记录leecode刷题思路

39.组合总数

39. 组合总和 - 力扣(LeetCode)

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:

输入: candidates = [2], target = 1
输出: []

思路如下:此题为标准的回溯法解决的问题

  • 将数组,大集合(存储每个可能结果),小集合(存储每个可能数目),目标值,位置。
  • i= 0开始,小集合放入当期数组中的值,并用目标值减去数组中的值
  • 如果目标值小于0说明该不行,小集合退出该值
  • 如果为0则将小集合放入大集合,但是这是有个问题,必须负责小集合的值存入,小集合中的值改变了,大集合中存入小集合的值也会变
  • i<数组长度 继续进行,否则结束
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        List<Integer> temp =  new ArrayList<Integer>();
        Arrays.sort(candidates);
        find(candidates,target,list,temp,0);
        return list;
    }
    public void find(int[] candidates , int target, List<List<Integer>> list, List<Integer> temp, int num){
         if(target< 0) return ;
         if(target == 0){
            list.add(new ArrayList<>(temp));
            return ;
         }
         for(int i = num; i< candidates.length ;i++){
             if(target<0) break;
             temp.add(candidates[i]);
             find(candidates,target-candidates[i],list,temp,i);
             temp.remove(temp.size()-1);
         }
    }
}

42.接雨水

42. 接雨水 - 力扣(LeetCode)

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

示例 1:

img

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:

输入:height = [4,2,0,3,2,5]
输出:9

思路如下:对于此题来说理解思路其实和简单,对于能接多少水其实是每个点接水面积的和,对于i点来说来说能接水的面积为Math.min(height(i左侧全部),height(i右侧全部))i点的长度1。思路转换:

  • 暴力破解:

    • 对于每个点都需要求取左侧最大与右侧最大,这样势必会造成浪费,因此这种有重复值计算的方法很容易想到动态规划的方式。
  • 双向链表:

    • 定义一个左侧最大、左侧点位置、右侧最大、右侧点位置,那么双向链表只需要对两侧判断之前的最大值并且不断更新即可。
class Solution {
    public int trap(int[] height) {
        int count = 0 ;
        int left = 0;
        int right = height.length-1;
        int leftHeight = height[left];
        int rightHeight = height[right];
        while(left<=right){
            if(leftHeight<rightHeight){
                leftHeight =  Math.max(leftHeight,height[left]);
                count = count + Math.max(leftHeight-height[left],0);
                left++;
            }else{
                rightHeight = Math.max(rightHeight,height[right]);
                count = count + Math.max(rightHeight-height[right],0);
                right--;
            }
        }
        return count;

    }
}
  • 找到最高点,分别从两端往最高点走
    • 走左边的时候定义一个左侧相对最大temp,如果遍历左侧的点大于temp,那么修改temp的值,如果小于则表示可以存水,temp-height(i)
    • 右侧同理。

46.全排列

给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

示例 1:

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

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

输入:nums = [1]
输出:[[1]]

思路如下:这类题目都是同一类型的,用回溯算法!其实回溯算法关键在于:不合适就退回上一步然后通过约束条件, 减少时间复杂度.

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list =  new ArrayList();
        List<Integer> tempList = new ArrayList();
        getAll(nums,list,tempList);
        return list;
    }   
    public void getAll(int[] nums,List<List<Integer>> list,List<Integer> tempList){
        if(tempList.size() == nums.length){
            list.add(new ArrayList(tempList));
            return ;
        }
        for(int i =0 ;i < nums.length;i++){
            boolean isConrains = tempList.contains(nums[i]);
            if(!isConrains){
                tempList.add(nums[i]);
                getAll(nums,list,tempList);
                tempList.remove(tempList.size()-1);
            }
        }
        return;
    }
}

48.旋转图像

给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[7,4,1],[8,5,2],[9,6,3]]
示例 2:

输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

48. 旋转图像

class Solution {
    public void rotate(int[][] matrix) {
        int[][] b = new int[matrix.length][matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            int[] a = matrix[i];
            for (int j = 0 ; j < a.length ; j++){
                b[i][j] = matrix[i][j];
            }
        }
        for(int i=0;i<b.length;i++){
            int[] a  = b[i];
            for (int j=0;j<a.length;j++){
                matrix[i][j] = b[a.length-j-1][i];
            }

        }
    }
}
posted @ 2022-06-20 08:47  zqy123  阅读(8)  评论(0编辑  收藏  举报