liuhuzone

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

主机名由多级域名组成,自右向左,依次是顶级域名、二级域名、三级域名…..以此类推  
例,主机名:google.com.hk

hk是顶级域名 

com是二级域名 

google是三级域名 

现在我们需要实现一个主机名的排序功能 

排序规则 

1)主机名按照域名等级排序,即先按照顶级域名排序,顶级域名相同的再按照二级域名排序,顶级和二级域名均相同的再按照三级域名排序,以此类推,直到整个主机名排序完毕 
2)如果短主机名是由长主机名从顶级域名开始的连续一个或多个域名组成,短主机名排在长主机名前面。例:google.com 排在gmail.google.com 之前 
3)每一级域名按照字典顺序排序,字典顺序定义见下页 

输入确保符合以下规则(无需检查)
1)主机名以字符串形式给出,非空串
2)主机名中仅包含小写英文字母和分隔符’.’
3)主机名中没有连续的’.’,不以’.’开始,也不以’.’结束 
3)主机名不存在重复

字典顺序定义

1、两个单词(字母按照自左向右顺序)先以第一个字母作为排序的基准,如果第一个字母相同,就用第二个字母为基准,如果第二个字母相同就以第三个字母为基准。依此类推,如果到某个字母不相同,字母顺序在前的那个单词顺序在前。 

     例:abc 排在 abf 之前 

2、如果短单词是长单词从首字母开始连续的一部分,短单词顺序在前。 

     例:abc 排在 abcd 之前

 

测试用例:

public class DemoTest extends TestCase {

	// tearDown: 在每个用例后执行一次
	protected void tearDown() {
		Demo.clear();
	}

	// 序列号越界
	public void testCase01() {

		String[] input = { "mail.huawei.com", "huawei.com", "teltalk.org",
				"google.com.hk", "imail.huawei.com" };

		for (int i = 0; i < input.length; ++i) {
			assertEquals(0, Demo.add_host_name(input[i]));
		}
		assertEquals(null, Demo.get_host_name(6));
	}

	// 样例用例:mail.huawei.com huawei.com teltalk.org google.com.hk
	// imail.huawei.com
	public void testCase02() {

		String[] input = { "mail.huawei.com", "huawei.com", "teltalk.org",
				"google.com.hk", "imail.huawei.com" };
		String[] expect = { "huawei.com", "imail.huawei.com",
				"mail.huawei.com", "google.com.hk", "teltalk.org" };

		for (int i = 0; i < input.length; ++i) {
			assertEquals(0, Demo.add_host_name(input[i]));
		}

		for (int i = 0; i < input.length; ++i) {
			assertEquals(expect[i], Demo.get_host_name(i + 1));
		}
	}

  

 

 

解答:

public final class Demo {
    
    private static List<String> nameList=new LinkedList<String>();
    /*****************************************************************************
    Description   : 添加主机名
    Input Param   : host_name 主机名字符串,非空串
    Output Param  : 无
    Return Value  : 成功返回0,失败返回-1
    *****************************************************************************/
    public static int add_host_name( String host_name)
    {
        if(host_name!=null && !"".equals(host_name)){
            int i=nameList.size()/2;
            int min=0;
            int max=nameList.size();
            String[] inserts=host_name.split("[.]");
out:            while(true){
                if(i==max){
                    break out;
                }
                String[] befores=nameList.get(i).split("[.]");
                
                int x=0;
                char[] beforechars=null;
                char[] insertchars=null;
                
                //将分隔后的String挨个字符比较,若当前String包含字符前一部分全相同则比较长度,否则继续循环
                while(x<Math.min(befores.length, inserts.length)){
                    String before=befores[befores.length-1-x];
                    String insert=inserts[inserts.length-1-x];
                    
                    beforechars=before.toCharArray();
                    insertchars=insert.toCharArray();
                    
                    for(int y=0;y<Math.min(beforechars.length, insertchars.length);y++){
                        if(beforechars[y]==insertchars[y]){
                            continue;
                        }else if(beforechars[y]>insertchars[y]){
                            max=i;
                            i=(i+1+min)/2;
                            continue out;
                        }else if(beforechars[y]<insertchars[y]){
                            min=i;
                            i=(i+1+max)/2;
                            continue out;
                        }
                    }
                    if(beforechars.length>insertchars.length){
                        max=i;
                        i=(i+1+min)/2;
                        continue out;
                    }else if(beforechars.length<insertchars.length){
                        min=i;
                        i=(i+1+max)/2;
                        continue out;
                    }
                    x++;
                }
                if(befores.length>inserts.length){
                    max=i;
                    i=(i+1+min)/2;
                    continue out;
                }else if(befores.length<inserts.length){
                    min=i;
                    i=(i+1+max)/2;
                    continue out;
                }
            }
            nameList.add(i,host_name);
            return 0;
        }
        return -1;
    }

    /*****************************************************************************
    Description   : 获取主机名
    Input Param   : serial_number  排序后的序列号,从1开始
    Return Value  :  主机名字符串
    *****************************************************************************/
    public static  String get_host_name(int serial_number)
    {

        /* 在这里实现功能 */
        if(nameList!=null && nameList.size()>=serial_number){
            return nameList.get(serial_number-1);
        }
        return null;
    }

    /*****************************************************************************
    Description   : 清空所有主机名
    Input Param   : 无
    Output Param  : 无
    Return Value  : 无
    *****************************************************************************/
    public static  void clear()
    {
        nameList=new LinkedList<String>();
        /* 在这里实现功能 */
        
    }

 

posted on 2013-09-22 20:38  刘浒  阅读(1062)  评论(0编辑  收藏  举报