leetcode-125-验证回文串
2019-08-02 23:22
323 查看
问题:
package com.example.demo; public class Test125 { /** * 验证是否是回文字符串 * 分析: * 定义两个索引,分别执行字符串的起始和结尾,两个索引同时向中间移动 * 在比较的时候,如果遇到非数字和字母的字符,在right--或left++(跳过当前字符) */ public boolean isPalindrome(String s) { if (s == null) { return false; } if (s.length() == 0) { return true; } // 忽略大小写 s = s.toLowerCase(); int left = 0; int right = s.length() - 1; while (left < right) { // 判断是否是数字或字母 while (left < right && !Character.isLetterOrDigit(s.charAt(left))) { left++; } while (left < right && !Character.isLetterOrDigit(s.charAt(right))) { right--; } if (s.charAt(left) != s.charAt(right)) { return false; } left++; right--; } return true; } public static void main(String[] args) { Test125 t = new Test125(); boolean asdffdsa = t.isPalindrome("A man, a plan, a canal: Panama"); System.out.println(asdffdsa); } }
相关文章推荐
- LeetCode125-验证回文串
- leetcode-125-验证回文串
- leetcode 125 验证回文串
- Leetcode刷题28-125.验证回文串(C++)
- Leetcode:125.验证回文串
- Python LeetCode(125.验证回文串)
- [LeetCode 125] - 验证回文(Valid Palindrome)
- leetcode-125. Valid Palindrome(验证含有标点符号String是否为回文串)
- [leetcode]验证回文串[javascript]
- LeetCode - 125. 验证回文串
- Leetcode题库 - 验证回文串(java语言版)
- 【LeetCode-面试算法经典-Java实现】【125-Valid Palindrome(回文字验证)】
- 验证回文串--leetcode第125题
- 125. 验证回文串 Java
- 125. 验证回文串
- LeetCode-125.验证回文串(相关话题:字符串)
- LeetCode 125.验证回文串
- 验证回文串 - LeetCode
- LeetCode 125. 验证回文串
- Leetcode 125 Valid Palindrome