您的位置:首页 > 其它

IT公司100题-25-求字符串中的最长数字串

2016-01-04 10:24 323 查看
问题描述:

实现一个函数,求出字符串中的连续最长数字串。例如输入”12345cbf3456″,输出”12345″。

问题分析:

遍历一遍字符串,记录起始位置和长度即可。

代码实现:

package oschina.IT100;

import java.util.Scanner;

/**
* @project: oschina
* @filename: IT25.java
* @version: 0.10
* @author: JM Han
* @date: 10:05 AM 1/4/2016
* @comment: Find the continous number of max length
* @result: Please input the String:
* 123dfasdf123123asdfasdf33333333333333asdfsdf221asdf2323
* The max continuous number is: 33333333333333
*/

public class IT25 {
public static String contiNumMax(String s){
char[] chars = s.toCharArray();
int length = chars.length;
int len = 0; int maxLen = 0;
String r = "";

for(int i = 0; i < length; i++){
char c = chars[i];
if(c > '0' && c < '9'){
len++;
} else{
if(len > maxLen){
maxLen = len;
r = s.substring(i - len, i);
}
//has to reset len to re-calculate
len = 0;
}
}
return r;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please input the String: ");
System.out.println("The max continuous number is: " + contiNumMax(sc.next()));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: