算法练习LeetCode初级算法之动态规划
- 爬楼梯:斐波那契数列- 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 - 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? - 注意:给定 n 是一个正整数。 
- 非递归解法- class Solution { - public int climbStairs(int n) { - if (n==1) { - return 1; - } - if (n==2) { - return 2; - } - int n1=1,n2=2; - for (int i = 0; i <n-2; i++) { - int m=n1+n2; - n1=n2; - n2=m; - } - return n2; - } - } 
- 递归解法- class Solution { - int[] result=null; - public int climbStairs(int n) { - result=new int[n+1]; - Arrays.fill(result, -1); - f(n); - return result[n]; - } - private void f(int X) { - if (result[X]!=-1) { - return; - } - if (X==0||X==1) { - result[X]=1; - return; - } - f(X-1); - f(X-2); - result[X]=result[X-1]+result[X-2]; - } - } 
- 买卖股票的最佳时机- 重点是要设置一个最小值和一个最大值,并且不断替换! - class Solution { - public int maxProfit(int[] prices) { - if (prices.length==0||prices.length==1) { - return 0; - } - int minPrice=prices[0]; - int maxPrice=0; - for (int i = 0; i < prices.length; i++) { - if (prices[i]<=minPrice) { - minPrice=prices[i]; - }else if ((prices[i]-minPrice)>maxPrice) { - maxPrice=prices[i]-minPrice; - } - } - return maxPrice; - } - } 
- 最大子序和
- 超出时间限制的解法- class Solution { - public int maxSubArray(int[] nums) { - if (nums.length==0) { - return 0; - } - if (nums.length==1) { - return nums[0]; - } - int sum=0; - Set<Integer> list=new TreeSet<>(); - int n=1; - while (n<=nums.length) { - for (int i = 0; i < nums.length-n+1; i++) { - int m=0; - for (int j = 0; j < n; j++) { - m+=nums[i+j]; - } - list.add(m); - } - n++; - } - int res=0; - for (Iterator iterator = list.iterator(); iterator.hasNext();) { - res= (Integer) iterator.next(); - } - return res; - } - } 
- 优化解法
没想到可以这样解,厉害!找个例子试一试就懂了
class Solution {
public int maxSubArray(int[] nums)
{
if (nums.length==0) {
return 0;
}
if (nums.length==1) {
return nums[0];
}
int max=nums[0];
int sum=0;
for (int i = 0; i < nums.length; i++) {
if (sum>0) {
sum+=nums[i];
}else {
sum=nums[i];
}
max=Math.max(max, sum);
}
return max;
}
}
- 更简单的解法:找最大子序列,最重要的要分清正负!!!- class Solution { - public int maxSubArray(int[] nums) - { - int max=nums[0]; - int sum=nums[0]; - for (int i = 1; i < nums.length; i++) { - sum=Math.max(sum+nums[i], nums[i]); - max=Math.max(max, sum); - } - return max; - } - } 
- 打家劫舍
挺难的,参考别人的解法,先记住
- 递归法- class Solution { - //测试2,1,1,2 - private int[] memo; - public int rob(int[] nums) { - memo=new int[nums.length]; - Arrays.fill(memo, -1); - return tryRob(nums, 0); - } - private int tryRob(int[] nums,int index) { - if (index>=nums.length) { - return 0; - } - if (memo[index]!=-1) { - return memo[index]; - } - int res=0; - for (int i = index; i < nums.length; i++) {//循环每次后移,即可以跳过(相隔)两个或多个 - res=Math.max(res, nums[i]+tryRob(nums,i+2)); - } - memo[index]=res; - return res; - } - } 
- 动态规划- class Solution { - //测试2,1,1,2 - public int rob(int[] nums) { - int n = nums.length; - if (n == 0) { - return 0; - } - if (n==1) { - return nums[0]; - } - if (n==2) { - return Math.max(nums[0], nums[1]); - } - int[] f = new int[n]; - f[0]=nums[0]; - f[1]=Math.max(nums[0], nums[1]);//典型动态规划问题,先将子问题记录,然后 - for (int i = 2; i < f.length; i++) { - f[i]=Math.max(f[i-2]+nums[i], f[i-1]);//这里利用子问题来解决问题 - } - return f[n-1]; - } - } - 参考:https://blog.csdn.net/likunkun__/article/details/80724683 
算法练习LeetCode初级算法之动态规划的更多相关文章
- 【LeetCode算法】LeetCode初级算法——字符串
		在LeetCode初级算法的字符串专题中,共给出了九道题目,分别为:反转字符串,整数反转,字符串中的第一个唯一字符,有效的字母异位词,验证回文字符串,字符串转换整数,实现strStr(),报数,最 ... 
- 算法练习LeetCode初级算法之链表
		删除链表中的节点 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode ne ... 
- 算法练习LeetCode初级算法之字符串
		反转字符串 我的解法比较low,利用集合的工具类Collections.reverse反转,用时过长 class Solution { public void reverseString(char[] ... 
- 算法练习LeetCode初级算法之数组
		删除数组中的重复项 官方解答: 旋转数组 存在重复元素 只出现一次的数 官方解答: 同一个字符进行两次异或运算就会回到原来的值 两个数组的交集 II import java.util.Arr ... 
- 算法练习LeetCode初级算法之其他
		位1的个数 解法一: class Solution { // you need to treat n as an unsigned value public int hammingWeight(int ... 
- 算法练习LeetCode初级算法之数学
		Fizz Buzz class Solution { public List<String> fizzBuzz(int n) { List<String> list=new L ... 
- 算法练习LeetCode初级算法之设计问题
		打乱数组 不断的让第一个与后面随机选择的数交换 class Solution { private int[] nums; private int[] initnums; public Solution ... 
- 算法练习LeetCode初级算法之排序和搜索
		合并两个有序数组 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { System.arrayco ... 
- 算法练习LeetCode初级算法之树
		二叉树的前序遍历 我的解法:利用递归,自底向下逐步添加到list,返回最终的前序遍历list class Solution { public List<Integer> preorderT ... 
随机推荐
- 2018-2019-2 20165312《网络攻防技术》Exp4 恶意代码分析
			2018-2019-2 20165312<网络攻防技术>Exp4 恶意代码分析 知识点总结 1.有关schtasks schtacks的作用:安排命令和程序定期运行或在指定时间内运行.从计 ... 
- 5、分布式缓存Redis之bitmap、setbit
			基本语法: 1)SETBIT redis 127.0.0.1:6379> setbit KEY_NAME OFFSET VALUE //该命令用于对 key 所储存的字符串值,设置或清除指定偏移 ... 
- idea启动tomcat 找不到 类,或者报Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
			这主要是打成war包的时候没有讲导入的jar也添加进去,只需要添加进来就行啦 可以看到WEB-INF目录下没有lib目录 put into output root 就可以了 然后就可以啦 
- python,列表,元祖,字典
			list 列表 li = [1,",[3,4]] 1.用中括号括起来 2.用,来分割每一个元素 3.列表中的元素可以是,数字,字符串,列表,布尔值 4.“集合”,内部可以放置任何东西 li ... 
- leetCode83. 删除排序链表中的重复元素
			给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: 1->1->2->3-&g ... 
- zabbix 邮件报警配置
			zabbxi 邮件告警推送有两种方式: 1.使用zabbix服务端的本地邮箱账号发送,邮件名为:user@hostname.localdomain,user为发送邮件的用户,hostname为zabb ... 
- python中os.path模块简介
			1.python中获取当前工作目录 curDir = os.getcwd() os.getcwd()返回的是执行命令时所在的目录,而不是脚本本身所在的目录 2.os.path os.path.absp ... 
- 简介C#读取XML的方式(转)
			在程序中访问进而操作XML文件一般有两种模型,分别是使用DOM(文档对象模型)和流模型,使用DOM的好处在于它允许编辑和更新XML文档,可以随机访问文档中的数据,可以使用XPath查询. XML作用 ... 
- python大法好——mysql防注入
			MySQL 及 SQL 注入 如果您通过网页获取用户输入的数据并将其插入一个MySQL数据库,那么就有可能发生SQL注入安全的问题. 本章节将为大家介绍如何防止SQL注入,并通过脚本来过滤SQL中注入 ... 
- Delphi  窗口操作
			unit UnitWinUtils; interface uses Windows; Type TDWA128=Array [..] of LongWord; TDWA256=Array [..] o ... 
