题目: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. ArcGis Python脚本——将细碎小面合并到相邻的面积最大的面

    参数: polygon_fc 面要素类 area_limit 给定面积值,小于它的面会被合并 给定两个参数即可,这回没有注释. #polygon_fc 面要素类 #area_limit 给定面积值,小 ...

  2. Modbus通讯两种传输方式

    控制器能设置为两种传输模式(ASCII或RTU)中的任何一种在标准的Modbus网络通信.用户选择想要的模式,包括串口通信参数(波特率.校验方式等),在配置每个控制器的时候,在一个Modbus网络上的 ...

  3. python selectsort

    # -*- coding: utf-8 -*-"""------------------------------------------------- File Name ...

  4. Moment.js简单使用

    1.设置语言环境,如设置中文环境: moment.locale("zh-cn"); 2.当前时间.指定时间: // 假设当前时间为:2018年12月10日 moment(); // ...

  5. dubbo直连提供者 & 只订阅 & 只注册

    1.    dubbo直连提供者 在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候可能需要点对点直连,点对点直连方式,将以服务接口为单位,忽略注册中心的提供者列表,A 接口配置点 ...

  6. Java 多线程 - 生产者消费者问题

    https://www.cnblogs.com/hckblogs/p/7858545.html

  7. edx的ST

    卷积概率论 顺便了解了一下卷积函数 反正以后学CNN还要再看 这个mit老师水平真的不太行 倒是以这个为源头找到不少好的youtube视频 看看她题目出的怎么样吧... cousera今天估计没时间了

  8. ffmpeg推流方式采用TCP协议

    ffmpeg默认推流方式采用UDP方式,若需要使用TCP协议,则需要修改. 1.使用命令时: ffmpeg 跟参数 -rtsp_transport tcp 2.编码时 AVFormatContext ...

  9. python 字符串转化为json、post请求

    在json模块有2个方法, loads():将json数据转化成dict数据 dumps():将dict数据转化成json数据 load():读取json文件数据,转成dict数据 dump():将d ...

  10. Helloworld——SpringMVC

    搭建环境:eclipse 这里需要配置Server runtime environment——Apache Tomcat 到官网下载 解压 在eclipse中: Window perferences ...