题目: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. 使用 LD_PRELOAD 变量拦截调用

    背景&原理 很多 a.out 程序都依赖动态库 libc.so, 比如使用 strcmp() 比较密码, 其实是不安全的 使用 LD_PRELOAD 变量可以使该变量中的可链接文件(编译时使用 ...

  2. uwsgi+anaconda+nginx部署django项目(ubuntu下)

    conda 环境不必多说: conda(或source)  activate  test 进入test虚拟环境 接下来安装uwsgi: pip install uwsgi 在conda环境下大概率安装 ...

  3. git错误记录及解决

    一.git每次提交.拉取都要输用户名和密码 问题描述:每次提交.拉取文件时都要输用户名和密码,特别麻烦 原因:在git上面注册了用户名a,然后本机安装了TortoiseGit工具,登录时会在本机C:\ ...

  4. JSON字符串与Map互转

    //一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return JSONObject.toJSON ...

  5. es集群数据库~运维相关

    一 数据同步方案  1 ES-JDBC  不能实现删除同步操作.MYSQL如果删除,ES不会删除  2 logstash-input-jdbc  能实现insert update,但是仍然不能实现删除 ...

  6. 2018-2019 20165221 网络对抗 Exp5 MSF基础

    2018-2019 20165221 网络对抗 Exp5 MSF基础 实践内容: 重点掌握metassploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主动攻击实践,如ms0 ...

  7. 盒子取球C语言 蓝桥杯

    盒子取球方法二今盒子里有 n 个小球,A.B 两人轮流从盒中取球,每个人都可以看到另一个人取了多少个, 也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断. 我们约定:每个人从盒子中取出 ...

  8. nginx 配置proxy_pass URL末尾加与不加/(斜线)的区别

    nginx在配置proxy_pass的时候 URL结尾加斜线(/)与不加的区别和注意事项 假设访问路径的 /pss/bill.html 加/斜线的情况 location /pss/ { proxy_p ...

  9. 生成透视列之for xml path

    临时表#t原始数据: 实现如下格式,即根据Province分组,把每个组对应的City列以某种格式展示: 实现方法: select t.Province,(select city+',' From # ...

  10. mysql常用

    查询表占用大小 select sum(DATA_LENGTH)+sum(INDEX_LENGTH) from information_schema.tables where table_schema= ...