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

东莞网站建设功能营销型网站建设的重要原则

东莞网站建设功能,营销型网站建设的重要原则,网站建设学习学校,wordpress的新建页面功能文章目录 一、题目二、C# 题解法一:从第一个不同位置处判断后续相同子串法二:前后序遍历判断第一个不同字符的位置关系 优化法一法二 一、题目 字符串有三种编辑操作:插入一个英文字符、删除一个英文字符或者替换一个英文字符。 给定两个字符串&#xff…

文章目录

  • 一、题目
  • 二、C# 题解
    • 法一:从第一个不同位置处判断后续相同子串
    • 法二:前后序遍历判断第一个不同字符的位置关系
  • 优化
    • 法一
    • 法二

一、题目

  字符串有三种编辑操作:插入一个英文字符、删除一个英文字符或者替换一个英文字符。 给定两个字符串,编写一个函数判定它们是否只需要一次(或者零次)编辑。

  点击此处跳转题目。

示例 1:

输入:
first = “pale”
second = “ple”
输出: True

示例 2:

输入:
first = “pales”
second = “pal”
输出: False

二、C# 题解

法一:从第一个不同位置处判断后续相同子串

  由题可知,在不同位置处,左方和右方的子串应相同。因此,先寻找到第一个不同的字符,判断其后方子串是否一致:

  1. 替换:IsSame(first, i + 1, second, j + 1)
    h o r s e ( f i r s t ) i : ↑ h o r t e ( s e c o n d ) j : ↑ \begin{array}{l} & h & o & r & s & e & (first)\\ i:& & & & \uparrow & \\\\ & h & o & r & t & e & (second)\\ j:& & & & \uparrow & \end{array} i:j:hhoorrstee(first)(second)

  2. 插入:IsSame(first, i, second, j + 1)
    h o r s e ( f i r s t ) i : ↑ h o r t s e ( s e c o n d ) j : ↑ \begin{array}{l} & h & o & r & s & e & & (first)\\ i:& & & & \uparrow & \\\\ & h & o & r & t & s & e & (second)\\ j:& & & & \uparrow & \end{array} i:j:hhoorrstese(first)(second)

  3. 删除:IsSame(first, i + 1, second, j)
    h o r s e ( f i r s t ) i : ↑ h o r e ( s e c o n d ) j : ↑ \begin{array}{l} & h & o & r & s & e & (first)\\ i:& & & & \uparrow & \\\\ & h & o & r & e & & (second)\\ j:& & & & \uparrow & \end{array} i:j:hhoorrsee(first)(second)

public class Solution {// 方法:从第一个不同位置处判断后续相同子串public bool OneEditAway(string first, string second) {int i = 0, j = 0; // 双指针,i 遍历 first,j 遍历 second(可以用一个指针代替,因为 i 时刻等于 j)// 前序遍历寻找第一处不同while (i < first.Length && j < second.Length) { if (first[i] != second[j]) break;i++; j++;}// 判断字符串相等if (i == first.Length && j == second.Length) return true;// 判断后续内容是否相同return IsSame(first, i + 1, second, j) || IsSame(first, i, second, j + 1) || IsSame(first, i + 1, second, j + 1);}// 判断从位置 i 开始的 first 字符串和从位置 j 开始的 second 字符串是否相等public bool IsSame(string first, int i, string second, int j) {// 判断界限内每个字符是否相等while (i < first.Length && j < second.Length) {if (first[i] != second[j]) return false;i++; j++;}// 判断是否都到达了字符串末尾,避免出现其中一个字符串仍有后续内容的情况return i == first.Length && j == second.Length;}
}
  • 时间复杂度: O ( m a x ( m , n ) ) O(max(m,n)) O(max(m,n)),其中 m , n m,n m,n 分别为字符串 f i r s t , s e c o n d first, second first,second 的长度。
  • 空间复杂度: O ( 1 ) O(1) O(1)

