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]; } }