【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 ...
随机推荐
- Android Studio3.0的下载及其安装详解加eclipse下载安装配置jdk9
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 今天我们来讲解如何下载android studio 3.0及其 ...
- 无人机一体化3DGIS服务平台
随着无人机技术的发展,无人机携带多种设备为GIS应用提供多元化海量基础数据.无人机航测更是以快速.灵活.高效的数据获取方式,迅速扩大了现有的GIS市场,同时GIS行业的广泛应用也推动了无人机技术的发展 ...
- SSH框架整合3——原生态SessionFactory
================================================web.xml============================================= ...
- java web开发及Servlet常用的代码
日志 1.使用门面模式的slfj,并结合log4j,logback. 2.info.debug.error,要写清楚. 3.使用占位符,如下: log.info("用户id为: {} &qu ...
- IntelliJ IDEA 2018.2,WebStorm 2018.2破解
一.IntelliJ IDEA 2018.2.4破解: 可参考:https://www.cnblogs.com/iathanasy/p/9469280.html 二.WebStorm 2018.2.4 ...
- [转]Windows内存堆内容整理总结
在系统安全研究中,堆,是一个极其重要的内存区域以及研究的热点.堆,区别于栈区.全局数据区以及代码区,它的主要作用是允许程序在运行时动态地申请某个大小的内存空间.本文将从宏观到微观,简单梳理总结一下Wi ...
- OpenTK学习笔记(1)-源码、官网地址
OpenTK源码下载地址:https://github.com/opentk/opentk OpenTK使用Nuget安装命令:OpenTK:Install-Package OpenTK -Versi ...
- 接口测试中模拟post四种请求数据
https://www.jianshu.com/p/3b6d7aa2043a 一.背景介绍 在日常的接口测试工作中,模拟接口请求通常有两种方法,fiddler模拟和HttpClient模拟. Fidd ...
- 爬虫urllib2 的异常错误处理URLError和HTTPError
urllib2 的异常错误处理 在我们用urlopen或opener.open方法发出一个请求时,如果urlopen或opener.open不能处理这个response,就产生错误. 这里主要说的是U ...
- 【转载】 《Human-level concept learning through probabilistic program induction》阅读笔记
原文地址: https://blog.csdn.net/ln1996/article/details/78459060 --------------------- 作者:lnn_csdn 来源:CSD ...