第25题:返回字符串中最长数字字符串的长度
2015-06-22 15:20
288 查看
欢迎转载,转载请务必注明出处:http://blog.csdn.net/alading2009/article/details/46593595
github:https://github.com/frank-cq/MyTest
执行结果
github:https://github.com/frank-cq/MyTest
第25题:在字符串中找出连续最长的数字串,把这个串的长度返回
代码package test025; /** * Created by cq on 2015/6/22. * 第25题:在字符串中找出连续最长的数字串,把这个串的长度返回。 */ public class Test025 { public static int getLenOfMaxNumSubstring(String str){ if (str == null){ return 0; } int maxLen = 0, count = 0; for (int i=0; i<str.length(); i++){ if (str.charAt(i) >= '0' && str.charAt(i) <= '9'){ count++; } else{ if (count > maxLen){ maxLen = count; //子字符串起始位置:i-count } count = 0; } } //若count较大,子字符串起始位置为str.length()-count return maxLen > count ? maxLen:count; } public static void main(String[] args){ System.out.println(getLenOfMaxNumSubstring("123sss12345dse12345678")); } }
执行结果
Connected to the target VM, address: '127.0.0.1:6254', transport: 'socket' 8 Disconnected from the target VM, address: '127.0.0.1:6254', transport: 'socket' Process finished with exit code 0
相关文章推荐
- 聊聊HTTPS和SSL/TLS协议
- JS关闭窗口或JS关闭页面的几种代码
- 【剑指offer 面试题17】合并两个排序的链表
- objc_setAssociatedObject、objc_getAssociatedObject
- 求高精度幂
- 一些基本属性随笔(不定时更新)
- photoshop画矩形款
- Using Gazebo plugins with ROS
- spi-奇怪的全双工方式
- Firebug入门指南(转)
- monoid之含义
- 前端bootstrap和jquery使用记录
- 伟大程序员的十大特质
- codeforces 549B B. Looksery Party(构造)
- WebGL中的OpenGL着色器语言
- atoi和itoa函数
- Eclipse+Pydev +Django搭建开发环境时容易出错的几点
- 书籍
- 粽子为啥都有“角”?
- csdn我的收藏 的路径