zoukankan      html  css  js  c++  java
  • 微软Java面试题按照字母排序

    1.对字符串进行排序,用任意一种编程语言来实现,不能使用现有的类,在排序中,字符串“Bc”,“Ad”,“aC”,“Hello”,“X man”,“little”,“During”,“day”能够排序成 “Ad”,"aC",“Bc”,“During”,“day”,“Hello”,“little”,“Hello”,也就是说,在排序的过程并不是传统的按照字符串排序,在排序中还需要将小写字母一并排序,也就是说a字符串要在B或b之前。

    1. import java.util.Arrays;   
    2. import java.util.Comparator;   
    3.   
    4. /**  
    5.  * 微软面试题-按照字母排序.<br>  
    6.  * 小写字母在大写后面,字母B/b在字母A/a后面<br>  
    7.  *   
    8.  * @author 老紫竹 JAVA世纪网(java2000.net)  
    9.  *   
    10.  */  
    11. class T {   
    12.   private static final int MASK = 0xFFDF// 用来把小写变成大写   
    13.   
    14.   public static int compare(String o1, String o2) {   
    15.     int length1 = o1.length();   
    16.     int length2 = o2.length();   
    17.     int length = length1 > length2 ? length2 : length1;   
    18.     int c1, c2;   
    19.     int d1, d2;   
    20.     for (int i = 0; i < length; i++) {   
    21.       c1 = o1.charAt(i);   
    22.       c2 = o2.charAt(i);   
    23.       d1 = c1 & MASK;   
    24.       d2 = c2 & MASK;   
    25.       if (d1 > d2) {   
    26.         return 1;   
    27.       } else if (d1 < d2) {   
    28.         return -1;   
    29.       } else {   
    30.         if (c1 > c2) {   
    31.           return 1;   
    32.         } else if (c1 < c2) {   
    33.           return -1;   
    34.         }   
    35.       }   
    36.     }   
    37.     if (length1 > length2) {   
    38.       return 1;   
    39.     } else if (length1 < length2) {   
    40.       return -1;   
    41.     }   
    42.     return 0;   
    43.   }   
    44.   
    45.   public static void sortByPOPO(String[] args) {   
    46.     String tmp;   
    47.     for (int i = 0; i < args.length; i++) {   
    48.       for (int j = i + 1; j < args.length; j++) {   
    49.         if (compare(args[i], args[j]) > 0) {   
    50.           tmp = args[i];   
    51.           args[i] = args[j];   
    52.           args[j] = tmp;   
    53.         }   
    54.       }   
    55.     }   
    56.     // [Ad, aC, Bc, During, day, Hello, little, X man]   
    57.     System.out.println(Arrays.toString(args));   
    58.   }   
    59.   
    60.   public static void main(String[] args) {   
    61.     String[] strs = { "Bc""Ad""aC""Hello""X man""little""During",   
    62.         "day" };   
    63.   
    64.     sortByPOPO(strs);   
    65.   
    66.   }   
    67. }  
  • 相关阅读:
    python 04 list增删改查 迷途小书童
    xhtml+css符合标准的WEB设计
    在Ubuntu16.0.4安装hipcaffe
    简单使用OpenSSL生成密钥
    Ubuntu 16.04 集成安装Apache+PHP+Kerberos+LDAP+phpLDAPadmin
    Ubuntu 安装mysql & 自定义数据存储目录
    TestLink+Jenkins在Ubuntu16.04搭建集成测试环境
    Rancher 2.1平台搭建及使用
    JavaFX_homework1_pane
    linux 命令1 转
  • 原文地址:https://www.cnblogs.com/encounter/p/2189077.html
Copyright © 2011-2022 走看看