【Leetcode】Valid Palindrome

题目链接:https://leetcode.com/problems/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.

算法

[java] view
plain
 copy

  1. public boolean isDecent(char c) {
  2. if (Character.isAlphabetic(c) || Character.isDigit(c)) {
  3. return true;
  4. } else
  5. return false;
  6. }
  7. public boolean isPalindrome(String s) {
  8. char c[] = s.toLowerCase().toCharArray();
  9. int i = 0, j = c.length - 1;
  10. while (i < j) {
  11. if (isDecent(c[i]) && isDecent(c[j])) {
  12. if (c[i] == c[j]) {
  13. i++;
  14. j--;
  15. } else {
  16. return false;
  17. }
  18. }
  19. if (!isDecent(c[i])) {
  20. i++;
  21. }
  22. if (!isDecent(c[j])) {
  23. j--;
  24. }
  25. }
  26. return true;
  27. }
时间: 2024-10-11 09:34:54

【Leetcode】Valid Palindrome的相关文章

【LeetCode】- 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" i

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 th

【LeetCode】Valid Number 解题报告

[题目] Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambig

【leetcode】Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

【LeetCode】009 Palindrome Number

题目:LeetCode 009 Palindrome Number 题意:判断一个整数是否为回文数,不要用额外空间 思路:我不会不用额外空间的方法,需要利用一个长度为20以内的字符串.将整数先写入一个字符串,然后判断首位字符是否相等即可. 代码如下: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 string s = to_string(x); 5 int len = s.size(); 6 for(int i = 0;

【leetcode】1278. Palindrome Partitioning III

题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindro

【LeetCode】Valid Sudoku

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku boar

【LeetCode】9 - Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

【LeetCode】234 - Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? Hide Tags: Linked List Two Pointers 1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 typedef struct ListNode