国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode-LongestPalindromicSubstring

2019-11-14 14:51:38
字體:
來源:轉載
供稿:網友

題目:

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

思路:

以每個點為基礎,尋找回文字符串,兩種情況,一種是形如aba,中心店兩邊是對稱的,長度為奇數,另一種是形如aa,長度為偶數。

package string;public class LongestPalindromicSubstring {    public String longestPalindrome(String s) {        int len = s.length();        String max = "";        for (int i = 0; i < len; ++i) {            String res = getStr(s, len, i, i);            if (res.length() > max.length())                max = res;            if (i + 1 < len && s.charAt(i) == s.charAt(i + 1)) {                res = getStr(s, len, i, i + 1);                if (res.length() > max.length())                    max = res;            }        }                return max;    }        PRivate String getStr(String s, int len, int left, int right) {        while (right < len && left >= 0 && s.charAt(left) == s.charAt(right)) {            --left;            ++right;        }        return s.substring(left + 1, right);    }            public static void main(String[] args) {        // TODO Auto-generated method stub        LongestPalindromicSubstring l = new LongestPalindromicSubstring();        System.out.println(l.longestPalindrome("aabbcc"));    }}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平利县| 雷波县| 星座| 定安县| 方城县| 沁阳市| 道孚县| 中山市| 建瓯市| 开平市| 阿鲁科尔沁旗| 阜城县| 仁怀市| 西青区| 昌邑市| 合阳县| 平乡县| 兴化市| 南陵县| 新安县| 舞钢市| 四平市| 金平| 南丹县| 吉木乃县| 舒城县| 岳普湖县| 泾川县| 滁州市| 禹州市| 贡嘎县| 锡林郭勒盟| 岳池县| 巴马| 永年县| 沿河| 增城市| 铁岭市| 拉孜县| 灵武市| 东海县|