淘宝网站开发多少金额,蝉知和wordpress,河北邯郸信息港,ui设计的基本流程图题目: 力扣514 #xff1a; 自由之路 . - 力扣#xff08;LeetCode#xff09;
题目的详细描述#xff0c;直接打开力扣看就是了#xff0c;下面说一下我对题目的理解:
事例1#xff1a;
输入: ring godding, key gd
输出: 4.
1. ring的第…题目: 力扣514 自由之路 . - 力扣LeetCode
题目的详细描述直接打开力扣看就是了下面说一下我对题目的理解:
事例1
输入: ring godding, key gd
输出: 4.
1. ring的第一个字符默认是指向12点方向的这一点很重要
2. key的第一个字符为g而ring中首字符和末尾字符都为g。因此必然存在选择首字符的g还是末尾字符g的问题。
3. 即使ring中第一个字符为g也还存在存在顺时针旋转和逆时针旋转的问题。(当然当前案例比较极端如果第一个字符不为g理解起来更合适)
4. 这一题要求的是旋转的最小步数。因此最终的值必然是获取最小值的。
5. 那么整体串起来分析 ring godding, key gd 字符godding下标0123456
a1. 如果ring的第一个g旋转顺时针步数为0逆时针步数为7算上最终确认的1步因此就 是在 1 和 8中选取最小值选取1
a2. 接着a1继续分析既然key的第一个字符已经确定了。那么key的第二个字符d又该选择 了。我们到底是用下标为2的d呢还是使用下标为3的d呢
选择1 下标为2的d从a1的g顺时针旋转只需要2步逆时针旋转需要5步。算上确认的1步就是在3和6中选取最小值选取3
选择2 下标为3的d, 从a1的g顺时针旋转只需要3步逆时针旋转需要4步。算上确认的1步就是在4和5中选取最小值. 选取 4
如果g使用的是ring中第一个字符针对以上2种选择最好的选择就是使用下标为2的d顺时针旋转2步即3步。 那么总的代价就是 1 3 4。 开头我们就说过ring中有开头和结尾都存在g因此存在选择的问题。
b1. 如果我们使用ring中下标为6的g最为key的开头。顺时针旋转需要1步逆时针旋转需要6步算上最终确认的1步就是2步和7步。选取最小值2.
b2. 接着b1继续分析既然key的第一个字符已经确定了。那么key的第二个字符d又该选择 了。我们到底是用下标为2的d呢还是使用下标为3的d呢 选择1 下标为2的d从b1的g顺时针旋转只需要4步逆时针旋转需要3步。算上确认的1步就是在5和4中选取最小值选取4
选择2 下标为3的d, 从b1的g顺时针旋转只需要3步逆时针旋转需要4步。算上确认的1步就是在4和5中选取最小值. 选取4
如果g使用的是ring中最后一个字符针对以上2种选择最好都为4。 那么总的代价就是 2 4 6。 最终如果以ring中第一个g作为旋转选择最小的步数为4; 以ring中最后一个g作为旋转选择那么最小步数为6 因此当前案例最小步数为 Math.min(4, 6). 递归代码
package 刷题.第三天;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** 力扣514 自由之路* https://leetcode.cn/problems/freedom-trail/*/
public class C2_FreeDomTtail_leet514_递归 {//递归版本public int findRotateSteps(String ring, String key) {if (ring null || ring.length() 0 || key null || key.length() 0) {return 0;}char[] source ring.toCharArray();char[] target key.toCharArray();//记录下每个字符的位置有可能存在重复值的情况HashMapCharacter, List map new HashMapCharacter, List();for (int i 0; i ring.length(); i) {if (map.containsKey(source[i])) {//放入下标的位置map.get(source[i]).add(i);}else {List list new ArrayList();list.add(i);map.put(source[i], list);}}return process(map, source, 0, target, 0);}public int process (MapCharacter, List map,char[] source, int sourceStartIndex,char[] target, int targetIndex) {if (targetIndex target.length) {return 0;}ListInteger ops map.get(target[targetIndex]);int minStep Integer.MAX_VALUE;for (int i 0; i ops.size(); i) {//从sourceStartIndex 到 ops.get(i) 最下步数; 1是确认按钮耗费的1步int step getMinSteps(sourceStartIndex, ops.get(i), source.length) 1;//深度优先遍历 此时source的的开始位置为 ops.get(i)minStep Math.min(minStep, step process(map, source, ops.get(i), target, targetIndex 1));}return minStep;}//获取从最小长度public int getMinSteps(int start, int end, int size){//如果start end, 则是顺时针; 反之 逆时针int step1 Math.abs(start - end);//如果step1是顺时针那么step则是逆时针; 反之顺时针int step2 size - step1;return Math.min(step1, step2);}public static void main(String[] args) {C2_FreeDomTtail_leet514_递归 ss new C2_FreeDomTtail_leet514_递归();String source godding;String target gd;System.out.println(ss.findRotateSteps(source, target));}
}测试结果 超时是好事情说明整体逻辑大概率是没有问题的。超时说明递归计算的次数有问题。上方的分析过程中a2和b2 都是针对d进行逻辑判断的明显存在重复的过程。那么就需要在递归的基础之上添加缓存了俗称记忆化搜索。
我之前在说动态规划的时候就说过如果表结构依赖不严格或者说即使依赖严格表结构但是没有优化的空间。 递归 缓存 动态规划。
递归缓存版本
package 刷题.第三天;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** 力扣514 自由之路* https://leetcode.cn/problems/freedom-trail/*/
public class C2_FreeDomTtail_leet514_递归_缓存 {//递归 缓存public int findRotateSteps(String ring, String key) {if (ring null || ring.length() 0 || key null || key.length() 0) {return 0;}char[] source ring.toCharArray();char[] target key.toCharArray();//记录下每个字符的位置有可能存在重复值的情况HashMapCharacter, List map new HashMapCharacter, List();for (int i 0; i ring.length(); i) {if (map.containsKey(source[i])) {//放入下标的位置map.get(source[i]).add(i);}else {List list new ArrayList();list.add(i);map.put(source[i], list);}}int[][] dp new int[source.length][target.length];for (int i 0; i source.length; i) {for (int j 0; j target.length; j) {//代表没算过dp[i][j] -1;}}return process(map, source, 0, target, 0, dp);}public int process (MapCharacter, List map,char[] source, int sourceStartIndex,char[] target, int targetIndex,int[][] dp) {if (targetIndex target.length) {return 0;}//缓存if (dp[sourceStartIndex][targetIndex] ! -1) {return dp[sourceStartIndex][targetIndex];}ListInteger ops map.get(target[targetIndex]);int minStep Integer.MAX_VALUE;for (int i 0; i ops.size(); i) {//从sourceStartIndex 到 ops.get(i) 最下步数; 1是确认按钮耗费的1步int step getMinSteps(sourceStartIndex, ops.get(i), source.length) 1;//深度优先遍历 此时source的的开始位置为 ops.get(i)minStep Math.min(minStep, step process(map, source, ops.get(i), target, targetIndex 1, dp));dp[sourceStartIndex][targetIndex] minStep;}return minStep;}//获取从最小长度public int getMinSteps(int start, int end, int size){//如果start end, 则是顺时针; 反之 逆时针int step1 Math.abs(start - end);//如果step1是顺时针那么step则是逆时针; 反之顺时针int step2 size - step1;return Math.min(step1, step2);}public static void main(String[] args) {C2_FreeDomTtail_leet514_递归_缓存 ss new C2_FreeDomTtail_leet514_递归_缓存();String source godding;String target gd;System.out.println(ss.findRotateSteps(source, target));}
}测试结果 84%的胜率8毫秒已经相当优秀了。其实这就是最优解 第三版本纯动态规划
动态规划那就需要分析递归的逻辑了。下面以ring作为行以key作为列
第一步
0 (g)1 (d)0 (g 下标0的g: 顺时针1步 逆时针8步 选取1步 1 (o)2 (d) 3 (d) 4 (i)5 (n)6 (g) 下标6的g : 顺时针2步 逆时针7步 选取2步 第二步
0 (g)1 (d)0 (g 下标0的g: 1步 1 (o)2 (d) 下标为2的d 从下标为0的g过来 顺时针2步逆时针5步 算上确认的1步 就是3步和6步选取小值即3步 从下标为6的g过来 顺时针3步逆时针4步 算上确认的1步 就是4步和5步选取小值即4步 最终值就是: 13 和 2 4选取小的。即4步 1是下标为0的g耗费的1步 2是下标为6的g耗费的2步 3 (d) 下标为3的d 从下标为0的g过来 顺时针3步逆时针4步 算上确认的1步 就是4步和5步选取小值即4步 从下标为6的g过来 顺时针4步逆时针3步 算上确认的1步 就是5步和4步选取小值即4步 最终值就是: 14 和 2 4选取小的。即5步 1是下标为0的g耗费的1步 2是下标为6的g耗费的2步 4 (i)5 (n)6 (g)下标6的g : 2步
因此最终最小的步数就是当key遍历完最后一个字符得到即 1 3 4步
纯动态规划
package 刷题.第三天;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** 力扣514 自由之路* https://leetcode.cn/problems/freedom-trail/*/
public class C2_FreeDomTtail_leet514_动态规划 {//纯动态规划public int findRotateSteps(String ring, String key) {if (ring null || ring.length() 0 || key null || key.length() 0) {return 0;}char[] source ring.toCharArray();char[] target key.toCharArray();//记录下每个字符的位置有可能存在重复值的情况HashMapCharacter, List map new HashMapCharacter, List();for (int i 0; i ring.length(); i) {if (map.containsKey(source[i])) {//放入下标的位置map.get(source[i]).add(i);}else {List list new ArrayList();list.add(i);map.put(source[i], list);}}int[][] dp new int[source.length][target.length 1];//最终返回的最小值int finalMinStep Integer.MAX_VALUE;//第一列ListInteger ops map.get(target[0]);for (int index : ops) {dp[index][0] getMinSteps(0, index, source.length) 1;//如果要拼接的key只有一个字符直接获取最小值即可finalMinStep Math.min(finalMinStep, dp[index][0]);}//如果要拼接的字符长度超过1那么finalMinStep的值需要//等到 target的最后一列才能确定if (target.length 1) {finalMinStep Integer.MAX_VALUE;}//列遍历从第二列开始往后遍历for (int i 1; i target.length ; i){//当前列对应的行信息ListInteger ops2 map.get(target[i]);//当前列前一列对应的行信息ListInteger ops3 map.get(target[i-1]);for (int j : ops2) //结束{//j行i列的默认最小值int minStep Integer.MAX_VALUE;for(int m : ops3) //开始{//从m行到j行的步数int curStep getMinSteps(m, j, source.length) 1;//dp[m][i-1] : 从0到m累计步数//dp[j][i-1] curStep 代表从0行到j行累计步数int steps dp[m][i-1] curStep;//更新j行i列的最小值minStep Math.min(minStep, steps);dp[j][i] minStep;}//要拼接字符串的最后一个字符也就是说可以//已经全部拼接完了if (i target.length - 1) {finalMinStep Math.min(finalMinStep, dp[j][i]);}}}return finalMinStep;}//获取从最小长度public int getMinSteps(int start, int end, int size){//如果start end, 则是顺时针; 反之 逆时针int step1 Math.abs(start - end);//如果step1是顺时针那么step则是逆时针; 反之顺时针int step2 size - step1;return Math.min(step1, step2);}public static void main(String[] args) {C2_FreeDomTtail_leet514_动态规划 ss new C2_FreeDomTtail_leet514_动态规划();/*String source godding;String target gd;*/String source eh;String target h;System.out.println(ss.findRotateSteps(source, target));}
}测试结果 10毫秒76%胜率也还行。 第四种解法即官方解法。因为胜率没有我写的两个版本的高我就不说了。
官方代码胜率
文章转载自: http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.gcqkb.cn.gov.cn.gcqkb.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.ggnjq.cn.gov.cn.ggnjq.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.tzpqc.cn.gov.cn.tzpqc.cn http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.kzcfr.cn.gov.cn.kzcfr.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.wmhqd.cn.gov.cn.wmhqd.cn http://www.morning.fkdts.cn.gov.cn.fkdts.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.mszwg.cn.gov.cn.mszwg.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.ssqwr.cn.gov.cn.ssqwr.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn http://www.morning.jcxgr.cn.gov.cn.jcxgr.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.swkpq.cn.gov.cn.swkpq.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.ymtbr.cn.gov.cn.ymtbr.cn http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.mm27.cn.gov.cn.mm27.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.qtryb.cn.gov.cn.qtryb.cn http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.fbdtd.cn.gov.cn.fbdtd.cn http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.whpsl.cn.gov.cn.whpsl.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.rydhq.cn.gov.cn.rydhq.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn