黑马程序员技术交流社区
标题:
求两个字符串的最大相同子串--简写!
[打印本页]
作者:
1017161726
时间:
2015-4-28 19:30
标题:
求两个字符串的最大相同子串--简写!
本帖最后由 1017161726 于 2015-4-28 19:32 编辑
<div class="blockcode"><blockquote>class Demo
{
public static void main(String []args)
{
String s1="ahfcengnkgn";
String s2="wcengjg";
System.out.println(tong(s1,s2));
}
public static String tong(String s1,String s2)
{
String max="",min="";
max= s1.length()>s2.length()? s1:s2;
min= max==s2? s1:s2;
int L=s2.length()-1;
while (L>0)
{
for (int x=0;x<s2.length()-L;x++)
{
String str=s2.substring(x,x+L);
if (s1.contains(str))
return str;
}
L--;
}
return null;
}
}
复制代码
//够不够简单高效?
作者:
1017161726
时间:
2015-4-28 19:33
为什么有时候代码写完了发表之后,发表的内容中只有前一部分了。后面的内容都没了??
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2