Day 14 - Valid Palindrome

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~


125. Valid Palindrome

Question

Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.


Example

Example1

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

Example2

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

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s consists only of printable ASCII characters.

解题

题目

首先先简单的翻译一下题目
给一个字串,要把除了英文字母以外的符号和空白都删掉,然後看这个字串是不是对称的(忽略大小写)。

Think

作法大致上是这样

  • 一开始写法是s = s.replace(",", "").replace(" ", "").replace(":", "")。
  • 然後送出後才知道为什麽这题这麽多倒赞,因为测资有一堆乱七八糟的符号,应该含有所有的符号啦XD,於是就换个写法,用for loop去跑,不然不知道要打多少个replace。ಥ_ಥ
  • 这题挺无聊的XD,C的再考虑这题要不要补。

Code

Python

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = s.strip()
        for character in ['\\', '`', '*', '_', '{',\
                          '}', '[', ']', '(', ')', \
                          '>', '#', '+', '-', '.', 
                          '!', '$', '\'', ' ', ',',\
                          ':', '%', '^', '<', '&',\
                          '=', '?', '{', '}', '|',\
                          '/', '~', '@', '\"', ';']:
            if character in s:
                s = s.replace(character,"")
        s = s.lower()
        # print(s)
        
        low = 0
        high = len(s)-1
        
        while(low<=high):
            if s[low] != s[high]:
                return False
            low += 1
            high -= 1
        
        return True

C


Result

  • Python

  • C

大家明天见/images/emoticon/emoticon29.gif


<<:  Day29 Lab 2 - Object storage数据压缩

>>:  DAY17: 实作提交表单的Post请求

学pchome截图-用html2canvas转成canvas,再下载

用html2canvas转成canvas,再下载 载入js,js会把div里的东西,转换成canva...

CSS垂直置中

absolute + top/left + tranform 利用top/left将子元素的左上角对...

Day - 19 分开页面

这在好几章以前有提过,我希望能透过增加不同的分页使每个练习分门别类,当然,要完成的作品也是。 但拜读...

Day29 订单 -- 订阅

其实订阅订单跟定期定额作法非常相似, 这边会额外拉出来讲是因为个人对於他们两兄弟有不同定义, 文章是...

Day 12 : 物件导向

在进入机器学习之前,想先大家深入了解一些 python 的进阶操作。接下来的操作会有点抽象,请好好品...