Leetcode贪心 验证回文字符串
生活随笔
收集整理的這篇文章主要介紹了
Leetcode贪心 验证回文字符串
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Given a string s, return true if the s can be palindrome after deleting at most one character from it.
思路
用頭尾指針遍歷原字符串,但碰到所指不相同時,需要退出循環(huán)記錄此書指針的位置。分別去除兩個指針上的內容,查看刪除一個字符后的字符串是否為回文串。
代碼
class Solution { public:bool validPalindrome(string s) {int len = s.size();int head = 0, tail = len-1;while(head < tail){if(s[head] != s[tail])break;head++;tail--;}if(ifpalindrome(s, head))return true;else if(ifpalindrome(s, tail))return true;return false;}bool ifpalindrome(string s, int index){s.erase(index, 1);int head = 0, tail = s.size()-1;while(head < tail){if(s[head] != s[tail])return false;head++;tail--;}return true;} };總結
以上是生活随笔為你收集整理的Leetcode贪心 验证回文字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中央空调怎么调节温度 中央空调调节温度方
- 下一篇: Leetcode动态规划 不同路径