IT公司100题-25-求字符串中的最长数字串

问题描述:java

实现一个函数,求出字符串中的连续最长数字串。例如输入”12345cbf3456″,输出”12345″。函数

问题分析:
spa

遍历一遍字符串,记录起始位置和长度便可。3d

代码实现:code

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()));
   }
}
相关文章
相关标签/搜索