题目:https://leetcode.com/problems/freedom-trail/

额。。。不解释大意了,题目我也不想写过程了有点繁琐,直接给出代码:

 public int findRotateSteps(String ring, String key) {
int rlen = ring.length();
int klen = key.length();
int[][]dp = new int[klen+1][rlen+1];
int temp = 0,step = 0,diff = 0;
for(int i = 0;i<=klen;i++) {
for(int j = 1;j<=rlen;j++)
if(i==0)dp[i][j] = 0;
else dp[i][j] = Integer.MAX_VALUE;
}
for(int i = 1;i<=klen;i++) {
for(int j = 1;j<=rlen;j++) {
temp = Integer.MAX_VALUE;
if(ring.charAt(j-1)==key.charAt(i-1)) {
if(i==1)temp = Math.min(j-1,rlen+1-j);
else {
for(int k = 1;k<=rlen;k++)
if(dp[i-1][k]!=Integer.MAX_VALUE) {
diff = Math.abs(j-k);
step = Math.min(diff,rlen-diff);
temp = Math.min(temp,step+dp[i-1][k]);
}
}
}
dp[i][j] = temp;
}
}
/*
for(int i = 1;i<=klen;i++) {
for(int j = 1;j<=rlen;j++)
System.out.print(dp[i][j]+" ");
System.out.println();
}*/
int ans = dp[klen][1];
for(int i = 2;i<=rlen;i++) {
ans = ans < dp[klen][i]?ans:dp[klen][i];
}
return ans+klen;
}

动态规划——Freedom Trail的更多相关文章

  1. [LeetCode] Freedom Trail 自由之路

    In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...

  2. [Swift]LeetCode514. 自由之路 | Freedom Trail

    In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...

  3. 514. Freedom Trail

    In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...

  4. 514 Freedom Trail 自由之路

    详见:https://leetcode.com/problems/freedom-trail/description/ C++: class Solution { public: int findRo ...

  5. LeetCode 514----Freedom Trail

    问题描述 In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a ...

  6. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  7. Leetcode 514.自由之路

    自由之路 视频游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring"的金属表盘,并使用表盘拼写特定关键词 ...

  8. leetcode 学习心得 (2) (301~516)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 我的长大app开发教程第一弹:Fragment布局

    在接下来的一段时间里我会发布一个相对连续的Android教程,这个教程会讲述我是如何从零开始开发“我的长大”这个Android应用. 在开始之前,我先来介绍一下“我的长大”:这是一个校园社交app,准 ...

  2. reduce/filter/map/zip/isinstance/list列表推导式

  3. jQuery中哪几种选择器

    基本选择器:直接根据id,css类名,元素名返回dom元素: 层次选择器:也叫路径选择器: $("div span") 选取<div>里的所有<span>元 ...

  4. 我常用的sublime快捷键整理

    由于很多人用sublime作为编辑器,我想我平时学习的时候也可以用sublime写demo,顺便熟悉一下sublime的使用方法.慢慢发现sublime中快捷键能节省很多时间,很方便,但是同时快捷键很 ...

  5. windows安装解压版mysql

    记录下用批处理安装mysql5.7.18的过程与踩到的坑 先在安装目录新建文件my.ini [mysql] default-character-set=utf8 basedir=TODO datadi ...

  6. vue适配移动端px自动转化为rem

    1.下载lib-flexible 我使用的是vue-cli+webpack,所以是通过npm来安装的 npm i lib-flexible --save 2.引入lib-flexible 在main. ...

  7. apt-get install 出问题怎么办?

    有时候在用apt-get安装包的时候总是会莫名其妙出现各种问题,建议先把如下命令行按顺序敲一遍,基本上都能解决 sudo apt-get clean sudo apt-get update sudo ...

  8. docker中的镜像中运行Django项目

    首先要在镜像中 安装python3 以及 django2.0.4 目前我用的是这两个版本. 进入镜像 创建项目 进入项目中修改setting文件 将引号和星号添加进括号中 ALLOWED_HOSTS ...

  9. 【easy】Number of Segments in a String 字符串中的分段数量

    以空格为分隔符,判断一个string可以被分成几部分. 注意几种情况:(1)全都是空格 (2)空字符串(3)结尾有空格 思路: 只要统计出单词的数量即可.那么我们的做法是遍历字符串,遇到空格直接跳过, ...

  10. 【easy】189. Rotate Array

    题目标签:Array 题目给了我们一个数组 和 k. 让我们 旋转数组 k 次. 方法一: 这里有一个很巧妙的方法: 利用数组的length - k 把数组 分为两半: reverse 左边和右边的数 ...