[LeetCode] 5. Longest Substring Without Repeating Characters 最长回文子串

[LeetCode] 5. Longest Substring Without Repeating Characters

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

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

Example 2:

Input: "cbbd"
Output: "bb"

思路:中心扩散法

对应于回文串有奇偶两种情况,扩散方式也有两种,从当前位置开始,和两数之间开始,依次比较对称位置是否相同

 1 class Solution {
 2     public String longestPalindrome(String s) {
 3         if (s.length()<1) {          
 4             return "";              
 5         }
 6         int start = 0;                            // 结果字符串首位下标,最后通过下标从主串中截取返回
 7         int end = 0;  
 8         for (int i=0;i<s.length();i++) {
 9             int len1 = judge(s, i, i);                  //扩散方式1:从当前位置开始,对应于奇数个
10             int len2 = judge(s, i, i+1);                            //扩散方式2:从两数之间开始,对应于偶数个
11             int max_len = Math.max(len1, len2);
12             if(max_len > end - start + 1) {
13                 start = i - (max_len-1)/2 ;               ▲下面解释
14                 end = i + max_len/2;
15             }
16         }
17         return s.substring(start, end+1);
18     }
19
20     public int judge(String s,int left ,int right) {       21         int L = left;
22         int R = right;
23         while(L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R) ) {
24             L--;
25             R++;
26         }
27         return R-L-1;              //如果是结束下标,长度应该返回 R-L+1  , 但是结束的时候 L , R各自往外移了一位,所以长度应该是 R-L-1
28     }
29 }
字符串、数组通过 中心下标 和 长度求出首尾下标的方法:

无论是列表长度是奇数还是偶数,中心位置都是一样的。

 

start = i + (length-1) / 2 ;

end   = i = length / 2 ;



原文地址:https://www.cnblogs.com/Poceer/p/10944430.html

时间: 2025-02-01 19:53:01

[LeetCode] 5. Longest Substring Without Repeating Characters 最长回文子串的相关文章

[LeetCode] Longest substring without repeating characters 最长无重复子串

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

Leetcode 3 Longest Substring Without Repeating Characters. (最长无重复字符子串) (滑动窗口, 双指针)

目录 问题描述 例子 方法 Leetcode 3 问题描述 Given a string, find the length of the longest substring without repeating characters. 例子 Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: &q

LeetCode Longest Substring Without Repeating Characters 最长不重复子串

题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离是多少,若大于max,则更新max.若小于,则不更新.每扫到一个字符就需要更新他的出现位置了.这里边还有个注意点,举例说明: 假如有长为16串 s="arbtbqwecpoiuyca" 当扫到第2个b时,距离上一个b的距离是2:(直接减) 当扫到第2个c时,距离上一个c的距离是6:(直接减

[LeetCode] Longest Substring Without Repeating Characters最长无重复子串

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

[LeetCode] 3. Longest Substring Without Repeating Characters 最长无重复字符的子串

1.暴力法: 本题让求给定字符串的最长的无重复字符的子串,首先想到暴力解法,穷举出字符串的所有子串,并判断每个子串是否是不重复子串,具体使用hashset或set判是否有重复字符:暴力法效率很差,时间O(n^3),空间O(n);参考代码如下: 1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s){ 4 int res = 0; 5 const int size = s.size(); 6 if(s.empty(

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

leetcode -day21 Longest Substring Without Repeating Characters

 1.Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the

[LeetCode][Python]Longest Substring Without Repeating Characters

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, find the length of the longest substring without repeating characters.For example, the longest s

[LeetCode][JavaScript]Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the lengt