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

Example Analysis of Java string

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "Java string example analysis". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Example 1:

Input: "babad"Output: "bab"Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"Output: "bb"

Solution 1 is relatively simple and easy to understand, but there are time-consuming operations for continuously repeating strings.

For example, abbba in the following table is 01234 the same do not compare, directly compare the same left and right

class Solution: def getTmpAns(self,s,l,r): size = len(s) while l>=0 and r len(ans): ans = tmpAns tmpAns = self.getTmpAns(s,i,i+1) if len(tmpAns) > len(ans): ans = tmpAns return ans def _longestPalindrome(self, s): """ :type s: str :rtype: str """ size = len(s) if size == 1 or size == 0: return s if size == 2: if s[0] == s[1]: return s else: return s[0] i = 0 maxp = 1 ans = s[0] while i

< size: j = i+1 while j < size: # let same num as a item like bbb,i是左边起始位置 j是右边起始位置 if s[i] == s[j]: j += 1 else: break k = 0 while i-k-1>

=0 and j+k maxp: maxp = j+k-i+k ans = s[i-k:j+k] if j+k == size-1: break i = j return ans"Java string example analysis" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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