百度的一个笔试题:求字符串的最长数字子串的长度

来源:互联网 发布:淘宝优惠券入口 编辑:程序博客网 时间:2024/05/21 17:58

比较简单。

package a;public class CopyOfTest1 {public static void main(String ss[]) {isNum('a');isNum('9');System.out.println(longestNumSubString("a15bcd78efg941234k"));}static int longestNumSubString(String src) {int max = 0;int count = 0;for (int i = 0; i < src.length(); i++) {if (isNum(src.charAt(i))) {count++;if (max < count) {max = count;}} else {count = 0;}}return max;}static boolean isNum(char c) {int d = c;d = d - 48;if (d <= 9 && d >= 0) {return true;}return false;}}


 

原创粉丝点击