【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]; } }
【LEETCODE】59、数组分类,适中级别,题目:39、48、64的更多相关文章
- LeetCode:颜色分类【75】
LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...
- LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)
这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...
- LeetCode:数组中的第K个最大元素【215】
LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- LeetCode一维数组的动态和
一维数组的动态和 题目描述 给你一个数组 nums.数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]...nums[i]). 请返回 nums 的动态和. 示例 1: ...
- 【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73
这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么??? 这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里??? 最近一直苦苦思考,不明所以,刷题刷得更多的感 ...
- 【LEETCODE】60、数组分类,适中级别,题目:75、560、105
package y2019.Algorithm.array.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.a ...
- 【LEETCODE】58、数组分类,适中级别,题目:238、78、287
package y2019.Algorithm.array.medium; import java.util.Arrays; /** * @ProjectName: cutter-point * @P ...
- 【LEETCODE】57、数组分类,适中级别,题目:969、442、695
package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...
随机推荐
- 过滤器的API
1.API a.生命周期(和servletcontext相似): (1)创建:服务器启动的时候创建(执行init方法). (2)销毁:服务器关闭的时候销毁(执行destory方法). b.filter ...
- redash oracle 数据源docker 镜像
redash 官方的docker 镜像是没有包含oracle的,需要我们自己添加,参考了一个docker 镜像进行了简单的修改 Dockerfile FROM redash/redash:7.0.0. ...
- 23-ESP8266 SDK开发基础入门篇--编写Android TCP客户端 , 加入消息处理
https://www.cnblogs.com/yangfengwu/p/11203546.html 先做接收消息 然后接着 public class MainActivity extends App ...
- 2017.10.4 国庆清北 D4T2 正方形
题目描述 在一个10000*10000的二维平面上,有n颗糖果. LYK喜欢吃糖果!并且它给自己立了规定,一定要吃其中的至少C颗糖果! 事与愿违,LYK只被允许圈出一个正方形,它只能吃在正方形里面的糖 ...
- centos7最小化安装准备工作
1.配置网络 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16780032 HWADDR=00:0C:29:48:9 ...
- MySQL实现按天分组统计,提供完整日期列表,无数据自动补0
业务需求最近要在系统中加个统计功能,要求是按指定日期范围里按天分组统计数据量,并且要能够查看该时间段内每天的数据量. 解决思路直接按数据表日期字段group by统计,发现如果某天没数据,该日期是不出 ...
- vue---使用Class
在用vue-cli开发项目的时候,很多时候会用到类.具体的使用方法: config.js(使用类,还可以定义构造函数) class config { /** * 构造函数 * @param {stri ...
- SpringCloud中遇到的问题总结
1.如果数据库URL字符串中不加serverTimezone=GMT%2B8且数据库未设置时区,会报如下错误 Caused by: com.mysql.cj.exceptions.InvalidCon ...
- 【Js】单页面多个倒计时问题
代码: <!DOCTYPE html> <html> <head> <title>多个timeout</title> <script ...
- cmd 连接oracle
第一步: sqlplus/nolog 第二步: conn 数据库名/密码@ip:端口/database: conn bill:/orcl 如果是连接不上. 报错信息为 Oracle ORA-0103 ...