首页 > 其他分享 >闯关leetcode——125. Valid Palindrome

闯关leetcode——125. Valid Palindrome

时间:2024-10-20 09:51:10浏览次数:3  
标签:palindrome false string Example Palindrome Valid characters letters leetcode

大纲

题目

地址

https://leetcode.com/problems/valid-palindrome/description/

内容

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:

Input: s = “A man, a plan, a canal: Panama”
Output: true
Explanation: “amanaplanacanalpanama” is a palindrome.

Example 2:

Input: s = “race a car”
Output: false
Explanation: “raceacar” is not a palindrome.

Example 3:

Input: s = " "
Output: true
Explanation: s is an empty string “” after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.

Constraints:

  • 1 <= s.length <= 2 * 105
  • s consists only of printable ASCII characters.

解题

这题就是要检测字符串中字符和字母是否是回文。这样我们就要过滤掉所有不是数字和字母的内容,然后去比较。
解题的思路也很简单:两个方向相向而行,然后过滤掉所有不是数字和字母的,只要发现不符合回文特点就返回false。当两个方向下标相遇后,就说明对比结束。如果此时还没返回false,说明内容符合回文特点。
在这里插入图片描述

代码地址

https://github.com/f304646673/leetcode/tree/main/125-Valid-Palindrome

标签:palindrome,false,string,Example,Palindrome,Valid,characters,letters,leetcode
From: https://blog.csdn.net/breaksoftware/article/details/142166884

相关文章

  • [LeetCode] 1545. Find Kth Bit in Nth Binary String
    Giventwopositiveintegersnandk,thebinarystringSnisformedasfollows:S1="0"Si=Si-1+"1"+reverse(invert(Si-1))fori>1Where+denotestheconcatenationoperation,reverse(x)returnsthereversedstringx,an......
  • leetcode:有效的数独
    题目:有效的数独link:https://leetcode.cn/problems/valid-sudoku/description/?envType=study-plan-v2&envId=top-interview-150defisValidSudoku(self,board:List[List[str]])->bool:rows=[[0foriinrange(9)]forjinrange(9)]colou......
  • leetcode:螺旋矩阵
    2024-10-19 https://leetcode.cn/problems/spiral-matrix/description/?envType=study-plan-v2&envId=top-interview-150   1classSolution:2defspiralOrder(self,matrix:List[List[int]])->List[int]:34m=len(matrix)5......
  • leetcode:栈和队列oj题
    目录1.有效的括号2.用队列实现栈                                        3.用栈实现队列                              ......
  • LeetCode热题100|买卖股票的最佳时机(贪心)
    简述题意省流版:在一个序列里找到max(a[i]-a[k])且i>k。解题思路:  遍历这个序列,i表示当前遍历到了第i个元素,min1表示1到i这个范围内最小的元素,max1表示1到i这个范围内最大的【max(a[i]-a[k])】。max1=max(max1,第i个元素的值-min1)代码如下:classSolution{public:intm......
  • LeetCode 707 - 设计链表
    题目描述你可以选择使用单链表或者双链表,设计并实现自己的链表。单链表中的节点应该具备两个属性:val 和 next 。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果是双向链表,则还需要属性 prev 以指示链表中的上一个节点。假设链表中的所有节点下标从 0 ......
  • Leetcode 1129. 颜色交替的最短路径
    1.题目基本信息1.1.题目描述给定一个整数n,即有向图中的节点数,其中节点标记为0到n–1。图中的每条边为红色或者蓝色,并且可能存在自环或平行边。给定两个数组redEdges和blueEdges,其中:redEdges[i]=[a_i,b_i]表示图中存在一条从节点a_i到节点b_i的红色有向边,bl......
  • Leetcode 1135. 最低成本连通所有城市
    1.题目基本信息1.1.题目描述想象一下你是个城市基建规划者,地图上有n座城市,它们按以1到n的次序编号。给你整数n和一个数组conections,其中connections[i]=[x_i,y_i,cost_i]表示将城市x_i和城市y_i连接所要的cost_i(连接是双向的)。返回连接所有城市的最低成本,......
  • 【leetcode】 码住—两种办法解决力扣数学思想 “加一” 操作
     前言......
  • LeetCode题练习与总结:最大单词长度乘积--318
    一、题目描述给你一个字符串数组 words ,找出并返回 length(words[i])*length(words[j]) 的最大值,并且这两个单词不含有公共字母。如果不存在这样的两个单词,返回 0 。示例 1:输入:words=["abcw","baz","foo","bar","xtfn","abcdef"]输出:16解释:这两个单词为"abc......