leecode 回文字符串加强版

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.

只考虑数字和字母,字母大小写都一样,判断是否是回文。重点是忽略非非字母和数字。比较简单。

 1 public class Solution {
 2     public boolean isPalindrome(String s) {
 3
 4         int low=0;
 5         int high=s.length()-1;
 6         String s1=s.toLowerCase();
 7         while(low<high)
 8         {
 9
10             while(low<high)
11             {
12                char c=s1.charAt(low);
13                if((c>=‘a‘&&c<=‘z‘)||(c>=‘0‘&&c<=‘9‘))
14                {
15                    break;
16                }
17                else  low++;
18
19             }
20
21              while(low<high)
22             {
23                char c=s1.charAt(high);
24                if((c>=‘a‘&&c<=‘z‘)||(c>=‘0‘&&c<=‘9‘))
25                {
26                    break;
27                }else high--;
28
29             }
30
31
32
33             if(s1.charAt(low)==s1.charAt(high))
34             {
35                 low++;
36                 high--;
37             }
38             else
39             {
40                 return false;
41             }
42
43
44
45         }
46
47         return true;
48
49
50     }
51 }

leecode 回文字符串加强版

时间: 2024-08-05 18:39:12

leecode 回文字符串加强版的相关文章

判断一个字符串是否为回文字符串

#include <stdio.h> #include <assert.h> #include <string.h> int is_pal_str(const char *p) {  assert(p);  int len = strlen(p);  const char *start = p;  const char *end = p+len - 1;  while (start < end)  {   if (*start == *end)   {    st

【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】

背景 近期開始研究算法,于是在leetcode上做算法题,第五题Longest Palindromic Substring便是关于回文子串的. 什么是回文字串 回文字符串是指将该字符串前后颠倒之后和该字符串一样的字符串.比如:a,aaaa,aba,abba- 最长回文子串 要求最长回文子串,就须要遍历每个子串,时间复杂度是O(N2):推断字串是不是回文,时间复杂度是O(N),这种话算法的时间复杂度就是O(N3). 我刚開始想到的就是中心扩展法,代码例如以下: public static Stri

判断是否是回文字符串(Java实现)

1.回文的定义:“回文数”就是正读倒读都一样的整数.如奇数个数字:98789,这个数字正读是98789 倒读也是98789.偶数个数字3223也是回文数.字母 abcba 也是回文. 2. 判断一个字符串是否是回文字符串(Java实现) 1 public class Test4 { 2 public static boolean isHuiWen(String text) { 3 int length = text.length(); 4 for (int i = 0; i < length /

shell脚本实现检测回文字符串

所有回文字的结构特征如下: 如果字符数是偶数,那么它在结构上表现为:一个字符序列连着另一个字符相同但次序恰好相反的字符序列. 如果字符数为奇数,那么它在结构上表现为:一个字符序列连着另一个字符相同但次序恰好相反的字符序列,但是这两个序列中间共享一个相同的字符. sed命令能够记住之前匹配的子样式.可以用正则表达式:'\(.\)',匹配任意一个字符,\1表示其反向引用.如匹配有两个字符的回文正则表达式为: '\(.\)\(.\)\2\1' 匹配任意长度的回文脚本如下所示: #!/bin/bash

最长回文字符串 POJ3974

曾经有一个好算法放到我面前,我没有好好珍惜,直到用到的时候才后悔莫及. 那就是Manacher(马拉车算法),以O(n)的复杂度计算最长回文字符串. 曾经刷Leetcode的时候,室友跟我说了这个算法,但当时那个题目用中间枚举也过了,我就没有在意,直到前天才弄会,写这篇报告之前, 我又专门写了一遍马拉车,果然还是有点问题的. 详细原理链接 点击 Mark #include <stdio.h> #include <iostream> #include <string.h>

Java Longest Palindromic Substring(最长回文字符串)

假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic string.如aba,或者abba.本题是这种,给定输入一个字符串.要求输出一个子串,使得子串是最长的padromic string. 下边提供3种思路 1.两側比較法 以abba这样一个字符串为例来看,abba中,一共同拥有偶数个字.第1位=倒数第1位.第2位=倒数第2位......第N位=倒数第N位 以aba这样一个字符串为例来看,aba中.一共同拥有奇数个字符.排除掉正中间的那个字符后,第1位=倒数第1

131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

131. Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["

【又见LCS】NYOJ-37 回文字符串

[题目链接] 回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然,我们给你的问题不会再简单到判断一个字符串是不是回文字符串.现在要求你,给你一个字符串,可在任意位置添加字符,最少再添加几个字符,可以使这个字符串成为回文字符串. 输入 第一行给出整数N(0<N<100)接下来的N行,每行一个字符串,每个字符串长度不超过1000. 输出 每行输出所需添

ACM学习历程——HDU5202 Rikka with string(dfs,回文字符串)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: One day, Yuta got a string which contains n letters but Rikka lost it in accident. Now