【LeetCode】 Longest Palindromic Substring 【Python C++】双语言实现

题目(来源leetcode):

题目的意思就是从给定的字符串中找出一个最长的 对称 字符串

这道题,我的代码感觉写的贼烂,求大佬指点!

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"
#Python版本
from collections import defaultdict
class Solution:
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        #dict_s=defaultdict(int)
        #for i,x in enumerate(s):
        #    dict_s+=1
        i=0
        b=""
        max=0
        if s=="":
            return ""
        while(i<len(s)):
            if len(s)-i-1>max:
                for j in range(len(s)):
                    if s[len(s)-1-j] == s[i] and len(s)-j-i >max:
                        temp,isornot=self.Is_palindromic(s[i:len(s)-j])
                        if isornot and len(s)-j-i >max:
                            max=len(s)-j-i
                            b=temp
                        else:
                            continue
                    else:
                        continue
                i+=1
            else:
                break
        if b=="":
            b=s[0]
        return b
                        
    def Is_palindromic(self,s):
        if len(s) %2 ==0:
            #assert len(s[:len(s)//2][::-1]) == len(s[len(s)//2 :])
            if s[:len(s)//2][::-1] == s[len(s)//2 :]:
                return s,1
            else:
                return "",0
        else:
            if s[:len(s)//2][::-1]==s[len(s)//2+1:]:
                return s,1
            else:
                return "",0

#C++版本
#include <cstring>
class Solution {
public:
    string longestPalindrome(string s) {
        int i,max;
        i=0;
        max=0;
        string b="";
        string temp="";
        if(s==""){
            return "";
        }
        while(i<s.size()){
            if(s.size()-i > max){
                for(int j=0;j <s.size();j++){
                    if(s[s.size()-1-j]==s[i] && s.size()-j-i >max){
                        temp=Is_palindrom(s.substr(i,s.size()-j-i));
                        if(temp != "" && s.size()-j-i >max){
                            max=s.size()-j-i;
                            b=temp;
                        }else{
                            continue;
                        }
                    }else{
                        continue;
                    }
                }
                i++;
            }else{
                break;
            }
        }
        if(b.compare("")==0){
            b=s[0];
        }
        return b;
    }
    string Is_palindrom(string s){
        string b="";
        string s1;
        string s2;
        if(s.size() % 2 == 0){
            s1=s.substr(0,s.size()/2);
            s2=s.substr(s.size()/2,s.size()/2);
            reverse(s1.begin(),s1.end());
            if(s1.compare(s2)==0){
            //if(strrev(s1)==s2){
                b=s;
            }else{
                b="";
            }
        }else{
            s1=s.substr(0,int(s.size()/2));
            s2=s.substr(int(s.size()/2 +1),s.size()-s.size()/2 -1);
            reverse(s1.begin(),s1.end());
            if(s1.compare(s2)==0){
            //if(strrev(s1)==s2){
                b=s;
            }else{
                b="";
            }
        }
        return b;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值