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

【LEETCODE】59、数组分类,适中级别,题目:39、48、64的更多相关文章

  1. LeetCode:颜色分类【75】

    LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...

  2. LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)

    这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...

  3. LeetCode:数组中的第K个最大元素【215】

    LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...

  4. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  5. LeetCode一维数组的动态和

    一维数组的动态和 题目描述 给你一个数组 nums.数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]...nums[i]). 请返回 nums 的动态和. 示例 1: ...

  6. 【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73

    这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么??? 这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里??? 最近一直苦苦思考,不明所以,刷题刷得更多的感 ...

  7. 【LEETCODE】60、数组分类,适中级别,题目:75、560、105

    package y2019.Algorithm.array.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.a ...

  8. 【LEETCODE】58、数组分类,适中级别,题目:238、78、287

    package y2019.Algorithm.array.medium; import java.util.Arrays; /** * @ProjectName: cutter-point * @P ...

  9. 【LEETCODE】57、数组分类,适中级别,题目:969、442、695

    package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...

随机推荐

  1. 集成omnibus-ctl+ chef 制作一个可配置的软件包

    前边有写过使用omnibus-ctl 制作软件包的,但是当时没有集成chef,只有一个空壳子,实际上omnibus-ctl 已经内置 了对于chef 的操作(但是我们还需要在添加一个依赖),以下简单说 ...

  2. 开源项目 07 AutoMapper

    using AutoMapper; using Newtonsoft.Json; using System; using System.Collections.Generic; using Syste ...

  3. Poj 2411 Mondriaan's Dream(状压DP)

    Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Description Squares and rectangles fascina ...

  4. Hyperspectral Images Classification Based on Dense Convolutional Networks with Spectral-Wise Attention Mechanism

    借鉴了DenseNet的思想,用了空洞卷积而不是池化,使得特征图不会缩小,因此每个dense连接都可以直接连,最后一层是包括了前面所有层的特征图. 此外还加入了channel-wise的注意力,对每个 ...

  5. unity2019新建LWRP项目出错:Failed to resolve project template

    原因不详,但是在C盘创建项目的确会出现这个问题,改到D盘或E盘就没这个问题了

  6. dubbo架构角色

    角色 Dubbo有5个参与者:其中Monitor.Registry不是必须的 Provider 暴露服务的服务提供方 Consumer 调用远程服务的服务消费方(负载均衡) Registry 服务注册 ...

  7. 【mybatis源码学习】mybatis的sql语句映射

    一.重要的接口和类 org.apache.ibatis.scripting.LanguageDriver //语言驱动org.apache.ibatis.scripting.xmltags.XMLLa ...

  8. odoo开发笔记 -- 多个子类继承同一个父类方法的执行顺序

    场景描述: odoo模块化开发的架构理念,科学&高效, 可以让很多业务场景,尽可能松耦合:让开发人员的主要精力,关注在当前的业务逻辑: 所谓「前人栽树,后人乘凉」,模块整体好比一棵大树, 开发 ...

  9. ISO/IEC 9899:2011 条款6.10.2——源文件包含

    6.10.2 源文件包含 约束 1.一个#include指示符应该指定一个能被实现处理的头文件或源文件. 语义 2.一个预处理指示符如下形式 #    include    <h-char-se ...

  10. Sword 第三方库介绍二

    /* uuid生成 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <ass ...