[leetcode]_Valid Palindrome

时间:2021-09-24 07:54:39

题目:判断一个数字字符串是否是回文串。空串认为是回文串。

思路:双指针问题,重点在于此题的很多陷进:例如,s = " " ,return true。 s = ".," , return true。

代码:修改了很多遍,终于AC , 要点在于只有当头尾两个指针都指向数字或者字母时,此时才有比较操作,否则都认为是相等的。

 public boolean isPalindrome(String s) {

         int i = 0 , j = s.length() - 1;
char left = 0 , right = 0 ;
while( i <= j ){ // 控制循环结束,所有元素都已经遍历过了。 left = s.charAt(i);
if(!ifLegal(left)){
i++;
continue;
} right = s.charAt(j);
if(!ifLegal(right)) {
j--;
continue;
} //只有当left 和 right 同时指向数字、字母时,才进行比较;其它情况都认为是相等的。
if((left != right && Math.abs(left - right) != ('a' - 'A'))) return false;
i++;
j--;
}
return true;
} public boolean ifLegal(char ch){
if(ch >= 'a' && ch <= 'z') return true;
if(ch >= 'A' && ch <= 'Z') return true;
if(ch >= '0' && ch <= '9') return true; return false;
}

[leetcode]_Valid Palindrome的更多相关文章

  1. &lbrack;LeetCode&rsqb; 131&period; Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. &lbrack;LeetCode&rsqb; 267&period; Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. &lbrack;LeetCode&rsqb; Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. &lbrack;LeetCode&rsqb; Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. &lbrack;LeetCode&rsqb; Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. LeetCode Longest Palindrome

    原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...

  7. LeetCode Shortest Palindrome

    原题链接在这里:https://leetcode.com/problems/shortest-palindrome/ 题目: Given a string S, you are allowed to ...

  8. leetcode&commat; &lbrack;336&rsqb; Palindrome Pairs &lpar;HashMap&rpar;

    https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...

  9. LeetCode——Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. sql SYS对象集合

    select * from SYS.objects select * from SYS.all_objects select * from SYS.tables select * from SYS.c ...

  2. Java for LeetCode 234 Palindrome Linked List

    解题思路: O(1)的空间复杂度,意味着不能通过开一个List来解决问题.我们可以把List分成前后两个部分,后半部分通过指针的相互赋值进行翻转即可. JAVA实现如下: public static ...

  3. 网络性能测试工具iperf详细使用图文教程

      Iperf是一个网络性能测试工具.Iperf可以测试TCP和UDP带宽质量.Iperf可以测量最大TCP带宽,具有多种参数和UDP特性. Iperf可以报告带宽,延迟抖动和数据包丢失.利用Iper ...

  4. Swift数组的加法运算符用法:array1 &plus;&equals; array2

    var stringList1 = [String]() //创建String类型空数组 var stringList2 = ["1", "3", " ...

  5. c&num; 代码执行时间

    Stopwatch sw = new Stopwatch(); sw.Start(); Thread.Sleep(2000); sw.Stop(); System.Diagnostics.Trace. ...

  6. java比较器 之compareable 和comparato比较

    compareable 测试类import java.util.Set; import java.util.TreeSet; public class Test { public static voi ...

  7. Android系统原理与源码分析(1):利用Java反射技术阻止通过按钮关闭对话框

    原文出处:博主宇宙的极客http://www.cnblogs.com/nokiaguy/archive/2010/07/27/1786482.html 众所周知,AlertDialog类用于显示对话框 ...

  8. &period;Net dependent configuration

    error info: 解决方案:在.exe.config文件中配置Newtonsoft.Json所用版本 <runtime> <assemblyBinding xmlns=&quo ...

  9. 论文阅读笔记二十五:Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition(SPPNet CVPR2014)

    论文源址:https://arxiv.org/abs/1406.4729 tensorflow相关代码:https://github.com/peace195/sppnet 摘要 深度卷积网络需要输入 ...

  10. Android Studio启动时出现unable to access android sdk add-on list

    目录 Android Studio First Run 检测 Android SDK 及更新,由于众所周知的原因,我们会「Unable to access Android SDK add-on lis ...