题目: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. GIL全局锁测试

    基础知识:https://www.cnblogs.com/SuKiWX/p/8804974.html 测试环境 python3.7默认解释器(cpython) cpu为四核 测试代码 #! /usr/ ...

  2. mui框架中dialog框的实现

    <script type="text/javascript" charset="utf-8"> //mui初始化 mui.init({ swipeB ...

  3. kail linux虚拟机安装tools工具

    因为自己比较懒,有时候自己不想打字需要粘贴就安装了虚拟机tools工具,又因为自己脑子不好使所以就写一下步骤,以便以后用得着.我这里用得是kail linux系统,不知道contest能不能这样安,下 ...

  4. 多个Fragment在一个activity中通过按钮的展示方法

    fragment使用方法 1. 创建主Mainactivity extends AppCompatActivity 2. Oncreate & setContentView 3. 完成XML的 ...

  5. js reduce()方法使用

    reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. 注意: reduce() 对 ...

  6. raw_input与input

    raw_input 不管用户输入的是什么,最后打印的类型都会是str字符串类型 input 会根据用户的输入变换成相应的类型,但是需要注意的是我们用户在输入字符或者字符串的时候,需要给他们加上双引号, ...

  7. day 18 - 2 正则与 re 模块练习

    1.爬虫的例子 #爬虫的例子(方法一) import re import urllib,request import urlopen def getPage(url): response = urlo ...

  8. 「JavaScript面向对象编程指南」闭包

    闭包 JS只有函数作用域,函数外为全局变量,函数内为局部变量 绿圆是函数fn的作用域,在这范围内可访问局部变量b和全局变量a,橙圆是fn内部函数inner的作用域,此范围内可访问自身作用域内的变量c, ...

  9. Spring 基于注解的AOP实现

    在本文开始之前,我要引入一张图,这张图的来源 https://blog.csdn.net/chenyao1994/article/details/79708496 ,版权归原作者所有,我借鉴了原作者的 ...

  10. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...