leetcode 最长回文子串

 2023-09-06 阅读 19 评论 0

摘要:103 / 103 个通过测试用例状态:通过执行用时:8 ms内存消耗:36.3 MB提交时间:6 月,3 周之前class Solution {public String longestPalindrome(String s) {if (s == null || s.length() < 1) return "";int start =

103 / 103 个通过测试用例
状态:通过
执行用时:8 ms
内存消耗:36.3 MB
提交时间:6 月,3 周之前

class Solution {public String longestPalindrome(String s) {if (s == null || s.length() < 1) return "";int start = 0, end = 0;for (int i = 0; i < s.length(); i++) {int len1 = expandAroundCenter(s, i, i);int len2 = expandAroundCenter(s, i, i + 1);int len = Math.max(len1, len2);if (len > end - start) {start = i - (len - 1) / 2;end = i + len / 2;}}return s.substring(start, end + 1);
}private int expandAroundCenter(String s, int left, int right) {int L = left, R = right;while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {L--;R++;}return R - L - 1;
}
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/5266.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息