用java求,给定一个字符串,求其中最大连续递增的数字子串(如"acb125vf...
发布网友
发布时间:2024-09-30 06:02
我来回答
共3个回答
热心网友
时间:2024-10-06 06:20
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Number {
public static void main(String[] args) {
StringBuilder stb=new StringBuilder("0,0");//容器!
String str="acb125vf13679dD4562789ABCDEF";//原串!
Matcher matcher=Pattern.compile("\\d+").matcher(str);//匹配!
for(int count=0;matcher.find();) {//挑选!
str=matcher.group();
for(int i=0;i<str.length()-1;i++) {//查看指定规则次数:
if(str.charAt(i)<str.charAt(i+1))
count++;
else
break;
}//如果,重新获取的比以前存入的次数更多,就放弃原来存入新的!
if(Integer.parseInt(stb.substring(0,stb.lastIndexOf(",")))<count) {
stb.delete(0, stb.length());
stb.append(count+","+str);
}//每次查看,为了查看,这个可有可无!
System.out.println(str+"\t递增次数:\t"+count);
count=0;//计数器归零!
}//最后容器里面存入的就是最大的,取出来即可!
System.out.println("最多的是:"+stb.substring(stb.lastIndexOf(",")+1)+"\t次数是:\t"+stb.substring(0,stb.lastIndexOf(",")));
}
}
热心网友
时间:2024-10-06 06:20
public static void main(String[] args) {
String str = "acb125vf13679dD4562789ABCDEF";
int start = 0;
int size = 0;
char prev = Character.MIN_VALUE;
int target = 0;
char[] charArray = str.toCharArray();
for (int i = 0; i <= charArray.length; i++) {
boolean isEnd = i == charArray.length;
char c = Character.MIN_VALUE;
if (!isEnd)
c = charArray[i];
boolean isNonNum = c < 48 || c > 57;
boolean isNonIncrease = prev != Character.MIN_VALUE && prev >= c;
if (isEnd || isNonNum || isNonIncrease) {
if (start != 0) {
int val = Integer
.valueOf(new String(charArray, start, size));
if (val > target)
target = val;
start = 0;
size = 0;
prev = Character.MIN_VALUE;
}
if (isNonNum)
continue;
}
if (size == 0)
start = i;
size++;
prev = c;
}
if (target != 0)
System.out.println("最大连续递增的数字子串\"" + target + "\"");
}
热心网友
时间:2024-10-06 06:27
public class Test {
static class NumberWord{
public int start_pos, len;
public char last_char;
}
public static void main(String[] args) {
String t="acb125vf13679dD4562789ABCDEF";
NumberWord Longest=null;
for(int i=0; i<t.length(); i++) {
char fc=t.charAt(i);
if(Character.isDigit(fc)) {
NumberWord w=new NumberWord();
w.len=1; w.last_char=fc; w.start_pos=i++;
while(i<t.length()){
char c=t.charAt(i);
if( Character.isDigit(c) && c>w.last_char){
w.len++; w.last_char=c;
i++;
}else{ i--;break; }
}
if(Longest==null || Longest.len<w.len) Longest=w;
}
}
System.out.println("The longest: "
+t.substring(Longest.start_pos, Longest.start_pos+Longest.len) );
}
}The longest: 13679
按123451也取12345写的,因为命题说“子串”。复习一下低级查找..
热心网友
时间:2024-10-06 06:28
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Number {
public static void main(String[] args) {
StringBuilder stb=new StringBuilder("0,0");//容器!
String str="acb125vf13679dD4562789ABCDEF";//原串!
Matcher matcher=Pattern.compile("\\d+").matcher(str);//匹配!
for(int count=0;matcher.find();) {//挑选!
str=matcher.group();
for(int i=0;i<str.length()-1;i++) {//查看指定规则次数:
if(str.charAt(i)<str.charAt(i+1))
count++;
else
break;
}//如果,重新获取的比以前存入的次数更多,就放弃原来存入新的!
if(Integer.parseInt(stb.substring(0,stb.lastIndexOf(",")))<count) {
stb.delete(0, stb.length());
stb.append(count+","+str);
}//每次查看,为了查看,这个可有可无!
System.out.println(str+"\t递增次数:\t"+count);
count=0;//计数器归零!
}//最后容器里面存入的就是最大的,取出来即可!
System.out.println("最多的是:"+stb.substring(stb.lastIndexOf(",")+1)+"\t次数是:\t"+stb.substring(0,stb.lastIndexOf(",")));
}
}
热心网友
时间:2024-10-06 06:22
public class Test {
static class NumberWord{
public int start_pos, len;
public char last_char;
}
public static void main(String[] args) {
String t="acb125vf13679dD4562789ABCDEF";
NumberWord Longest=null;
for(int i=0; i<t.length(); i++) {
char fc=t.charAt(i);
if(Character.isDigit(fc)) {
NumberWord w=new NumberWord();
w.len=1; w.last_char=fc; w.start_pos=i++;
while(i<t.length()){
char c=t.charAt(i);
if( Character.isDigit(c) && c>w.last_char){
w.len++; w.last_char=c;
i++;
}else{ i--;break; }
}
if(Longest==null || Longest.len<w.len) Longest=w;
}
}
System.out.println("The longest: "
+t.substring(Longest.start_pos, Longest.start_pos+Longest.len) );
}
}The longest: 13679
按123451也取12345写的,因为命题说“子串”。复习一下低级查找..
热心网友
时间:2024-10-06 06:28
public static void main(String[] args) {
String str = "acb125vf13679dD4562789ABCDEF";
int start = 0;
int size = 0;
char prev = Character.MIN_VALUE;
int target = 0;
char[] charArray = str.toCharArray();
for (int i = 0; i <= charArray.length; i++) {
boolean isEnd = i == charArray.length;
char c = Character.MIN_VALUE;
if (!isEnd)
c = charArray[i];
boolean isNonNum = c < 48 || c > 57;
boolean isNonIncrease = prev != Character.MIN_VALUE && prev >= c;
if (isEnd || isNonNum || isNonIncrease) {
if (start != 0) {
int val = Integer
.valueOf(new String(charArray, start, size));
if (val > target)
target = val;
start = 0;
size = 0;
prev = Character.MIN_VALUE;
}
if (isNonNum)
continue;
}
if (size == 0)
start = i;
size++;
prev = c;
}
if (target != 0)
System.out.println("最大连续递增的数字子串\"" + target + "\"");
}