LeetCode 125. Valid Palindorme (验证回文字符串)

时间:2021-12-08 03:46:30

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.


题目标签:String, Two Pointers

  题目给了我们一个string s, 让我们判断它是不是 palindorme。

  对于s, 我们只需要比较 字母 和 数字。

设置一个 left = 0, right = s.length() -1,依次比较,其中遇到任何其他 char 就跳过,具体看code。

  一开始不知道有 Character.isLetterOrDigit() ,还分别用了 letter 和 digit - -

Java Solution:

Runtime beats 80.01%

完成日期:03/07/2017

关键词:two pointers

关键点:skip all non-alphanumeric chars

 class Solution
{
public boolean isPalindrome(String s)
{
// use two pointers
int left = 0;
int right = s.length() - 1; char [] char_arr = s.toCharArray(); while(left < right)
{
while(left < right && !Character.isLetterOrDigit(char_arr[left])) // if char is not letter or digit
left++; while(left < right && !Character.isLetterOrDigit(char_arr[right])) // if char is not letter or digit
right--; if(Character.toUpperCase(char_arr[left]) != Character.toUpperCase(char_arr[right]))
return false; left++;
right--;
} return true;
} }

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 125. Valid Palindorme (验证回文字符串)的更多相关文章

  1. &lbrack;LeetCode&rsqb; 125&period; Valid Palindrome 验证回文字符串

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

  2. 125 Valid Palindrome 验证回文字符串

    给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...

  3. &lbrack;LeetCode&rsqb; Valid Palindrome 验证回文字符串

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

  4. &lbrack;leetcode&rsqb;125&period; Valid Palindrome判断回文串

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

  5. &lbrack;LintCode&rsqb; Valid Palindrome 验证回文字符串

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

  6. &lbrack;LeetCode&rsqb; 680&period; Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  7. leetcode 125 验证回文字符串 Valid Palindrome

    验证回文字符串 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格:然后遍历第二遍,首尾一一对应比较:时间复杂度O(n+n/2),空间O(n); class Solu ...

  8. LeetCode 680&period; 验证回文字符串 Ⅱ&lpar;Valid Palindrome II&rpar; 1

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

  9. LeetCode(125):验证回文串

    Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...

随机推荐

  1. &lbrack;HTML&sol;HTML5&rsqb;4 使用文本

    4.1  组织文本 在已经采用结构化方式将页面划分为多个关键内容区域(content area)并添加相应的标题之后,就可以进一步对这些内容区域中的文本进行组织. 4.1.1  段落 p元素的具体功能 ...

  2. php ci 2&period;0框架 ORM

    很早知道ci出了2.0版本了.这几天正好有项目要用ci开发 虽然项目不大.不过也从开发项目的过程中熟悉了CI框架 因为是个电商项目 本来想用个YII2 的. 封装的虽然厉害不过功能强大 因为另个兄弟坚 ...

  3. C&plus;&plus;面向对象要点

    先说说面向对象思想的一个总体认识 对象通常会有行为,这些行为是靠信息支撑,这些信息包括外部信息和内部信息,对象行为会维护其中的一部分信息 因此对象可以看成是这样一种实体,它获取信息,然后决定自己的行为 ...

  4. 返回类型和return语句

    return语句终止当前正在执行的函数并将控制权返回到调用该函数的地方.return语句有两种形式: return; return expression; 无返回值函数 没有返回值的return语句只 ...

  5. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

  6. 推荐15个月 Node&period;js 开发工具

    Node.js 越来月流行.这个基于 Google V8 引擎建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.在本文中.我们列出了2015年最佳的15个 Node.js 开发工具.这些工具 ...

  7. Pulse-per-second &lpar;PPS&rpar; Signal Interfacing

    Some radio clocks and related timekeeping gear have a pulse-per-second (PPS) signal that can be used ...

  8. GitHub for Windows客户端使用操作流程

    Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.作为一个程序员,我们需要掌握其用法. 作为开源代码库以及版本控制系统,Github目前拥有140 ...

  9. AddHandler php5-script &period;php&bsol;AddType text&sol;html &period;php和AddType application&sol;x-httpd-php &period;php的区别?

    让apache支持php文件的解释,有2种方法配置,RPM装的默认配置是:AddHandler php5-script .phpAddType text/html .php网上很多人的配置方法是:Ad ...

  10. Microsoft Power BI Desktop概念学习系列之Microsoft Power BI Desktop是什么?

    不多说,直接上干货! 官网 https://powerbi.microsoft.com/zh-cn/desktop/ Microsoft  Power BI Desktop是什么? https://p ...