class Solution { public String longestPalindrome(String s) { int max = 1; // 设置回文字符串长度为1 int len = s.length(); // 设置总字符串长度 String result = s.substring(0,1); // 设置回文字符串 for(int start = 0 ; start < len ; start ++){ for (int end = len -1 ; end > start ; end--){ if(s.charAt(start) == s.charAt(end)){ // 假设start 与end 形成回文字符串 int difmid = (end - start)/2; // 回文中间数,或委会左侧数 for(int temp = 0; temp <= difmid ; temp ++){ if(s.charAt(start + temp) != s.charAt(end-temp)){ break; } if(temp == difmid && max < (end -start + 1)){ // 取到中位数时,确认回文数 max = end -start + 1; result = s.substring(start , end + 1); } } } } } return result; } }
最长回文:https://leetcode.cn/problems/longest-palindromic-substring/
标签:end,String,int,len,力扣,start,最长,回文 From: https://www.cnblogs.com/luyj00436/p/17142194.html