Sort Letters by case

Source: Internet
Author: User

Given a string which contains only letters. Sort it by lower case first and upper case second.

Example

"abAcD" for, a reasonable "acbAD" answer is

With the negative numbers in front, the positive numbers are placed in the following topic.

Time Complexity of O (n)

Find the first capital letter, record its subscript as I, then the lowercase letter must appear after I, after I find the first occurrence of the lowercase letter J. Exchange I, J. Note: The letters between I and J must be uppercase. Proceed in turn.

1  Public classSolution {2     /** 3      *@paramchars:the Letter Array-should sort by case4      *@return: void5      */6      Public voidSortletters (Char[] chars) {7         if(Chars = =NULL|| Chars.length = = 0) {8             return;9         }Ten         inti = 0; One          for(i = 0; i < chars.length; i++) { A             if(Chars[i] >= ' A ' && chars[i] <= ' Z ') { -                  Break; -             } the         } -          for(intj = i + 1; J < Chars.length; J + +) { -             if(Chars[j] >= ' A ' && chars[j] <= ' z ') { -                 Chartemp =Chars[i]; +Chars[i] =Chars[j]; -CHARS[J] =temp; +i++; A             } at         } -         return; -     } -}

Sort Letters by case

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.