Microsoft Algorithm 100 question 25 find the longest consecutive number string

Source: Internet
Author: User

Question 25th:
Write a function whose prototype is int Continumax (char *outputstr,char *intputstr)
Function:
Find the longest consecutive number string in the string, and return the length of this string,
and send this longest number string to one of the function parameters outputstr the memory.
For example: When the first address of "abcd12345ed125ss123456789" is passed to Intputstr, the function returns 9,
Outputstr refers to a value of 123456789

Idea: Two pointers, one to save the current longest length of the variable max, and then move the pointer until the end of the string

1  PackageCom.rui.microsoft;2 3  Public classtest25_findlongestdigitstring {4 5      Public Static voidMain (string[] args) {6String s = "ABCD12345ED125SS12345678900ADB11";7         intMax =find (s);8 System.out.println (max);9     }Ten      One      Public Static intFind (String s) { A         if(NULL= = S | | S.isempty ())return0; -         Char[] chars =S.tochararray (); -         intMax = 0; the         intStart = 0, cur = 0; -         intLen =chars.length; -          while(Cur <Len) { -              while(cur < len &&!)isdigit (Chars[cur])) { +cur++; -             } +Start =cur; A              while(Cur < len &&isdigit (Chars[cur])) { atcur++; -             } -max = max > (cur-start)? Max: (cur-start); -         } -          -         returnMax; in     } -      to     Private Static BooleanIsDigit (Charc) { +         returnC >= ' 0 ' && C <= ' 9 '; -     } the}

Microsoft Algorithm 100 25 Find the longest consecutive number string

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.