用Java编写 求一个字符串s的最大连续递增数字子串。

比如:
输入
f123fffwf3210abcd
输出为
123

输入
abcd765bbw1357f123
输出为
123

package test;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Tester
{
private static boolean isASC ( String group )
{
if (group.isEmpty ())
{
return false;
}
else if (group.length () == 1)
{
return true;
}
else
{
int a = Integer.parseInt (group.charAt (0) + "");
int b = Integer.parseInt (group.charAt (1) + "");
if (b - a != 1)
{
return false;
}
else
{
return isASC (group.substring (1));
}
}
}

public static void main ( String[] args )
{
String input = "abcd5678bbw1357f123";
String regex = "\\d+";
Pattern pattern = Pattern.compile (regex);
Matcher matcher = pattern.matcher (input);
LinkedList<String> result = new LinkedList<String> ();
while (matcher.find ())
{
String group = matcher.group ();
if (isASC (group))
{
result.add (group);
}
}
Collections.sort (result, new Comparator<String> ()
{
@Override
public int compare ( String o1, String o2 )
{
if (o1.length () > o2.length ())
{
return -1;
}
else if (o1.length () < o2.length ())
{
return 1;
}
else
{
return 0;
}
}
});
for ( String string : result )
{
if (string.length () == result.get (0).length ())
{
System.out.println (string);
}
}
}
}

温馨提示:答案为网友推荐,仅供参考
相似回答