Leetcode 面试题 01.05. 一次编辑

思路

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
bool oneEditAway(string first, string second) {
if(first.length()==second.length()){
int l=first.length();
int res=0;
for(int i=0;i<l;i++){
if(first[i]!=second[i]){
res++;
}
}
if(res>1)return false;
return true;
}else{
int sub=first.length()-second.length();
if(sub>1||sub<-1) return false;
else if(first==""||second=="") return true;
int p1=0,p2=0;
if(sub==-1){
string s=first;
first=second;
second=s;
}
while(p1<first.length()&&p2<second.length()){
if(first[p1]!=second[p2]){
p1++;
}else{
p1++;p2++;
}
}
cout<<"first:"<<first<<endl<<"second:"<<second<<endl;
if(p1!=p2&&(p1!=first.length()||p2!=second.length())) return false;
return true;
}
}
};

参考