49. Sort Letter by Case

Leave a comment

August 21, 2016 by oneOokay

我自己的想法是正确的,two pointers,但是我的implementation 比较丑陋,就不放了。 可以参考九章的进行改进。应该可以作为一个two pointers 的模板背下来吧。

public void sortLetters(char[] chars) {
//write your code here
if (chars == null || chars.length <= 1) {
return;
}

int start = 0;
int end = chars.length – 1;
char tmp;
while (start < end) {
while (start < end && Character.isLowerCase(chars[start])){
start ++;
}
while (start < end && Character.isUpperCase(chars[end])){
end –;
}

if(start < end) {
tmp = chars[start];
chars[start] = chars[end];
chars[end] = tmp;
start ++;
end –;
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: