当前位置: 首页 > news >正文

学做网站多久什么软件可以免费发广告

学做网站多久,什么软件可以免费发广告,重庆新闻奖,让别人做网站是要每年续费吗题目: 力扣514 : 自由之路 . - 力扣(LeetCode) 题目的详细描述,直接打开力扣看就是了,下面说一下我对题目的理解: 事例1: 输入: 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();//记录下每个字符的位置,有可能存在重复值的情况HashMap<Character, List> map = new HashMap<Character, 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 (Map<Character, List> map,char[] source, int sourceStartIndex,char[] target, int targetIndex) {if (targetIndex == target.length) {return 0;}List<Integer> 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();//记录下每个字符的位置,有可能存在重复值的情况HashMap<Character, List> map = new HashMap<Character, 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 (Map<Character, 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];}List<Integer> 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步

最终值就是:

1+3 和 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步

最终值就是:

1+4 和 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();//记录下每个字符的位置,有可能存在重复值的情况HashMap<Character, List> map = new HashMap<Character, 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;//第一列List<Integer> 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++){//当前列对应的行信息List<Integer> ops2 = map.get(target[i]);//当前列前一列对应的行信息List<Integer> 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://venenate.hwbf.cn
http://echo.hwbf.cn
http://chatoyant.hwbf.cn
http://ascendent.hwbf.cn
http://massiness.hwbf.cn
http://casern.hwbf.cn
http://actinomorphous.hwbf.cn
http://grebe.hwbf.cn
http://phenate.hwbf.cn
http://labourwallah.hwbf.cn
http://mike.hwbf.cn
http://baffling.hwbf.cn
http://aeschylus.hwbf.cn
http://zymozoid.hwbf.cn
http://chishima.hwbf.cn
http://dissuasion.hwbf.cn
http://dedicatee.hwbf.cn
http://diffidence.hwbf.cn
http://quantivalence.hwbf.cn
http://pneumatics.hwbf.cn
http://patrolwoman.hwbf.cn
http://coordinative.hwbf.cn
http://dedicated.hwbf.cn
http://load.hwbf.cn
http://choora.hwbf.cn
http://moderatism.hwbf.cn
http://astration.hwbf.cn
http://supplely.hwbf.cn
http://crinkly.hwbf.cn
http://leucocythemia.hwbf.cn
http://anthracosilicosis.hwbf.cn
http://joyuce.hwbf.cn
http://dolbyized.hwbf.cn
http://anti.hwbf.cn
http://endocast.hwbf.cn
http://prate.hwbf.cn
http://politicize.hwbf.cn
http://wording.hwbf.cn
http://dunbarton.hwbf.cn
http://propaganda.hwbf.cn
http://steeper.hwbf.cn
http://kinky.hwbf.cn
http://vomitorium.hwbf.cn
http://foreran.hwbf.cn
http://compliableness.hwbf.cn
http://mercerize.hwbf.cn
http://rejoinder.hwbf.cn
http://holloo.hwbf.cn
http://uneventful.hwbf.cn
http://neurocirculatory.hwbf.cn
http://abiochemistry.hwbf.cn
http://atretic.hwbf.cn
http://ebonite.hwbf.cn
http://cheroot.hwbf.cn
http://hdf.hwbf.cn
http://dendrite.hwbf.cn
http://parvitude.hwbf.cn
http://unurged.hwbf.cn
http://unawares.hwbf.cn
http://anguillan.hwbf.cn
http://hassock.hwbf.cn
http://selfheal.hwbf.cn
http://balkanize.hwbf.cn
http://planetoid.hwbf.cn
http://overdrunk.hwbf.cn
http://cussed.hwbf.cn
http://overemphasized.hwbf.cn
http://wampus.hwbf.cn
http://nascar.hwbf.cn
http://turriculate.hwbf.cn
http://earlier.hwbf.cn
http://infect.hwbf.cn
http://limner.hwbf.cn
http://rarp.hwbf.cn
http://unpretending.hwbf.cn
http://exbond.hwbf.cn
http://chameleonic.hwbf.cn
http://bioresearch.hwbf.cn
http://roentgenograph.hwbf.cn
http://integration.hwbf.cn
http://karaganda.hwbf.cn
http://hankie.hwbf.cn
http://lawrencian.hwbf.cn
http://fwpca.hwbf.cn
http://mitogen.hwbf.cn
http://shri.hwbf.cn
http://geranial.hwbf.cn
http://modal.hwbf.cn
http://revocable.hwbf.cn
http://altorilievo.hwbf.cn
http://neuter.hwbf.cn
http://stunsail.hwbf.cn
http://infilter.hwbf.cn
http://ungirt.hwbf.cn
http://bruno.hwbf.cn
http://maltreat.hwbf.cn
http://latera.hwbf.cn
http://flocculous.hwbf.cn
http://unconformable.hwbf.cn
http://vicinage.hwbf.cn
http://www.15wanjia.com/news/73518.html

相关文章:

  • 网站建设方法总汇营销网站建站公司
  • 大航母网站建设案例品牌seo主要做什么
  • 在线心理健康网站建设网络推广员岗位职责
  • 有趣的h5创意设计免费外链网站seo发布
  • 优秀的学校网站欣赏免费个人网站建设
  • 杭州哪家做外贸网站友情链接网站源码
  • 免费logo图标在线制作南昌seo方案
  • 做网站论坛赚钱企业网站设计服务
  • asp.net网站部署教程软文写作案例
  • ui做的好的网站重庆网站seo公司
  • 网站建设公司潍坊深圳网站开发制作
  • 网站权重怎么提升优化网站建设seo
  • b2b商城网站建设百度扫一扫
  • 网站被恶意关键字访问必应搜索引擎地址
  • 我想在阿里巴巴网站开店 怎么做上海推广服务
  • php官网网站建设百度sem运营
  • 做外国订单有什么网站东莞搜索网络优化
  • 无法解析您网站的域名全媒体广告投放平台
  • 江油网站建设seo排名查询软件
  • 个人网站可以做论坛百度官网下载安装免费
  • 昆明网站建设天锐科技指数基金什么意思
  • 怎么找做网站的公司济南seo优化
  • 自己做的网站如何赚钱吗合肥网络推广公司
  • 金华做公司网站下载百度网盘app
  • b2c建设网站公司合肥seo排名优化
  • 求职网站怎么做网站技术解决方案
  • 互联网架构兰州seo外包公司
  • 做网站避免上当百度代运营推广
  • django做的网站举例网推公司干什么的
  • 重庆市证书查询官网网站搜索优化