【LEETCODE】59、數組分類,適中級別,題目:39、48、64

package y2019.Algorithm.array.medium;

import java.util.*;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: CombinationSum
 * @Author: xiaof
 * @Description: TODO 39. Combination Sum
 * Given a set of candidate numbers (candidates) (without duplicates) and a target number (target),
 * find all unique combinations in candidates where the candidate numbers sums to target.
 * The same repeated number may be chosen from candidates unlimited number of times.
 *
 * Input: candidates = [2,3,6,7], target = 7,
 * A solution set is:
 * [
 *   [7],
 *   [2,2,3]
 * ]
 *
 * @Date: 2019/7/18 9:02
 * @Version: 1.0
 */
public class CombinationSum {

    public List<List<Integer>> solution(int[] candidates, int target) {
        //這個題類似探索類,就是不斷探索下一個元素是那個,那麼我們就用遞歸做吧
        //爲了避免遞歸的時候,吧上一層用過的遞歸數據再次使用,這裏可以用排序,因爲還可以放同一元素,所以下一層開始的位置可以是當前位置
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(candidates);
        findNum(res, new ArrayList<>(), candidates, target, 0);
        return res;
    }

    public void findNum(List<List<Integer>> res, List<Integer> curList, int[] nums, int target, int start) {
        if(target < 0) {
            //遞歸過深
            return;
        } else if(target == 0) {
            //成功,遞歸到最後一層了
            res.add(new ArrayList<>(curList));
        } else {
            //正常數據獲取
            for(int i = start; i < nums.length; ++i) {
                //依次獲取數據,因爲是從小的往大的數據遞歸,那麼比start位置小的已經遞歸過了
                curList.add(nums[i]);
                //遞歸進入下一層
                findNum(res, curList, nums, target - nums[i], i);
                //但是遞歸下層完畢,獲取下一個數據的時候,我們把末尾的數據去掉
                curList.remove(curList.size() - 1); //因爲有<0的情況,所以不一定就是當前的這個數據

            }
        }
    }

    public static void main(String[] args) {
        int data[] = {9,6,8,11,5,4};
        int n = 34;
        CombinationSum fuc = new CombinationSum();
        System.out.println(fuc.solution(data, n));
        System.out.println();
    }


}

 

package y2019.Algorithm.array.medium;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: Rotate
 * @Author: xiaof
 * @Description: TODO 48. Rotate Image
 * You are given an n x n 2D matrix representing an image.
 * Rotate the image by 90 degrees (clockwise).
 *
 * 給定一個 n × n 的二維矩陣表示一個圖像。
 * 將圖像順時針旋轉 90 度。
 *
 * Given input matrix =
 * [
 *   [1,2,3],
 *   [4,5,6],
 *   [7,8,9]
 * ],
 *
 * rotate the input matrix in-place such that it becomes:
 * [
 *   [7,4,1],
 *   [8,5,2],
 *   [9,6,3]
 * ]
 *
 * 學習大神操作:https://leetcode-cn.com/problems/rotate-image/solution/yi-ci-xing-jiao-huan-by-powcai/
 *
 * 像旋轉數組的話,如果是90°旋轉
 *
 * 1 2 3        7 4 1
 * 4 5 6  =》     8 5 2
 * 7 8 9     9 6 3
 * 計算公式:
 * (i, j)            ———————————->  (j, n - i - 1)
 *   ↑                                     |
 *   |                                     ↓
 * (n - j -1, i)     <———————————-  (n - i - 1, n - j - 1)
 *
 * @Date: 2019/7/18 10:00
 * @Version: 1.0
 */
public class Rotate {

    public void solution(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n/2; i++ ) {
            for (int j = i; j < n - i - 1; j ++ ){
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[n-j-1][i];
                matrix[n-j-1][i] = matrix[n-i-1][n-j-1];
                matrix[n-i-1][n-j-1] = matrix[j][n-i-1];
                matrix[j][n-i-1] = tmp;
            }
        }
    }

    public static void main(String[] args) {
        int data[][] = { {1,2,3}, {4,5,6}, {7,8,9}};
        Rotate fuc = new Rotate();
        fuc.solution(data);
        System.out.println();
    }
}

 

package y2019.Algorithm.array.medium;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: MinPathSum
 * @Author: xiaof
 * @Description: TODO 64. Minimum Path Sum
 *
 * Given a m x n grid filled with non-negative numbers,
 * find a path from top left to bottom right which minimizes the sum of all numbers along its path.
 *
 * Input:
 * [
 *   [1,3,1],
 *   [1,5,1],
 *   [4,2,1]
 * ]
 * Output: 7
 * Explanation: Because the path 1→3→1→1→1 minimizes the sum
 *
 * 說明:每次只能向下或者向右移動一步。
 *
 * 明顯就是動態規劃了
 * a{i,j} = min{a{i-1, j}, a{i, j - 1}}
 *
 * @Date: 2019/7/18 10:31
 * @Version: 1.0
 */
public class MinPathSum {

    public int solution(int[][] grid) {
        if(grid == null || grid.length <= 0 || grid[0].length <= 0) {
            return -1;
        }
        //動態規劃數組
        int[][] res = new int[grid.length][grid[0].length];
        int r = grid.length, c = grid[0].length;
        res[0][0] = grid[0][0];
        //初始化第一條路,最開始的橫向與縱向
        for(int i = 1; i < c; ++i) {
            res[0][i] = grid[0][i] + res[0][i - 1];
        }
        for(int j = 1; j < r; ++j) {
            res[j][0] = res[j - 1][0] + grid[j][0];
        }

        //開始線性規劃
        for(int i = 1; i < r; ++i) {
            for(int j = 1; j < c; ++j) {
                res[i][j] = Math.min(res[i - 1][j], res[i][j - 1]) + grid[i][j];
            }
        }

        return res[r - 1][c - 1];

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章