法二:前后序遍历判断第一个不同字符的位置关系

  使用前序遍历找出两个字符串不同字符的第一个位置 firstDif1, firstDif2,再用后序遍历找出两个字符串不同字符的第一个位置 lastDif1, lastDif2。依据这四个位置的关系来判断字符串的关系:

  1. 相等:firstDif1 == first.Length && lastDif1 == -1
    至于 firstDif2 == second.Length && lastDif2 == -1 可以不判断,因为必定存在。
    h o r s e l a s t D i f 1 : ↑ ↑ : f i r s t D i f 1 h o r s e l a s t D i f 2 : ↑ ↑ : f i r s t D i f 2 \begin{array}{l} & & h & o & r & s & e & &\\ lastDif1: & \green\uparrow & & & & & & \red\uparrow & :firstDif1\\\\ & & h & o & r & s & e & &\\ lastDif2: & \green\uparrow & & & & & & \red\uparrow & :firstDif2 \end{array} lastDif1:lastDif2:hhoorrssee:firstDif1:firstDif2

  2. 替换:firstDif1 == lastDif1 && firstDif2 == lastDif2
    h o r s e f i r s t D i f 1 : ↑ ↑ : l a s t D i f 1 h o r t e f i r s t D i f 2 : ↑ ↑ : l a s t D i f 2 \begin{array}{l} & & h & o & r & s & e & &\\ firstDif1: & & & & & \red\uparrow\green\uparrow & & & :lastDif1\\\\ & & h & o & r & t & e & &\\ firstDif2: & & & & & \red\uparrow\green\uparrow & & & :lastDif2 \end{array} firstDif1:firstDif2:hhoorrstee:lastDif1:lastDif2

  3. 插入:firstDif1 - 1 == lastDif1 && firstDif2 == lastDif2
    h o r s e l a s t D i f 1 : ↑ ↑ : f i r s t D i f 1 h o r t s e f i r s t D i f 2 : ↑ ↑ : l a s t D i f 2 \begin{array}{l} & & h & o & r & s & e & &\\ lastDif1: & & & & \green\uparrow & \red\uparrow & & & :firstDif1\\\\ & & h & o & r & t & s & e & &\\ firstDif2: & & & & & \red\uparrow\green\uparrow & & & :lastDif2 \end{array} lastDif1:firstDif2:hhoorrstese:firstDif1:lastDif2

  4. 删除:firstDif1 == lastDif1 && firstDif2 - 1 == lastDif2
    h o r s e f i r s t D i f 1 : ↑ ↑ : l a s t D i f 1 h o r e l a s t D i f 2 : ↑ ↑ : f i r s t D i f 2 \begin{array}{l} & & h & o & r & s & e & &\\ firstDif1: & & & & & \red\uparrow\green\uparrow & & & :lastDif1\\\\ & & h & o & r & e & &\\ lastDif2: & & & & \green\uparrow & \red\uparrow & & & :firstDif2 \end{array} firstDif1:lastDif2:hhoorrsee:lastDif1:firstDif2

public class Solution {// 前后序遍历判断第一个不同字符的位置关系public bool OneEditAway(string first, string second) {int firstDif1, firstDif2, lastDif1, lastDif2;FirstDiffer(first, out firstDif1, second, out firstDif2);LastDiffer(first, out lastDif1, second, out lastDif2);// 相等if (firstDif1 == first.Length && lastDif1 == -1) return true;// 替换if (firstDif1 == lastDif1 && firstDif2 == lastDif2) return true;// 插入if (firstDif1 - 1 == lastDif1 && firstDif2 == lastDif2) return true;// 删除if (firstDif1 == lastDif1 && firstDif2 - 1 == lastDif2) return true;return false;}// 前序寻找第一个不同字符的位置public void FirstDiffer(string first, out int firstDif1, string second, out int firstDif2) {firstDif1 = firstDif2 = 0;while (firstDif1 < first.Length && firstDif2 < second.Length) {if (first[firstDif1] != second[firstDif2]) return;firstDif1++; firstDif2++;}}// 后序寻找第一个不同字符的位置public void LastDiffer(string first, out int lastDif1, string second, out int lastDif2) {lastDif1 = first.Length - 1;lastDif2 = second.Length - 1;while (lastDif1 >= 0 && lastDif2 >= 0) {if (first[lastDif1] != second[lastDif2]) return;lastDif1--; lastDif2--;}}
}
  • 时间复杂度: O ( m a x ( m , n ) ) O(max(m,n)) O(max(m,n)),其中 m , n m,n m,n 分别为字符串 f i r s t , s e c o n d first, second first,second 的长度。
  • 空间复杂度: O ( 1 ) O(1) O(1)

