Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to validate palindromes in leetcode

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to verify palindromes in leetcode. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

Given a non-empty string s, delete at most one character. Determine whether it can become a palindrome string.

Example 1:

Enter: "aba"

Output: True

Example 2:

Enter: "abca"

Output: True

Explanation: you can delete the c character.

Note:

The string contains only lowercase letters from amerz. The maximum length of a string is 50000.

Test venue:

Palindromes have the same properties as positive and negative order.

Solution:

Start the comparison from the beginning to the end, and if it is not equal, delete one end and continue down.

Class Solution: def validPalindrome (self S: str)-> bool: # cycle to distinguish''def help (s): if s = = s [::-1]: return True else: return False if help (s): return True length = len (s) for i in range (length): If help (s [: I] + s [iTun1:]): return True return False''# starts from both ends left = 0 right = len (s)-1 while left < right: if s [left]! = s [right]: return s [left: right] = = s [left: Right] [:-1] or s [left+1: right+1] = = s [left+1: right+1] [::-1] left+ = 1 right-= 1 return True are all the contents of the article "how to verify palindromes in leetcode" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report