LintCode 49. 字符大小写排序

给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。

public class Solution {
   /*
    * @param chars: The letter array you should sort by Case
    * @return: nothing
    */
   public void sortLetters(char[] chars) {
      // write your code here
      if (chars == null || chars.length == 0) {
         return;
      }
      char standard = ('Z' + 'a') / 2;
      char[] newChars = new char[chars.length+1];
      for (int i = 0; i < newChars.length; i++) {
         if (i == 0) {
            newChars[i] = standard;
         } else {
            newChars[i] = chars[i - 1];
         }
      }
      int i = 0;
      int j = newChars.length - 1;
      while (i < j) {
         while (i < j && newChars[j] <= standard) {
            j--;
         }
         if (i < j) {
            newChars[i++] = newChars[j];
         }
         while (i < j && newChars[i] >= standard) {
            i++;
         }
         if (i < j) {
            newChars[j--] = newChars[i];
         }
         newChars[i] = standard;
      }
      for (int k = 0; k < chars.length; k++) {
         if (k < i) {
            chars[k] = newChars[k];
         } else {
            chars[k] = newChars[k+1];
         }
      }
   }
}

阅读更多

没有更多推荐了,返回首页