leetcode算法题解(Java版)-16-动态规划(单词包含问题)
一、递归
题目描述
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
思路
- 碰到二叉树的问题,差不多就是深搜、广搜,递归那方面想想了,当然如果要考虑一下空间、时间,还需要进行剪枝和压缩处理。这题比较简单:判断两个树是否相等,可以递归的判断子树是否相等,最后找到边界条件就是是否都为空,都不为空时节点里面的值是否相等。
代码
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p==null&&q==null){ return true; } else if(p==null||q==null){ return false; } else if(q.val!=p.val){ return false; } else{ return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right); } } }
二、动态规划
题目描述
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s ="leetcode",
dict =["leet", "code"].
Return true because"leetcode"can be segmented as"leet code".
思路
- dp的题目,写了几道了。核心无非就是确定dp数组和状态转移方程。这几道题都有明显的特点,那就是dp数组记录的就是所求的答案,所以答案一般都是dp[s.length()]这种形式的。
- 有了上面的总结,再来看着道题目。要求一串字母是否可以由所给的字典中的单词拼出来,要求返回布尔型。那好,也同时提示我们了dp数组就是记录它的子串是否能满足要求,类型是布尔型:dp[i]表示的是s[0,i-1]这个子串能否满足要求。
- dp[i]=dp[j]&&s[j,i]是否在字典中(0<=j<=i-1)
代码
import java.util.Set; public class Solution { public boolean wordBreak(String s, Set<String> dict) { if(s==null||s.length()==0||dict==null||dict.size()==0){ return false; } boolean [] dp = new boolean [s.length()+2]; dp[0] = true; for(int i=1;i<=s.length();i++){ for(int j=i-1;j>=0;j--){//从尾部扫描单词 if(dp[j]==true&&dict.contains(s.substring(j,i))){ dp[i]=true; break; } else{ dp[i]=false; } } } return dp[s.length()]; } }
三、深搜
题目描述
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s ="catsanddog",
dict =["cat", "cats", "and", "sand", "dog"].
A solution is["cats and dog", "cat sand dog"].
思路
- 题目是上一道题的加强版,为了考察动态规划。感觉有点复杂,没想出来怎么在上一道的基础上改进。
- 深搜可以解决问题!直接看代码了
代码
import java.util.ArrayList; import java.util.Set; public class Solution { public ArrayList<String> res = new ArrayList<>(); public ArrayList<String> wordBreak(String s, Set<String> dict) { dfs(s,s.length(),dict,""); return res; } public void dfs(String s,int index,Set<String> dict,String temp){ if(index==0){ if(temp.length()>0){ res.add(temp.substring(0,temp.length()-1));//如果写成res.add(temp)则末尾会多一个空格,小细节 } } for(int i=index-1;i>=0;i--){ if(dict.contains(s.substring(i,index))){ dfs(s,i,dict,s.substring(i,index)+" "+temp); } } } }

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Java的反射机制
前言: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。 Java反射的使用: 有如下student类: public class Student { private int age; private String name; public Student() { super(); } public Student(int age, String name) { super(); this.age = age; this.name = name; } public void study() { System.out.println("测试反射!"); } } 现在通过反射来获取student类的信息:一、获取class对象 获取class对象有三种方式,如下:1、方式一: Class clazz =Class.forName("com.demos.reflect.Student"); 注意:forName里面的参数是Student的全类...
- 下一篇
Python3函数,形参,实参,返回值及相关细节
Python3基础——函数 (赠言):编程实现某项功能最终都依赖与函数,函数是实现某个功能的代码集合体,实现某一逻辑功能的都可以定义成一个函数,它给程序提供了简洁,可读的优良特质。大家在编程过程中要尽量将某一逻辑功能的代码块定义成函数,这样能增加代码的可阅读性同时能是程序看起来更简洁。 函数声明定义:Python函数定义并不像c++,java,c.....定义那样c++函数定义. c,c++,java函数定义:(返回类型) 函数名(参数列表) ---返回类型有 void(无返回),int(返回一个整型值) ,double(返回一个双精度浮点值).....等等如:void getName(string name) ,int getNumber(int number) 。 下面我们来看看Python怎么进行函数定义的 Python函数定义:def 函数名 (参数列表): def是关键字,目的是让Python知道你定义了一个函数,而不是其他例子如:**def get_name(name): def get_number(number):**-------为什么函数命名会是这样呢?我在Pyth...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2全家桶,快速入门学习开发网站教程
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- MySQL8.0.19开启GTID主从同步CentOS8
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Linux系统CentOS6、CentOS7手动修改IP地址