一、全排列变式(递归)

题目描述

Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2]have the following unique permutations:
[1,1,2],[1,2,1], and[2,1,1].

思路

  • 多了一个条件,就是有重复数字出现。那可以考虑先排序,然后递归选择在相应位置放置数字的时候,可以添加判断是否被用过,也就是和前面那个比较一下:如果前面那个数和它一样值,而且目前的used 显示false那说明这个数已经在这个位置被用过了,而且已经计入了结果res中。

代码

import java.util.ArrayList;
import java.util.Arrays;public class Solution {public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {ArrayList<ArrayList<Integer>> res=new ArrayList<>();int len=num.length;if(num==null||len==0){return res;}boolean [] used=new boolean[len];ArrayList<Integer> list=new ArrayList<>();Arrays.sort(num);dfs(list,num,used,res);return res;}public void dfs(ArrayList<Integer> list,int [] num,boolean [] used,ArrayList<ArrayList<Integer>> res){if(list.size()==num.length){res.add(new ArrayList<Integer>(list));return ;}for(int i=0;i<num.length;i++){if(i>0&&num[i]==num[i-1]&&!used[i-1]){//如果它前一个和它一样的数现在没有被用过,那就跳过,//说明之前那个数已经形成过结果list之一了,目前这个分支处于回溯阶段。。。有点绕,希望能明白continue;}if(!used[i]){used[i]=true;list.add(num[i]);dfs(list,num,used,res);list.remove(list.size()-1);used[i]=false;}}}
}

二、贪心

题目描述

Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A =[2,3,1,1,4], returntrue.
A =[3,2,1,0,4], returnfalse

思路

  • 贪心的题,每走一步,跟新一下从当前这个位置所能到达的最大距离。这就是这题的贪心,贪心一般证明很难,但是可以逻辑简单想一下,肯定是能走的距离越大越好,越有可能到达终点,再说,如果最大距离都不能到终点,那其他情况更加不可能了。

代码

public class Solution {public boolean canJump(int[] A) {int maxlen=A[0];for(int i=1;i<A.length;i++){if(maxlen==0){return false;}maxlen-=1;maxlen=Math.max(maxlen,A[i]);//if(maxlen+i==A.length-1){//剪枝//    return true;// }}return true;}
}

三、动态规划or贪心

题目描述

Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =[2,3,1,1,4]
The minimum number of jumps to reach the last index is2. (Jump1step from index 0 to 1, then3steps to the last index

思路

  • dp解法:
  • 题目想考察的是贪心,想了一下,思绪有点乱。先看一下动态规划的解法。
  • 首先,满足dp的条件:1.子问题的最优解也是全局的最优解,有最优子结构;2.当前状态只依赖于它上一个状态,与怎么到达它上一个状态的路径无关。

代码

public class Solution {public int jump(int[] A) {int [] dp=new int[A.length];//存放起点到各点的最小步数for(int i=0;i<A.length;i++){int maxpos=Math.min(i+A[i],A.length-1);for(int j=i+1;j<=maxpos;j++){if(dp[j]==0){dp[j]=dp[i]+1;}}if(dp[A.length-1]!=0){break;}}return dp[A.length-1];}
}

思路二

  • 贪心解法:
  • 贪心大法好,但真的难想明白。。首先,要设置三个值:当前位置(是一个区域:从i~furthest_pre中,区域中的点中能到达的最大位置)所能到达的最大位置(furthest_cur),当前位置的上一个位置(也是区域)所能到达的最大位置(furthest_pre),还有就是所走的步数。
  • 有一点可能有点会懵,furthest_cur是还没有step++的时候,具体结合代码,也就是是一个预判能走到的但还没走的状态。
  • 感觉讲的还是有点乱,现在抛开代码,想象我们站在题目给的数组的开头位置:从开始位置到开始位置能走到的最大距离(furthest_pre)之间构成了一块区域,然后我们开始一格一格走,每走一下刷新一下当前这块区域能到的最大位置(furthest_cur),如果走到从开始位置走到了furthest_pre那我们也刷新出了最大的furthest_cur,如果furthest_cur比终点大,那恭喜!再跳一不就到终点了!可以开始跳一步咯!然后重复上述的动作,直到到达终点。
  • 如果能一步到最大位置furthest_pre,那肯定能到一步到它前面那块区域的某一位置,实行下一步跳,跳到furthest_cur
  • 给一个测试用例,可以在纸上画画:
4 1 6 9 7 4 5 0 6 8 1 2 3 5 8 0 2 1 2

代码

public class Solution {public int jump(int[] A) {int len=A.length;if(len==0||A==null){return 0;}int furthest_cur=0;int furthest_pre=0;int steps=0;for(int i=0;i<len;i++){if(furthest_pre>=len){return steps;}if(i>furthest_pre){furthest_pre=furthest_cur;steps++;}furthest_cur=Math.max(furthest_cur,A[i]+i);}return steps;}
}

leetcode算法题解(Java版)-11-贪心大法相关推荐

  1. leetcode算法题解(Java版)-9-N皇后问题

    一.贪心 题目描述 Find the contiguous subarray within an array (containing at least one number) which has th ...

  2. leetcode算法题解(Java版)-16-动态规划(单词包含问题)

    摘要: 碰到二叉树的问题,差不多就是深搜.广搜,递归那方面想想了,当然如果要考虑一下空间.时间,还需要进行剪枝和压缩处理.这题比较简单:判断两个树是否相等,可以递归的判断子树是否相等,最后找到边界条件 ...

  3. java链表变成字符串,leetcode算法题解(Java版)-6-链表,字符串

    一.字符串处理 题目描述 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the r ...

  4. 经典排序算法(Java版)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 经典排序 ...

  5. 数据结构与算法(Java版) | 本套系列教程的课程亮点和授课方式

    接下来,在这一讲,我会花一点时间同同学们达成一个共识,就是我们这套系列教程在讲述的时候,究竟是以一种什么方式来讲述的.我希望,经过我的讲解之后,大家能够对我们这套系列教程的课程亮点和授课方式达成如下这 ...

  6. 算法:经典leetcode算法题解

    索引 1. https://leetcode.com/problems/patching-array/  补丁数组 2. https://leetcode.com/problems/find-the- ...

  7. 一致性Hash算法(JAVA版)(摘抄至五月的仓颉的博客)

    一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...

  8. 11. 旋转数组的最小数字(剑指 Offer 题解Java版)

    文章目录 11. 旋转数组的最小数字 题目描述 题目链接 解题思路 可以借助下图理解过程 代码 11. 旋转数组的最小数字 题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. ...

  9. 【算法java版11】:实现求s = a + aa + aaa + aaaa + aa...a 的值,其中a是一个数字,几个数相加由键盘控制

    实现求s = a + aa + aaa + aaaa + aa...a 的值 一.题目描述 二.解题思路 三.代码示例 4.测评结果 一.题目描述

最新文章

  1. webpack打包后的文件
  2. 移动web开发都会遇到的坑(会持续更新)
  3. 上传数据时 ajax请求成功 上传完成,ajaxSubmit请求返回数据成功,但是不执行success回调函数...
  4. dateformat 返回类型_SpringBoot返回date日期格式化
  5. [css] width属性的min-content和max-content有什么作用
  6. 荣耀系统更新服务器不可用,荣耀确认系统更新方式 4月1日前发布的机型固件升级由华为负责...
  7. 值传递,引用传递,指针传递
  8. 分享一本Swift好书
  9. sap甲方_带你走进SAP项目实施过程——前言
  10. c语言 选择结构,C语言学习:选择结构
  11. 【UOJ #351】新年的叶子(树的直径,期望)
  12. 企业局域网——论文开题报告
  13. 云计算虚拟化之Docker上如何安装Mongodb?
  14. 缺失值处理 - 定位空值并用空值的上一个值填充 - (Excel)
  15. php如何用sql语句修改数据库,SQL语句进行数据表的增删改查教程(phpMyAdmin使用教程)...
  16. PVT(Pyramid Vision Transformer: A Versatile Backbone for Dense Prediction without Convolutions)
  17. 巨准SCRM私域案例拆解丨看看WonderLab如何霸屏朋友圈
  18. GFPGAN:老旧照片的面部恢复神器
  19. Linux:冯诺伊曼体系结构 | 操作系统 | 显卡 | 主板
  20. Objective-C知识点总结

热门文章

  1. openSession()和getCurremtSession()的区别
  2. IOS 开发之-- textfield和textview,return键的改变,点击return键
  3. 数据库中间件MyCat学习总结(1)——MyCat入门简介
  4. linux 笔记--while循环、函数和进程管理
  5. Android设计模式系列(2)--SDK源码之观察者模式
  6. socket udp
  7. DKHadoop人力资源大数据解决方案架构
  8. db2不记录日志插入记录
  9. ASP.NET Core 1.0 使用 MySQL for EF Core 1.0 (.NET Core 1.0)
  10. Mac安装MySql 5.7.11