Find the longest consecutive numeric string in the string

Source: Internet
Author: User
Tags addall

I have no intention of seeing this question on csdn today, and suddenly I am very interested. Because I think this seemingly simple problem does not seem easy. I thought it was not reasonable to see a few others, so I decided to write it by myself. What is the meaning of the so-called continuous numeric string? I think 12345, 54321 and so on are both calculated. Another example is 123567. The whole is not, but it can be divided into two parts: 123 and 567. 765321 is similar. When the maximum number of consecutive numeric strings is multiple, take the last one. The implementation is as follows:

 

public
static
int Continumax(String intputStr, StringBuffer outputStr) {         char [] charArray = intputStr.toCharArray();         StringBuilder sb =
new StringBuilder();         for
( int
i =
0 ; i < charArray.length; i++) {             if
(Character.isDigit(charArray[i])) {                 sb.append(charArray[i]);             }
else {                 sb.append( "," );             }         }         String[] digitArray = sb.toString().replaceAll( "(,)+" ,
"," ).split( "," ); // Obtain the number Array         if
(digitArray.length <= 1 ) {             System.out.println( "No digit string! " );             return
0 ;         }         List<String> digitList =
new ArrayList<String>(Arrays.asList(digitArray)); // Convert to list for easy operation         List<String> positiveDigitList = fetchContinueDigitByPositiveSort(digitList,
true ); // Obtain a positive continuous number, such as 1234,345.         List<String> backDigitList = fetchContinueDigitByPositiveSort(digitList,
false ); // Obtain the reverse continuous number, for example, 4321,765.         List<String> lastList =
new ArrayList<String>();         lastList.addAll(positiveDigitList);         lastList.addAll(backDigitList);         int
maxLength = 0 ;         String maxValue =
"" ;         List<String> endStrList =
new ArrayList<String>();         for
(String s : lastList) {         // Find the longest and last value and length             if
(s.length() > maxLength) {                 maxLength = s.length();             }         }         for
(String s : lastList) {         // Find the longest and last value and length             if
(s.length() == maxLength) {                 endStrList.add(s);             }         }         int
startIndex = 0 ;         for
(String s : endStrList) {             if
(intputStr.lastIndexOf(s) >= startIndex) {                 startIndex = intputStr.lastIndexOf(s);                 maxValue = s;                 maxLength = s.length();             }         }         System.out.println( "The maximum continuous numeric string is :"
+ maxValue);         System.out.println( "The length is :"
+ maxLength);         return
maxLength;     }      private
static List<String> fetchContinueDigitByPositiveSort(List<String> digitList,
boolean isPositive) {         List<String> lastDigitList =
new ArrayList<String>();         for
(String s : digitList) {             if
(s.equals( "" )) {                 continue ;             }             char [] charArray = s.toCharArray();             int
start = Integer.parseInt(String.valueOf(charArray[ 0 ]));             int
begin = Integer.parseInt(String.valueOf(charArray[ 0 ]));             int
startIndex = 0 ;             if
(charArray.length == 1 ) {                 lastDigitList.add(String.valueOf(start));                 return
lastDigitList;             }             for
( int
j =
1 ; j < charArray.length; j++) {                 if
(isPositive) {                     if
(begin + 1
== Integer.parseInt(String.valueOf(charArray[j])) && j != charArray.length -

1 ) {                         begin = begin +
1 ;                         continue ;                     }                 }
else {                     if
(begin - 1
== Integer.parseInt(String.valueOf(charArray[j])) && j != charArray.length -

1 ) {                         begin = begin -
1 ;                         continue ;                     }                 }                 begin = Integer.parseInt(String.valueOf(charArray[j]));                 if
(start == begin) {                     lastDigitList.add(String.valueOf(start));                 }
else if
(j == charArray.length - 1
&& ((isPositive && Integer.parseInt(String.valueOf(charArray[j])) -
1 == Integer.parseInt(String.valueOf(charArray[j -
1 ])))                         || (Integer.parseInt(String.valueOf(charArray[j])) +
1 == Integer.parseInt(String.valueOf(charArray[j -
1 ]))))) {                     lastDigitList.add(String.valueOf(charArray).substring(startIndex, j +
1 ));                 }
else {                     lastDigitList.add(String.valueOf(charArray).substring(startIndex, j));                     if
(j == charArray.length - 1 ) {                         lastDigitList.add(String.valueOf(charArray[j]));                     }                 }                 start = begin;                 startIndex = j;             }          }         return
lastDigitList;      }      public
static void
main(String[] arg) {
        Continumax( "54321test123457 " ,
null );     }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.