Leetcode_005_Longest_Palindromic_Substring
来源:互联网 发布:破解windows开机密码 编辑:程序博客网 时间:2024/06/04 01:31
public class Solution { public String longestPalindrome(String s) { String result = ""; if(s.length() == 1) return s; for(int i = 0; i<s.length()-1; i++){ String odd = helper(i,i,s); String even = helper(i,i+1,s); if(odd.length()>even.length()){ if(odd.length() > result.length()) result = odd; }else{ if(even.length() > result.length()) result = even; } } return result; } private String helper(int i, int j, String s){ if(i != j && s.charAt(i) != s.charAt(j)){ return ""; } while(i-1>=0 && j+1<s.length() && s.charAt(i-1) == s.charAt(j+1)){ i--; j++; } return s.substring(i,j+1); }}
go through while string, make every character possible middle character(odd or even) and search for max length.
0 0
- Leetcode_005_Longest_Palindromic_Substring
- uoj #86 mx的组合数 FFT 原根
- 基于Spring的扩展Schema配置
- 数组,vector,map,list,queue区别
- CoreData应用UI活动指示器不显示的解决
- P1141 01迷宫
- Leetcode_005_Longest_Palindromic_Substring
- 《DSAA》 12.1 自顶向下伸展树
- 【LeetCode】 167. Two Sum II - Input array is sorted
- 跨域请求
- 389. Find the Difference
- 查看端口被占用
- Oracle数据库之数据处理
- go 语言 TCP服务器接受客户端字符串信息
- Pycharm的运行和简单调试