標籤:

LeetCode 125. Valid Palindrome

題目:

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

For example,

"A man, a plan, a canal: Panama" is a palindrome.

"race a car" is not a palindrome.

Note:

Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

代碼如下:

public class Solution {n public boolean isPalindrome(String s) {n if (s.isEmpty()) {n treturn true;n }n int head = 0, tail = s.length() - 1;n char cHead, cTail;n while(head <= tail) {n tcHead = s.charAt(head);n tcTail = s.charAt(tail);n tif (!Character.isLetterOrDigit(cHead)) {n tthead++;n t} else if(!Character.isLetterOrDigit(cTail)) {n tttail--;n t} else {n ttif (Character.toLowerCase(cHead) != Character.toLowerCase(cTail)) {n tttreturn false;n tt}n tthead++;n tttail--;n t}n }n n return true;n }n} n

推薦閱讀:

想轉行IT行業的工科生,該如何系統有效的學習編程知識,選擇合適的職業發展路徑?
Linux操作系統、偏底層的項目?
C++輸出hello world,請從電子電路、內存CPU、程序層面解釋一下?
如何在 Github 上做一個規範的開源項目?
為什麼函數能遞歸調用自己?

TAG:LeetCode | 编程 |