• 爬楼梯:斐波那契数列

    假设你正在爬楼梯。需要 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初级算法之动态规划的更多相关文章

  1. 【LeetCode算法】LeetCode初级算法——字符串

      在LeetCode初级算法的字符串专题中,共给出了九道题目,分别为:反转字符串,整数反转,字符串中的第一个唯一字符,有效的字母异位词,验证回文字符串,字符串转换整数,实现strStr(),报数,最 ...

  2. 算法练习LeetCode初级算法之链表

    删除链表中的节点 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode ne ...

  3. 算法练习LeetCode初级算法之字符串

    反转字符串 我的解法比较low,利用集合的工具类Collections.reverse反转,用时过长 class Solution { public void reverseString(char[] ...

  4. 算法练习LeetCode初级算法之数组

    删除数组中的重复项 官方解答: 旋转数组 存在重复元素 只出现一次的数     官方解答:  同一个字符进行两次异或运算就会回到原来的值 两个数组的交集 II import java.util.Arr ...

  5. 算法练习LeetCode初级算法之其他

    位1的个数 解法一: class Solution { // you need to treat n as an unsigned value public int hammingWeight(int ...

  6. 算法练习LeetCode初级算法之数学

    Fizz Buzz class Solution { public List<String> fizzBuzz(int n) { List<String> list=new L ...

  7. 算法练习LeetCode初级算法之设计问题

    打乱数组 不断的让第一个与后面随机选择的数交换 class Solution { private int[] nums; private int[] initnums; public Solution ...

  8. 算法练习LeetCode初级算法之排序和搜索

    合并两个有序数组 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { System.arrayco ...

  9. 算法练习LeetCode初级算法之树

    二叉树的前序遍历 我的解法:利用递归,自底向下逐步添加到list,返回最终的前序遍历list class Solution { public List<Integer> preorderT ...

随机推荐

  1. linux中.nfsxxxx引起的文件无法删除

    一个linux系统中的某个文件夹无法删除,使用ls -al查看有1个.nfsxxxx的文件无法删除. 使用lsof +D /filepath/,查看到文件被一个进程一直占用. 再使用ps -aux | ...

  2. 三个线程,ABC 10次(volatile+synchronized(2 synchronized可以保证内存可见性,所以去掉status 的volatile修饰符)

    package ThreadABC; public class MyThread extends Thread { public static int status = 0; @Override pu ...

  3. 事件&表达式

    typeFaqs.ForEach(async p => { var results = await  ; p.Results = results; }); https://stackoverfl ...

  4. 著名java博客

    http://blog.csdn.net/net19880504/article/details/20807403

  5. scikit-learn框架学习笔记(一)

    sklearn于2006年问世于Google,是使用python语言编写的.基于numpy.scipy和matplotlib的一个机器学习算法库,设计的非常优雅,它让我们能够使用同样的接口来实现所有不 ...

  6. mysql表关联

    mysql的表关联: left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录 inner ...

  7. java 括号匹配 成对

    import java.util.Stack; public class Solution { public static void main(String[] args) { Solution s ...

  8. SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)

    1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...

  9. TIMESTAMP(6)类型的时间差

    TIMESTAMP 数据类型 它包括了所有DATE数据类型的年月日时分秒的信息,而且包括了小数秒的信息. 以分钟为单位查询时间差 select ROUND(TO_NUMBER(to_date(to_c ...

  10. [Torch]提示torch.inverse错误

    来源:https://blog.csdn.net/zziahgf/article/details/72548128 当执行官方第二个例子时,提示xs = torch.inverse(A)*b错误 是因 ...