leetcode|面试题 01.05. 一次编辑【模拟、字符串】

题目描述leetcode|面试题 01.05. 一次编辑【模拟、字符串】_第1张图片

解题思路

题意分析

根据题意,两字符串 first , second 能够通过一次(或者零次)编辑互相转换的条件为:

两字符串 first , second 的长度之差 ≤1 ;
当 first , second 长度相等时,两字符串各对应位置只有一个字符不同;
当 first , second 长度之差为 1 时,较长的字符串只比较短的的字符串多一个字符。

代码设计

  • 首先对长度进行判断,区分不同的长度
  • 然后分别对以上三种情况进行判断即可。

原始代码

class Solution {
public:
   bool oneEditAway(string first, string second) {
        if (first == second) {//相等 没有改变
            return true;
        }
        int cha = first.length() - second.length();
        if (abs(cha) > 1) {// 两个字符串相差的长度大于1 不满足提议
            return false;
        }
        else if (cha ==1 )// 插入或者删除 firts 多一个
        {
            int start_long = 0;
            while ( start_long < second.length())
            {
                if (first[start_long] == second[start_long]) {
                    start_long++;
                }
                else {
                    return first.substr(start_long+1) == second.substr(start_long);          
                }
            }
            return true;
        }
        else if (cha == -1)
        {
            int start_long = 0;
            while (start_long < first.length())
            {
                if (first[start_long] == second[start_long]) {
                    start_long++;
                }
                else {
                    return second.substr(start_long + 1) == first.substr(start_long);
                }
            }
            return true;
        }
        else {// 修改
            int start_long = 0;
            while (start_long < first.length())
            {
                if (first[start_long] == second[start_long]) {
                    start_long++;
                }
                else {
                    return second.substr(start_long + 1) == first.substr(start_long+1);
                }
            }
            return true;
        }
        return false;
    }
};

进行化简

-可以通过函数调用进行化简 减少代码行数。具体如代码

class Solution {
public:
    bool oneEditAway(string first, string second) {
        if (first == second) {//相等 没有改变
            return true;
        }
        int cha = first.length() - second.length();

        // 是第一个字符串永远长于第二个字符串
        if (cha < 0) { 
            return oneEditAway(second, first);
        }
        else if (cha > 1) {// 两个字符串相差的长度大于1 不满足提议
            return false;
        }
        else if (cha ==1 )// 插入或者删除 firts 多一个
        {
            int start_long = 0;
            while ( start_long < second.length())
            {
                if (first[start_long] == second[start_long]) {
                    start_long++;
                }
                else {
                    return first.substr(start_long + 1) == second.substr(start_long);
                }
            }
            return true;
        }
        else {// 修改
            int start_long = 0;
            while (start_long < first.length())
            {
                if (first[start_long] == second[start_long]) {
                    start_long++;
                }
                else {
                    return second.substr(start_long + 1) == first.substr(start_long + 1);
                }
            }
            return true;
        }
        return false;       
    }
};

知识沉淀

注意在代码具有复用的可能性的时候,尽量考虑代码复用

你可能感兴趣的:(leetcode,leetcode,算法,职场和发展)