本文共 1226 字,大约阅读时间需要 4 分钟。
大家好,我是小黄呀
class Solution { public: bool oneEditAway(string first, string second) { if(first==second){ //字符串相等 return true; } const int len1=first.size(); const int len2=second.size(); if(abs(len1-len2)>1){ //两者长度相差大于1 return false; } int i=0,j=len1-1,k=len2-1; //双指针扫描 while(i=0 && k>=0 && first[j]==second[k]){ // j、k从右至左扫描 --j; --k; } return j-i<1 && k-i<1;//判定剩余长度 }};
string compressString(string S) { int N = S.length(); string res; int i = 0; while (i < N) { int j = i; while (j < N && S[j] == S[i]) { j++; } res += S[i]; res += to_string(j - i); i = j; } if (res.length() < S.length()) { return res; } else { return S; }}
转载地址:http://gzhtz.baihongyu.com/