代码分析-leetcode:Scramble String问题

问题描述

leetcode:Scramble String问题
题目:
Given a string s1 we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.

Below is one possible representation of s1 = ""great"":

great

/
gr eat
/ /
g r e at
/
a t
To scramble the string we may choose any non-leaf node and swap its two children.

For example if we choose the node ""gr"" and swap its two children it produces a scrambled string ""rgeat"".

rgeat

/
rg eat
/ /
r g e at
/
a t
We say that ""rgeat"" is a scrambled string of ""great"".

Similarly if we continue to swap the children of nodes ""eat"" and ""at"" it produces a scrambled string""rgtae"".

rgtae

/
rg tae
/ /
r g ta e
/
t a
We say that ""rgtae"" is a scrambled string of ""great"".

Given two strings s1 and s2 of the same length determine if s2 is a scrambled string of s1.

 public class Solution {    /**     * @param s1 A string     * @param s2 Another string     * @return whether s2 is a scrambled string of s1     */    public boolean isScramble(String s1 String s2) {        // Write your code here        int n = s1.length();        if (s2.length() != n)             return false;        boolean dp[][][] = new boolean[n][n][n];        // case: length is 1        for (int i=0; i<n; i++)            for (int j=0; j<n; j++)                dp[i][j][0] = s1.charAt(i) == s2.charAt(j);        // case: length is 2....n        for (int l=1; l<n; l++)        {            for (int i=0; i+l<n; i++)            {                for (int j=0; j+l<n; j++)                {                    for (int k=0; k<l; k++)                    {                        if ((dp[i][j][k] && dp[i+k+1][j+k+1][l-1-k])                            || (dp[i][j+l-k][k] && dp[i+k+1][j][l-1-k]))                            dp[i][j][l] = true;                    }                }            }        }        return dp[0][0][n-1];    }}

我想问的是 为什么要初始dp[i][j][0],这样做的意义何在?还有很多动态规划的题目 例如 方格步数 爬楼梯 都要一开始初始化 这样的意义是什么?

解决方案

LeetCode: Scramble String
[LeetCode]Scramble String
[LeetCode]Scramble String

时间: 2024-09-09 10:36:09

代码分析-leetcode:Scramble String问题的相关文章

java-动态规划初始化问题,类似于下面这道题leetcode:Scramble String

问题描述 动态规划初始化问题,类似于下面这道题leetcode:Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / / g r e at

C语言中的数组和指针汇编代码分析实例

  这篇文章主要介绍了C语言中的数组和指针汇编代码分析实例,本文用一则C语言例子来得到对应的汇编代码,并一一注解每句汇编代码的含义,需要的朋友可以参考下 今天看<程序员面试宝典>时偶然看到讲数组和指针的存取效率,闲着无聊,就自己写了段小代码,简单分析一下C语言背后的汇编,可能很多人只注重C语言,但在实际应用当中,当出现问题时,有时候还是通过分析汇编代码能够解决问题.本文只是为初学者,大牛可以飘过~ C源代码如下: 代码如下: #include "stdafx.h" int

传智播客c/c++公开课学习笔记--C语言与木马恶意代码分析和360安全防护揭秘

黑客代码分析与预防 笔记 [课程简介] C/C++语言是除了汇编之外,最接近底层的计算机语言,目前windows,linux,iOS,Android等主流操作系统都是用C/C++编写的,所以很多病毒.木马也都是用C/C++实现的.课程的目的就是通过C语言揭秘木马和各种远程控制软件的实现原理以及如何防护.  [课程知识点] 1.木马入侵系统的方式: 2.木马入侵到宿主目标后的关键行为分析: 3.可信任端口以及端口扫描技术: 4.远程控制的实现代码实现: 5.恶意代码中使用TCP.UDP协议与防火墙

【C/C++学院】0907-象棋五子棋代码分析/寻找算法以及排序算法

象棋五子棋代码分析 编译代码报错: 错误 1 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated. You must change the project property to Unicode or download an additional library. See http://go.microsoft.com/fwlink/p/?LinkId=286820 for mo

meter资源监控器开发——关键代码分析

代码分析也无需事无巨细皆列而剖之,只要找到关键所在也就是了:又不然列一堆的声明上来,纵然有人有耐心看下去,我也没耐心写下去啊.特别关注了三 个类,Stats.MonitorPerformancePanel.MonitorGraph.分别是获取解析得到的数据.监控器面板显示和监视器上的 图像绘制.下面选取了一些关键代码来进行分析: 首先是Stats.java,下面是计算内存使用率的方法 public static int calculateMemoryLoad(Status stat) {   d

NSQ系列之nsqlookupd代码分析一(初探nsqlookup)

NSQ系列之nsqlookupd代码分析一(初探nsqlookup) nsqlookupd 是守护进程负责管理拓扑信息.客户端通过查询 nsqlookupd 来发现指定话题(topic)的生产者,并且提供 nsqd 节点广播话题(topic)和通道(channel)信息. nsqlookupd 有两个接口:TCP 接口,nsqd 用它来广播.HTTP 接口,客户端用它来发现和管理. 本系列的代码分析均是基于nsq v0.3.5的代码进行的分析,如有不对之处欢迎大家指正指导. nsqlookup

详细分析Java中String、StringBuffer、StringBuilder类的性能_java

我们先要记住三者的特征: String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 一.定义查看API会发现,String.StringBuffer.StringBuilder都实现了 CharSequence接口,虽然它们都与字符串相关,但是其处理机制不同. String:是不可改变的量,也就是创建后就不能在修改了. StringBuffer:是一个可变字符串序列,它与String一样,在内存中保存的都是一个有序的字符串序

Spring AOP实现声明式事务代码分析

 众所周知,Spring的声明式事务是利用AOP手段实现的,所谓"深入一点,你会更快乐",本文试图给出相关代码分析.   AOP联盟为增强定义了org.aopalliance.aop.Advice接口,Spring由Advice接口扩展了5中类型的增强(接口),AOP联盟自身提供了IntroductionInterceptor->MethodInterceptor->Interceptor->Advice,而MethodInterceptor就代表环绕增强,表示在目标

SDL2.0例子代码分析-----CheckKeys Project

SDL简介 SDL(Simple DirectMedia Layer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成.SDL提供了数种控制图像.声音.输出入的函数,让开发者只要用相同或是相似的代码就可以开发出跨多个平台(Linux.Windows.Mac OS X等)的应用软件.目前SDL多用于开发游戏.模拟器.媒体播放器等多媒体应用领域. SDL1.2和SDL2的差别 SDK1.2和SDL2.1系列的API接口变动的不小,当然功能也大大增强,支持多线程窗口. 具体的change 请看 h