publicclassSolution { public String longestPalindrome(String s) { if (s == null || s.length() < 1) { return""; } intstart=0, end = 0; for (inti=0; i < s.length(); i++) { intlen1= expandAroundCenter(s, i, i); intlen2= expandAroundCenter(s, i, i + 1); intlen= Math.max(len1, len2); if (len > end - start) { start = i - (len - 1) / 2; end = i + len / 2; } } return s.substring(start, end + 1); } privateintexpandAroundCenter(String s, int left, int right) { while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { left--; right++; } return right - left - 1; } }
classSolution { funclongestPalindrome(_s: String) -> String { if s.isEmpty { return"" } let chars =Array(s) var start =0, end =0 for i in0..<chars.count { let len1 = expandAroundCenter(chars, i, i) let len2 = expandAroundCenter(chars, i, i +1) let len =max(len1, len2) if len > end - start { start = i - (len -1) /2 end = i + len /2 } } returnString(chars[start...end]) } funcexpandAroundCenter(_chars: [Character], _left: Int, _right: Int) -> Int { var left = left, right = right while left >=0&& right < chars.count && chars[left] == chars[right] { left -=1 right +=1 } return right - left -1 } }
classSolution { funlongestPalindrome(s: String): String { if (s.isEmpty()) { return"" } val n = s.length var start = 0 var maxLength = 1 val dp = Array(n) { BooleanArray(n) } for (i in0 until n) { dp[i][i] = true } for (i in0 until n - 1) { if (s[i] == s[i + 1]) { dp[i][i + 1] = true start = i maxLength = 2 } } for (length in3..n) { for (i in0 until n - length + 1) { val j = i + length - 1 if (s[i] == s[j] && dp[i + 1][j - 1]) { dp[i][j] = true start = i maxLength = length } } } return s.substring(start, start + maxLength) } }
classSolution{ String longestPalindrome(String s) { if (s.isEmpty) { return""; } int start = 0; int maxLen = 1; for (int i = 0; i < s.length; i++) { int len1 = expandAroundCenter(s, i, i); int len2 = expandAroundCenter(s, i, i + 1); int len = max(len1, len2); if (len > maxLen) { maxLen = len; start = i - (maxLen - 1) ~/ 2; } } return s.substring(start, start + maxLen); } int expandAroundCenter(String s, int left, int right) { while (left >= 0 && right < s.length && s[left] == s[right]) { left--; right++; } return right - left - 1; } }
funclongestPalindrome(s string)string { iflen(s) == 0 { return"" } var start, end int for i := 0; i < len(s); i++ { len1 := expandAroundCenter(s, i, i) len2 := expandAroundCenter(s, i, i+1) length := max(len1, len2)
if length > end-start { start = i - (length-1)/2 end = i + length/2 } } return s[start : end+1] } funcexpandAroundCenter(s string, left, right int)int { for left >= 0 && right < len(s) && s[left] == s[right] { left-- right++ } return right - left - 1 } funcmax(x, y int)int { if x > y { return x } return y }
# @param {String} s # @return {String} deflongest_palindrome(s) return""if s.nil? || s.empty? start = 0 max_len = 1 (0...s.length).each do |i| len1 = expand_around_center(s, i, i) len2 = expand_around_center(s, i, i + 1) len = [len1, len2].max if len > max_len max_len = len start = i - (max_len - 1) / 2 end end s[start...(start + max_len)] end defexpand_around_center(s, left, right) while left >= 0 && right < s.length && s[left] == s[right] left -= 1 right += 1 end right - left - 1 end
objectSolution{ deflongestPalindrome(s: String): String = { if (s.isEmpty) return"" var start = 0 var end = 0 defexpandAroundCenter(left: Int, right: Int): Int = { var l = left var r = right while (l >= 0 && r < s.length && s.charAt(l) == s.charAt(r)) { l -= 1 r += 1 } r - l - 1 } for (i <- 0 until s.length) { val len1 = expandAroundCenter(i, i) val len2 = expandAroundCenter(i, i + 1) val len = Math.max(len1, len2) if (len > end - start) { start = i - (len - 1) / 2 end = i + len / 2 } } s.substring(start, end + 1) } }