当前位置: 首页 > 面试经验 >

去哪儿客户端9.7

优质
小牛编辑
142浏览
2023-03-28

去哪儿客户端9.7

第一题

解析url,在参数中找出key为hybridId对应的value

public static void main(String[] args) {
        String url  = "qunarphone://react/open?hybridId=gl_home_rn&pageName=Home&initProps=%7B%22param%22%3A%7B%22bd_source%22%3A%22Platform_City%22%2C%22distId%22%3A299782%7D%7D";
        String query = url.substring(url.indexOf("?")+1);
        String[] querys = query.split("&");
        String res = "unknown";
        for (int i=0; i<querys.length; i++){
            String key = querys[i].substring(0, querys[i].indexOf("="));
            if (key.equals("hybridId")){
                res = querys[i].substring(querys[i].indexOf("=")+1);
            }
        }
        System.out.println(res);
    }

第二题

布局问题
我的思路是每3(M)个中,小于等于6的直接输出,余下的所有布局输出长度为:剩余总长除以剩下的布局个数。
写的代码有些繁琐。。没想到更好的方法

public static void main(String[] args) {
        String[] texts = new String[]{"北京到上海的火车票", "上海到北京的飞机票", "北京周边游玩", "上海迪士尼乐园"};
        int N = 3;
        int M = 20;
        String res = "";
        for (int i=0; i<texts.length; i+=3){
            if (i!=0) res+=";";
            int l1 = texts[i].length(), l2 = 0, l3 = 0;
            if (i+1< texts.length) l2 = texts[i+1].length();
            if (i+2< texts.length) l3 = texts[i+2].length();
            if (l1+l2+l3<=M){
                if (i+2<texts.length){
//                    System.out.print(texts[i]+" ");
                    res+=texts[i]+" ";
//                    System.out.print(texts[i+1]+" ");
                    res+=texts[i+1]+" ";
//                    System.out.print(texts[i+2]);
                    res+=texts[i+2];
                }else {
//                    System.out.print(texts[i]);
                    res+=texts[i];
                    if (i+1< texts.length) res+=" " + texts[i+1];
                    if (i+2< texts.length) res+=" " + texts[2+1];
                }
            }else {
                int sum = 20;
                int cut = 3;
                if (l1<=6){ sum -= l1; cut--;}
                if (l2<=6){ sum -= l2; cut--;}
                if (l3<=6){ sum -= l3; cut--;}
                int avg = sum/cut;
                int num = sum%cut;
                for (int j=0; j<3; j++){
                    if (i+j< texts.length && texts[i+j].length()>6){
                        if (num>0){
//                            System.out.print(texts[i+j].substring(0, avg+1));
                            res+=texts[i+j].substring(0, avg+1);
                            if (i+j+1<texts.length && j<2) res+=" ";
                        }else {
//                            System.out.print(texts[i+j].substring(0,avg));
                            res+=texts[i+j].substring(0,avg);
                            if (i+j+1<texts.length && j<2) res+=" ";
                        }
                    }else if (i+j< texts.length && texts[i+j].length()<=6){
//                        System.out.print(texts[i+j]);
                        res+=texts[i+j];
                        if (i+j+1<texts.length && j<2) res+=" ";
                    }
                }
            }
        }
        System.out.println(res);
    }

第三题 最长公共子串

动态规划

public static void main(String[] args) {
        String strA = "abd";
        String strB = "abcd";
//        if (strA == null || strB == null){
//            return;
//        }
        int len1 = strA.length();
        int len2 = strB.length();
        int max = 0;
        int index = 0;
        int[][] dp = new int[len1+1][len2+1];
        for (int i=0; i<len1; i++){
            for (int j=0; j<len2; j++){
                if (strA.charAt(i) == strB.charAt(j)){
                    dp[i+1][j+1] = dp[i][j]+1;
                }
                if (max < dp[i+1][j+1]){
                    max = dp[i+1][j+1];
                    index = i+1;
                }
            }
        }
        System.out.println(strA.substring(index-max, index));
    }

顺便复习一下公共子序列

public int longestCommonSubsequence(String text1, String text2) {
        int length1 = text1.length();
        int length2 = text2.length();
        int[][] dp = new int[length1+1][length2+1];
        for (int i=1; i<=length1; i++){
            for (int j=1; j<=length2; j++){
                if (text1.charAt(i-1) == text2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        return dp[length1][length2];
    }
 类似资料: