49 character-Case sorting

Source: Internet
Author: User
Tags lintcode

Original title URL: https://www.lintcode.com/problem/sort-letters-by-case/description

Describe

Given a string containing only letters, sort in the order of uppercase letters followed by lowercase letters.

lowercase letters or uppercase letters they do not have to remain relative to each other in the original string.

Have you ever encountered this problem in a real interview?is aSample Example

Give "ABACD", one possible answer is "Acbad"

Challenge

Scan it in situ and complete it again.

Label sort two pointer string processing Lintcode All rights reserved Ideas: Refer to the insertion sort method, set two pointers I, J, a point to the left to adjust the lower case letter to the bottom of the next position, and the other point to the first element of the area to be processed. If the pending element is a lowercase letter, place it at I, I place the element at J, I move one bit to the right, and J moves one bit to the right. If the pending element is not a lowercase letter, I does not move, J moves one bit to the right. AC Code:
classSolution { Public:    /** @param chars:the letter Array should sort by case * @return: Nothing*/    voidSortletters (string&chars) {        //Write your code here         intn=chars.size (); intI=0;//the next position of the lower case letter area;      for(intj=0; j<n;j++)     {         if(chars[j]>='a'&&chars[j]<='Z') {swap (chars[i],chars[j]); I++; }     }    }};

49 character-Case sorting

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.