zoukankan      html  css  js  c++  java
  • 去哪儿笔试题(2012年成都站)- 马上要笔试了,做做历年的题。

    摘要:马上要笔试,做做历年的题目把= =1

    题目:

    * 2. 已知字母序列【d, g, e, c, f, b, o, a】,请实现一个函数针对输入的一组字符串 input[] = {"bed", "dog",
    * "dear", "eye"},按照字母顺序排序并打印。
    *
    * 本例的输出顺序为:dear, dog, eye, bed。

    /**
     * 2. 已知字母序列【d, g, e, c, f, b, o, a】,请实现一个函数针对输入的一组字符串 input[] = {"bed", "dog",
     * "dear", "eye"},按照字母顺序排序并打印。
     * 
     * 本例的输出顺序为:dear, dog, eye, bed。
     * 
     * @author Administrator
     * 
     */
    public class Q2 {
    	String[] table = { "d", "g", "e", "c", "f", "b", "o", "a" };
    
    	public void sort(String[] input) {
    		if (input.length == 1) {
    			System.out.println(input);
    		}
    
    		for (int i = 1; i < input.length; i++) {
    			// 插入排序
    			for (int j = i - 1; j > -1; j--) {
    				if (isHigher(input[i], input[j]) == 1
    						|| isHigher(input[i], input[j]) == 0) {
    					continue;
    				}
    				if (isHigher(input[i], input[j]) == -1) {
    					// 交换
    					String temp = input[i];
    					input[i] = input[j];
    					input[j] = temp;
    				}
    			}
    
    		}
    		for (int i = 0; i < input.length; i++) {
    			System.out.println(input[i]);
    		}
    
    	}
    
    	public int isHigher(String s1, String s2) {
    		int length = s1.length() < s2.length() ? s1.length() : s2.length();
    		for (int i = 0; i < length; i++) {
    
    			int index1 = getIndex(String.valueOf(s1.charAt(i)));
    			int index2 = getIndex(String.valueOf(s2.charAt(i)));
    			if (index1 > index2) {
    				return 1;
    			} else if (index1 < index2) {
    				return -1;
    			} else {
    				continue;
    			}
    
    		}
    		return 0;
    
    	}
    
    	public int getIndex(String s) {
    		for (int i = 0; i < table.length; i++) {
    			if (table[i].equalsIgnoreCase(s)) {
    				return i;
    			}
    		}
    		return -1;
    
    	}
    
    	public static void main(String[] args) {
    		Q2 q = new Q2();
    		String[] input = { "bed", "dog", "dear", "eye" };
    		q.sort(input);
    
    	}
    }
    

      

    计划、执行、每天高效的活着学着
  • 相关阅读:
    mongodb安装与启动
    js数组操作
    js字符串操作
    js原型对象和原型链
    Js 原型对象与原型链
    zepto
    闭包的理解
    JSON与XML的区别比较
    ajax 异步请求四个步骤
    angularJS之项目知识
  • 原文地址:https://www.cnblogs.com/huxiaoyun90/p/3360376.html
Copyright © 2011-2022 走看看