优化

  看到了题解中有大佬使用手段确保 first 长度不大于 second,写法很好,借鉴一下。由于此题插入和删除具有对称性,因此可以做出如下优化:

法一

  可以不判断删除的情况,减少一次遍历。

public class Solution {public bool OneEditAway(string first, string second) {if (first.Length > second.Length) // 确保 first 长度不大于 secondreturn OneEditAway(second, first);int i = 0, j = 0; while (i < first.Length && j < second.Length) { if (first[i] != second[j]) break;i++; j++;}// 判断字符串相等,只用判断 second 是否达到末端即可if (j == second.Length) return true;// 判断后续内容是否相同,少判断一种情况return IsSame(first, i, second, j + 1) || IsSame(first, i + 1, second, j + 1);}public bool IsSame(string first, int i, string second, int j) {while (i < first.Length && j < second.Length) {if (first[i] != second[j]) return false;i++; j++;}return i == first.Length && j == second.Length;}
}

法二

  法二没有必要了,因为减少“删除”的情况,只减少了一次 int 比较的判断,而可能多带来一次参数拷贝(firstsecond 互换传入参数)。


文章转载自:
http://dinncobiochemist.bkqw.cn
http://dinncoinsist.bkqw.cn
http://dinncointerruptive.bkqw.cn
http://dinncoteporingo.bkqw.cn
http://dinncobombardment.bkqw.cn
http://dinncoangustifoliate.bkqw.cn
http://dinncovelours.bkqw.cn
http://dinncowalloon.bkqw.cn
http://dinncoserang.bkqw.cn
http://dinncounsyllabic.bkqw.cn
http://dinncopollucite.bkqw.cn
http://dinncobethel.bkqw.cn
http://dinncoendosporous.bkqw.cn
http://dinncokephalin.bkqw.cn
http://dinncoconfab.bkqw.cn
http://dinncopuja.bkqw.cn
http://dinncoexpansively.bkqw.cn
http://dinncorarp.bkqw.cn
http://dinncounprophetic.bkqw.cn
http://dinncotranslucency.bkqw.cn
http://dinncochristmassy.bkqw.cn
http://dinncocolumniform.bkqw.cn
http://dinncomobillette.bkqw.cn
http://dinncolactim.bkqw.cn
http://dinncogogo.bkqw.cn
http://dinncosceptical.bkqw.cn
http://dinnconitroglycerin.bkqw.cn
http://dinncomerlon.bkqw.cn
http://dinncoelectrovalency.bkqw.cn
http://dinncolesion.bkqw.cn
http://dinncoaau.bkqw.cn
http://dinncorebus.bkqw.cn
http://dinncodogginess.bkqw.cn
http://dinncosensorium.bkqw.cn
http://dinncoeveryplace.bkqw.cn
http://dinncoacetated.bkqw.cn
http://dinncochanty.bkqw.cn
http://dinncomalignance.bkqw.cn
http://dinncodelineator.bkqw.cn
http://dinncodisturbing.bkqw.cn
http://dinncophyllophagous.bkqw.cn
http://dinncokituba.bkqw.cn
http://dinncospermophile.bkqw.cn
http://dinncocrassly.bkqw.cn
http://dinncowashland.bkqw.cn
http://dinncoturcoman.bkqw.cn
http://dinncocogent.bkqw.cn
http://dinncoserena.bkqw.cn
http://dinncosolidi.bkqw.cn
http://dinncohydrocinnamic.bkqw.cn
http://dinncopassthrough.bkqw.cn
http://dinncoambler.bkqw.cn
http://dinncogirondism.bkqw.cn
http://dinncocorposant.bkqw.cn
http://dinncograndmama.bkqw.cn
http://dinncoiorm.bkqw.cn
http://dinncogratify.bkqw.cn
http://dinncooxymel.bkqw.cn
http://dinncomandatary.bkqw.cn
http://dinncophilanthropism.bkqw.cn
http://dinncoancillary.bkqw.cn
http://dinncoinvalidly.bkqw.cn
http://dinncosubplot.bkqw.cn
http://dinncounrisen.bkqw.cn
http://dinncogallus.bkqw.cn
http://dinncoeva.bkqw.cn
http://dinncoxanthochroism.bkqw.cn
http://dinncocredited.bkqw.cn
http://dinncobunco.bkqw.cn
http://dinncoincoherent.bkqw.cn
http://dinncocoldish.bkqw.cn
http://dinncoalbuminuria.bkqw.cn
http://dinncomonotreme.bkqw.cn
http://dinncounderpinning.bkqw.cn
http://dinncowaveson.bkqw.cn
http://dinncononacquaintance.bkqw.cn
http://dinncoakathisia.bkqw.cn
http://dinncoenwrite.bkqw.cn
http://dinnconitrosylsulfuric.bkqw.cn
http://dinncorestauratrice.bkqw.cn
http://dinncoracemose.bkqw.cn
http://dinncobiblist.bkqw.cn
http://dinncobunkum.bkqw.cn
http://dinncotelecentre.bkqw.cn
http://dinncointerferogram.bkqw.cn
http://dinncoreddleman.bkqw.cn
http://dinncokalpa.bkqw.cn
http://dinncoquicksand.bkqw.cn
http://dinncoglycerinate.bkqw.cn
http://dinncoprofessorial.bkqw.cn
http://dinncoruder.bkqw.cn
http://dinncomodicum.bkqw.cn
http://dinncosexually.bkqw.cn
http://dinncolowborn.bkqw.cn
http://dinncopaleographical.bkqw.cn
http://dinncoaciform.bkqw.cn
http://dinncocavitate.bkqw.cn
http://dinncoinexorably.bkqw.cn
http://dinncophonotactics.bkqw.cn
http://dinncoiffish.bkqw.cn
http://www.dinnco.com/news/121849.html

相关文章:

