当前位置: 首页> 娱乐> 影视 > LeetCode 125.验证回文串 C++写法

LeetCode 125.验证回文串 C++写法

时间:2025/7/8 5:29:22来源:https://blog.csdn.net/m0_63816268/article/details/140614637 浏览次数:0次

LeetCode 125.验证回文串 C++写法

image-20240721123800589

思路🤔:

  我们不对字符串进行删除,这样效率太低了,所以可以左右开工,下标begin和end遇到不是字母数字字符的就跳过,当两边都是字母就进行比对,一样就继续往后走,不一样就返回false。

代码🔎:

class Solution {
public:bool isString(char& s){if(s >= 'a' && s <= 'z')return true;if(s >= 'A' && s <= 'Z') //顺便将大写改为小写{s += 32;return true;}if(s >= '0' && s <= '9')return true;return false;}bool isPalindrome(string s) {int i = 0;int end = s.size() - 1;int begin = 0;if(s.empty()) //为空直接返回return true;while(begin < end){while(begin < end && !isString(s[begin])) //不为数字字母就跳过{begin++;}while(begin < end && !isString(s[end])){end--;}if(s[begin] == s[end]) //相等,继续往后走{begin++;end--;}else{return false; //不相等就返回}}return true;}
};

image-20240721130356681

关键字:LeetCode 125.验证回文串 C++写法

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: