在字符串中找出连续最长的数字串
2017-01-13 00:00
375 查看
题目描述
样例输出 输出123058789,函数返回值9 输出54761,函数返回值5
输入描述
输入一个字符串。
输出描述
输出字符串中最长的数字字符串和它的长度。
输入例子
abcd12345ed125ss123058789
输出例子
123058789,9
算法实现
import java.util.Scanner; /** * Declaration: All Rights Reserved !!! */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt")); while (scanner.hasNext()) { String input = scanner.nextLine(); System.out.println(maxNum(input)); } scanner.close(); } private static String maxNum(String s) { int max = 0; // int idx = 0; int cur = 0; String result = ""; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c >= '0' && c <= '9') { cur++; if (max < cur) { max = cur; // idx = i; result = s.substring(i - max + 1, i + 1); } else if (max == cur) { result += s.substring(i - max + 1, i + 1); } } else { cur = 0; } } return result + "," + max; } }
相关文章推荐
- 在字符串中找出连续最长的数字串,并把这个串的长度返回
- 25、在字符串中找出连续最长的数字串
- 华为OJ 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 第25题: 写一个函数,它的原形是int continumax(char *outputstr,char *intputstr),在字符串中找出连续最长的数字串,并把这个串的长度返回
- H面试程序(6):字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 字符串操作系列_1在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 每天学习一点编程(4)(在字符串中找出连续最长的数字串,并把这个串的长度返回)
- 【练习】写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)功能:在字符串中找出连续最长的数字串,并把这个串的长度返回,
- 在字符串中找出连续最长的数字串并以参数形式返回,并把这个串的长度返回
- 在字符串中找出连续最长的数字串
- 找出字符串中的最长连续数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 字符串中找出连续最长的数字串
- 写正确函数需要注意的地方:在字符串中找出连续最长的数字串,并把这个串的长度返回
- 在字符串中找出连续最长的数字串,并把这个串的长度返回.
- 微软面试之25 在字符串中找出连续最长的数字串,并把这个串的长度返回,