南京网站设计网站,天津网站优化公司价格,带产品列表 wordpress,西安网站建设公司咪豆文章目录 一、题目二、C# 题解法一#xff1a;从第一个不同位置处判断后续相同子串法二#xff1a;前后序遍历判断第一个不同字符的位置关系 优化法一法二 一、题目 字符串有三种编辑操作:插入一个英文字符、删除一个英文字符或者替换一个英文字符。 给定两个字符串#xff… 文章目录 一、题目二、C# 题解法一从第一个不同位置处判断后续相同子串法二前后序遍历判断第一个不同字符的位置关系 优化法一法二  一、题目 字符串有三种编辑操作:插入一个英文字符、删除一个英文字符或者替换一个英文字符。 给定两个字符串编写一个函数判定它们是否只需要一次(或者零次)编辑。 点击此处跳转题目。 
示例 1: 输入: first  “pale” second  “ple” 输出: True 示例 2: 输入: first  “pales” second  “pal” 输出: False 二、C# 题解 
法一从第一个不同位置处判断后续相同子串 由题可知在不同位置处左方和右方的子串应相同。因此先寻找到第一个不同的字符判断其后方子串是否一致 替换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:hhoorrs↑t↑ee(first)(second)  插入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:hhoorrs↑t↑ese(first)(second)  删除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:hhoorrs↑e↑e(first)(second)  
public class Solution {// 方法从第一个不同位置处判断后续相同子串public bool OneEditAway(string first, string second) {int i  0, j  0; // 双指针i 遍历 firstj 遍历 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。依据这四个位置的关系来判断字符串的关系 相等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  替换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:hhoorrs↑↑t↑↑ee:lastDif1:lastDif2  插入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:hhoor↑rs↑t↑↑ese:firstDif1:lastDif2  删除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:hhoorr↑s↑↑e↑e: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 比较的判断而可能多带来一次参数拷贝first 和 second 互换传入参数。 文章转载自: http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn http://www.morning.btpzn.cn.gov.cn.btpzn.cn http://www.morning.qgghj.cn.gov.cn.qgghj.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn http://www.morning.nqlkb.cn.gov.cn.nqlkb.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.ylsxk.cn.gov.cn.ylsxk.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.yqsq.cn.gov.cn.yqsq.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.trsdm.cn.gov.cn.trsdm.cn http://www.morning.zzfqn.cn.gov.cn.zzfqn.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.cykqb.cn.gov.cn.cykqb.cn http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.snnb.cn.gov.cn.snnb.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.dbfwq.cn.gov.cn.dbfwq.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.kdbcx.cn.gov.cn.kdbcx.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.hhqtq.cn.gov.cn.hhqtq.cn http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.pwppk.cn.gov.cn.pwppk.cn http://www.morning.stbfy.cn.gov.cn.stbfy.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.rahllp.com.gov.cn.rahllp.com http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.mdtfh.cn.gov.cn.mdtfh.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.ldspj.cn.gov.cn.ldspj.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn