publicbooleanvalidPalindrome(String s){// 使用双指针intleft=0;intright=s.length()-1;while(left < right) {// 如果左右指针所在的字符不相等,那就删掉右边的或者删掉左边的字符,再判断if(s.charAt(left) != s.charAt(right)) {returnisPalindrome(s, left, right-1) || isPalindrome(s, left+1, ...
3.1 Java实现 publicclassSolution{intpos=0;publicbooleanvalidPalindrome(String s){booleanvalid=isPalindrome(s);if(!valid) {intleft=pos;intright=s.length() - pos;booleanret1=isPalindrome(s.substring(left +1, right));booleanret2=isPalindrome(s.substring(left, right -1)); valid = ret1 || ...
Description: 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 1. 2. Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. 1. 2. 3. Note: The st...
但是需要遍历两次,每次删除的是前面一个元素还是后面一个元素。 classSolution(object):defvalidPalindrome(self,s):""":type s: str:rtype: bool"""i,j=0,len(s)-1count1=0whilei<=j:ifs[i]==s[j]:i+=1j-=1elifj-1>=0ands[j-1]==s[i]:j-=1count1+=1elifi+1<len(s)ands[i+1]==...
1/**2* @param {string} s3* @return {boolean}4*/5varvalidPalindrome =function(s) {6let left = 0;7let right = s.length - 1;8while(left < right && s[left] ===s[right]) {9left++;10right--;11}12if(checkPalindrome(s, left + 1, right)) {13returntrue;14}15if(checkPalindrom...
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. 这道题是之前那道Valid Palindrome的拓展,还是让我们验证回复字符串,但是区别是这道题的字符串中只含有...
Valid Palindrome·有效回文 秦她的菜 吉利 程序员题目描述 英文版描述 A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and ...
For the purpose of this problem, we define empty string as valid palindrome. 思路分析: 将字符串中的非数字字母字符跳过,大写全部转小写,然后从字符串两头向中间逼近,逐一进行比较。 C++参考示例: 代码语言:javascript 复制 classSolution{private:boolisAlphanumeric(char&ch){if(ch>='A'&&ch<='Z'){ch+...
1.7 Valid Sudoku 1.8 Trapping Rain Water 1.9 Swap Nodes in Pairs 1.10 Reverse Nodes in k-Group 2. 字符串 2.1 Valid Palindrome 2.2 Implement strStr() 3.3 String to Integer (atoi) 3.4 Add Binary 3.5 Longest Palindromic Substring 3.6 Regular Expression Matching 3.7 Wildcard Matching 3.8 Longest ...
/* * @lc app=leetcode id=125 lang=javascript * * [125] Valid Palindrome */// 只处理英文字符(题目忽略大小写,我们前面全部转化成了小写, 因此这里我们只判断小写)和数字function isValid(c) { const charCode = c.charCodeAt(0); const isDigit = charCode >= "0".charCodeAt(0) && ...