leetcode 680. Valid Palindrome II 去除一个字符的回文字符串判断 + 双指针
2017-12-23 15:06
441 查看
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
Example 1:
Input: “aba”
Output: True
Example 2:
Input: “abca”
Output: True
Explanation: You could delete the character ‘c’.
Note:
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
本题题意很简单,要是直接暴力的删除字符会超时,所以直接跳过即可
代码如下:
Example 1:
Input: “aba”
Output: True
Example 2:
Input: “abca”
Output: True
Explanation: You could delete the character ‘c’.
Note:
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
本题题意很简单,要是直接暴力的删除字符会超时,所以直接跳过即可
代码如下:
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> using namespace std; class Solution { public: bool validPalindrome(string s) { int left = 0, right = s.length() - 1; while (left < right) { if (s[left] == s[right]) { left++; right--; } else { return isPalindrome(s, left, right - 1) || isPalindrome(s, left + 1, right); } } return true; } bool isPalindrome(string s,int left,int right) { while (left < right) { if (s[left] == s[right]) { left++; right--; } else return false; } return true; } };
相关文章推荐
- 【leetcode】对撞指针应用之回文字符串判断(忽略大小写,以及出数字外其他字符)
- 写一个少于 80 字符的函数,判断一个字符串是不是回文字符串
- 算法基础——判断一个字符串至多添加一个字符之后是否回文
- python判断是否可以只插一个字符,让字符串变成回文,返回true/false
- 判断一个字符串是否能够通过添加一个字符变成回文序列的方法
- 输入一个字符串,判断该字符串插入一个字符,能否使其成为回文字符串
- 判断一个字符串通过变化字符的位置,是否可以组成回文
- 判断一个字符串(超过80个字符)是否是回文结构(正序和逆序相同)
- 如何判断一个字符串中的字符是否唯一
- 判断一个字符串是否是回文
- 判断一个字符串出现次数最多的字符,统计该字符出现的次数
- c# 判断一个字符串列表中是否已经包括指定字符
- 如何判断一个字符串是否仅由数字or仅由数字英文字符or仅由汉字组成
- 给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写。
- 判断一个字符串里是否含有某段字符?怎么截取一段字符?
- 1.判断一个字符串中的字符是否唯一(CC150)---编程笔记
- 判断一个字符是否包含在另一字符串中
- 判断一个字符串是否回文
- 判断一个字符串是不是回文
- Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)