leetcode 680. Valid Palindrome II
来源:互联网 发布:asmr德叔是哪国人 知乎 编辑:程序博客网 时间:2024/06/08 09:56
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.
本题题意很简单,要是直接暴力的删除字符会超时,所以直接跳过即可
代码如下:
#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; }};
阅读全文
0 0
- leetcode 680. Valid Palindrome II
- leetcode 680. Valid Palindrome II
- leetcode题解-125. Valid Palindrome && 680. Valid Palindrome II
- Leetcode-680. Valid Palindrome II(判断回文)
- Leetcode 680 Valid Palindrome II
- leetcode 680 Valid Palindrome II
- 680. Valid Palindrome II
- 680. Valid Palindrome II
- 680. Valid Palindrome II
- 680. Valid Palindrome II
- 680. Valid Palindrome II
- 680. Valid Palindrome II
- 【leetcode】680. Valid Palindrome II(Python & C++)
- LWC 50:680. Valid Palindrome II
- Valid Palindrome II
- leetcode -day13 Valid Palindrome & Triangle & Pascal's Triangle I II
- Leetcode算法学习日志-680 valid Palindrome II
- [Leetcode] Valid Palindrome
- 修改启动图片和等待时间
- Ubuntu下 正确安装VMware Tools
- 数据结构实验之查找六:顺序查找
- cxz
- LC-Next Greater Element I
- leetcode 680. Valid Palindrome II
- CSDN添加表格神器
- JAVA小项目-银行管理系统(图形化界面)1-菜单
- linux sched init简介
- Android Studio 3.0安装
- Magic Bullet Suite 13破解安装教程(附加序列号)
- c++之智能指针(一)之动态内存与智能指针
- 175. Spring Boot WebSocket:单聊
- 记录一次RAC升级的过程