美团2015笔试题以及最优解(四)

题目: * 一个 char 类型的数组 chs,其中所有的字符都不同。 例如,chs=['A', 'B', 'C', ... 'Z'],则字符串与整数的对应关系如下: A, B... Z, AA,AB...AZ,BA,BB...ZZ,AAA...
 * ZZZ, AAAA... 1, 2...26,27, 28... 52,53,54...702,703...18278, 18279... 例如,chs=['A', 'B', 'C'],则字符串与整数的对应关系如下:

 * A,B,C,AA,AB...CC,AAA...CCC,AAAA... 1, 2,3, 4, 5 ...12, 13 ... 39, 40... 给定一个数组 chs,实现根据对应关系完成字符串与整数相互转换的两个函数

算法思路:利用伪n进制的算法进行位数解析(QQ:3036643587 欢迎交流)

public class TransforString {

    /**
     * 
     * 功能描述:将数字转换成对应的字符串
     * 
     * @param chs 字符串数组 eg:['A','B','V']
     * @param n 待转换的数字
     * @return
     * @author jiaozhb/15072775
     */
    public static String getString(char[] chs,
                                   int n) {
        int cur = 1;// 个位数字全是1
        int base = chs.length;// 转换基数
        int len = 0;// 字符结果长度,初始默认为0
        //
        while (n >= cur) {
            len++;
            n -= cur;
            cur *= base;
        }
        char[] res = new char[len];
        int index = 0;
        int nCur = 0;
        do {
            cur /= base;
            nCur = n / cur;
            res[index++] = getCharAtValue(chs, nCur + 1);
            n %= cur;
        } while (index != res.length);
        return String.valueOf(res);

    }
    /**
     * 
     * 功能描述:将对应的字符串转换成数字
     * @param chs 元素数组
     * @param s 字符串
     * @return 对应数字
     * @author jiaozhb/15072775
     */
    public static int getInt(char[] chs,String s){
        int res = 0;
        int base = chs.length;
        int eValue = 0;
        for(int i=s.length(),j=0;i>0;i--,j++){
            eValue = getCharAtIndex(chs, s.charAt(i-1));
            res+=eValue*(int)Math.pow(base, j);
        }
        return res;
    }
    
    /**
     * 
     * 功能描述:获取数组在元素s上的位置
     * 
     * @param chs
     * @param s
     * @return
     * @author jiaozhb/15072775
     */
    public static int getCharAtIndex(char[] chs,
                                      char s) {
        String chs_1 = String.valueOf(chs);
        String s_1 = String.valueOf(s);
        int res = chs_1.indexOf(s_1);
        if(res<0){
            return 0;
        }
        return res+1;
    }
    /**
     * 
     * 功能描述:获取数组在k位置上的值
     * 
     * @param chs
     * @param k
     * @return
     * @author jiaozhb/15072775
     */
    public static char getCharAtValue(char[] chs,
                                      int k) {

        if (k < 1 || k > chs.length) {
            return 0;
        }
        return chs[k - 1];
    }

    public static void main(String[] args) {
        char[] chs = {
                'A',
                'B',
                'C'
        };
        int n = 12;
//        System.out.println(getString(chs, n));
//        System.out.println(getCharAtIndex(chs, 'A'));
        System.out.println(getInt(chs, "AA"));

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值