华为OJ 初级:在字符串中找出连续最长的数字串

来源:互联网 发布:常见的nosql数据库 编辑:程序博客网 时间:2024/06/06 21:42

描述

样例输出

输出123058789,函数返回值9

输出54761,函数返回值5

 

接口说明

函数原型:

   unsignedint Continumax(char** pOutputstr,  char* intputstr)

输入参数:
   char* intputstr  输入字符串;

输出参数:
   char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;  

返回值:
  连续最长的数字串的长度

 

 

 

 

知识点位运算运行时间限制10M内存限制128输入

输入一个字符串。

输出

输出字符串中最长的数字字符串和它的长度。

如果数字字符串为空,则只输出0

如 input: dadfsaf  output:0

样例输入abcd12345ed125ss123058789样例输出123058789,9
import java.util.Scanner;public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);String input = sc.next();int[] result = new int[input.length()];sc.close();for (int i = 0; i < input.length(); i++) {if (input.charAt(i) >= '0' && input.charAt(i) <= '9') { //遍历判断是否是数字int length = 0;for (; i < input.length(); i++) { //如果是数字,从这个位置开始往后面遍历if (input.charAt(i) >= '0' && input.charAt(i) <= '9') {length++;  //当是数字的时候,长度+1} else {break;  //否则终断遍历}result[i] = length;  //i为数字串结束的位置,length为数字串长度}}}int length = 0;int temp = 0;for (int i = 0; i < result.length; i++) {if (result[i] >= length) {length = result[i];  //找出最长的数字串和其终点位置temp = i;}}if (length == 0)System.out.println(0);else {String output = input.substring(temp - length + 1, temp + 1);System.out.println(output + "," + length);}}//方法二:使用split配合正则表达式,将字符串分隔成只含有数字的数组,此方法只适合输入字符串只含有字符和数字private static void find(String input){String[] string = input.split("[a-zA-Z]");String temp = null;int length = 0;for(int i = 0; i < string.length; i++){if(length <= string[i].length()){length = string[i].length();temp = string[i];}}if(string.length == 0)System.out.println(0);elseSystem.out.println(temp + "," + length);}}



0 0