leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring
Given
a string S,
find the longest palindromic substring in S.
You may assume that the maximum length of S is
1000, and there exists one unique longest palindromic substring.
time=255ms
accepted 暴力遍历
public String longestPalindrome(String s){
String result=null;
int length=s.length();
int max=0;
if(length==0)
return null;
else if(length==1)
return s;
else{
for(int i=1;i<length;i++){
boolean b=false;
int k=0;
int j=0;
for(int m=i-1;m>=0&&m>=i-2;m--){
if(s.charAt(i)==s.charAt(m)){
k=m-1;
b=true;
j=i+1;
while(b&&k>=0&&j<length){
if(s.charAt(j)==s.charAt(k)){
j++;
k--;
}else
b=false;
}
if(max<j-k-1){
result=s.substring(k+1,j);
max=j-k-1;
}
}
}
if(j>=length)
break;
}
return result;
}
}
leetcode 第五题 Longest Palindromic Substring (java)的更多相关文章
- leetcode第五题--Longest Palindromic Substring
Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...
- Leetcode: Longest Palindromic Substring. java
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- leetcode--5 Longest Palindromic Substring
1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- LeetCode(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- leetcode 5. Longest Palindromic Substring [java]
public String longestPalindrome(String s) { String rs = ""; int res = 0; for(int i = 0; i& ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- LeetCode第五题:Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- 非spring环境中配置文件工具
http://commons.apache.org/proper/commons-configuration/ 注意:属性的值使用","分割,会造成读取属性值不正确的问题.建议使用 ...
- apache solr简单搭建
首先,下载位置是:http://lucene.apache.org/solr/downloads.html 官网的学习资料:http://lucene.apache.org/solr/quicksta ...
- WPF在后台中写一个鼠标移入移出的操作
在这个问题上我纠结了好久就是为了一个问题就是forebackground这个属性 lblPwd.Foreground = Brushes.Black;我以前一直以为是fontground这个属性可是我 ...
- FTP上传下载
使用的是apache开源包commons-net-3.3.jar所提供的FTPClient FTP服务器使用Quick Easy FTP Server 4.0.0(服务器ip为192.168.31.1 ...
- Java Socket简例
Socket IO工具类: package com.test.util; import java.io.DataInputStream; import java.io.DataOutputStream ...
- Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536
关于方法数超限,Google官方给出的方案是这样的:https://developer.android.com/intl/zh-cn/tools/building/multidex.html 我也写过 ...
- Android adapter适配器的使用
说起Adapter的使用,首先想到的就是listview或各种各样的Adapter.下面我们对常用的一些Adapter进行简单的使用讲解. 这是Adapter的关系图: 下面的所有例子均使用同一个布局 ...
- asp.net中Get请求和Post请求
Get和Post请求的区别:Get请求因为传输的数据在URL中,因此不安全,而且多数浏览器有限制其长度,最长为2KB.通过Get请求获取数据的方式:string strName=context.Req ...
- NSArray 常用的一些方法
- (NSUInteger) count; 返回数组中元素个数 - (id)objectAtIndex:(NSUInteger)index; 返回一个id类型的数组指定位置元素 - (id)lastO ...
- HTTP 417解决方案
在一次模拟HTPP请求时,本人在项目中的一般处理程序中调用客户接口返回非成功的结果.为了方便调试,所以将核心代码拷贝至控制台中进逐个调试. 在控制台中,启动调试时提示: 未经处理的异常: S ...