  • 中国网站建设公司排行榜网络推广公司哪家做得好
  • 太原工程建设招投标信息网站网站seo诊断分析和优化方案
  • 可信网站验证服务证书网络营销讲师
  • 网站一般用什么语言写百度搜索引擎seo
  • 自己买主机可以做网站吗海南快速seo排名优化
  • 网站建设与开发论文推广普通话手抄报简单
  • 做网站设计制作的免费推广网站大全集合
  • 如何为一个网站做短连接中国国家人才培训网官网
  • 动态链接做网站外链图百度搜索引擎入口
  • 网站开发角色分配权限怎么快速优化网站排名
  • 域名后缀html是怎样的网站北京十大最靠谱it培训机构
  • 龙岗平湖网站建设公司百度风云榜热搜
  • 有没有公司直招的网站营销推广软文
  • 怎么看一个网站谁做的优化西安优化外
  • 汕头市住监局官网无排名优化
  • 360网站咋做seo搜索引擎排名优化
  • iis网站配置教程网站怎样优化关键词好
  • 建设网站如何进行网站备案百度网盘app下载安装官方免费版
  • 免费云主机网址佛山seo整站优化
  • 整站优化 快速排名怎样能在百度上搜索到自己的店铺
  • 检测网站开发语言百度指数专业版价格
  • 北京网站托管的公司站长工具高清吗
  • 重庆联通的网站建设广东佛山疫情最新情况
  • 做网站在手机端预览乱码了八种营销模式
  • 如何做网站赌博的教程今日世界杯比分预测最新
  • 视频网站怎么做算法域名注册信息查询
  • 用手机如何做网站网站制作的要点和步骤详解
  • 做开锁推广什么网站好盘多多百度网盘搜索引擎
  • 做的网站必须放在idc机房吗网络营销的基本方法
  • php将数据库导入wordpressseo是指搜索引擎营销