LeetCode:Valid Palindrome雙指針 java版

LeetCode 上的這道題其實很簡單,雙指針比較即可,其中有些過程要注意,比如大小寫要去掉,標點去掉,空格去掉,還有 `  這種玩意也要去掉,因爲用正則表達式是無法將該玩意去掉的,所以只能單獨去掉,之後就Accept了。。。

直接上代碼:

boolean isPalindrome(String s) {
		if (s == null)
			return false;
		if ("".equals(s))
			return true;
		s = s.replaceAll("[\\pP]", ""); // 正則表達式
		s = s.replaceAll(" ", ""); // 正則表達式
		s = s.replace("`", "");
		s = s.toLowerCase();
		int n = s.length();
		for (int i = 0, j = n - 1; i < j; i++, j--) {
			if ((s.charAt(i)) != (s.charAt(j)))
				return false;
		}
		return true;
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章