IT公司100题-25-求字符串中的最长数字串
2016-01-04 10:24
323 查看
问题描述:
实现一个函数,求出字符串中的连续最长数字串。例如输入”12345cbf3456″,输出”12345″。
问题分析:
遍历一遍字符串,记录起始位置和长度即可。
代码实现:
实现一个函数,求出字符串中的连续最长数字串。例如输入”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())); } }
相关文章推荐
- Android Fragment 真正的完全解析(上)
- oracle O7_DICTIONARY_ACCESSIBILITY参数
- oracle 存储过程执行权限
- oracle 11g新特性:Pending Statistics
- oracle alert log file(告警日志文件)
- ORACLE临时表—ON COMMIT PRESERVE ROWS
- oracle with语句详解
- oracle order by与union一起使用的用法
- oracle flashback query(闪回查询)
- oracle sequence-序列
- oracle 中 rollup、cube、grouping 使用详解
- oracle连接(join)中使用using关键字
- JBoss AS7 快速配置
- Should I design my classes from the outside (interfaces first) or from the inside (data first)?
- javascript对文本进行关键字过滤
- windows下配置java程序bat启动
- Python-OpenCV 处理图像(六):对象识别
- 数据解析为json格式
- 《Java编程思想》学习笔记19——并发编程(二)
- Oracle分析函数——函数RANK,DENSE_RANK,FIRST,LAST