首页 > 爱奇艺算法岗的笔试题就硬尬么???
头像
宝石
编辑于 2020-08-23 16:55
+ 关注

爱奇艺算法岗的笔试题就硬尬么???

本来看Death Note呢,想起来有笔试,结果做着有些浪费时间了。。。
第一题代码尬过去了,我都觉得无语。
public static String getRes(String sentence, String[] word2) {

    StringBuilder sent = new StringBuilder();
    Map<Integer, String> map = new HashMap<>();
    String[] word1 = sentence.split("\\s+");

    for (String word : word1)
        sent.append(word);

    for (String word : word2) {

        int idx = sent.indexOf(word, 0);
        while (idx != -1) {

            map.put(idx, word);
            idx = sent.indexOf(word, idx + 1);
        }
    }


    StringBuilder res = new StringBuilder();

    int cur = 0, start = 0;

    for (int i = 0; i < sentence.length(); i++) {

        char ch = sentence.charAt(i);

        if (map.containsKey(start)) {

            res.append(" " + map.get(start) + " ");
            start += map.get(start).length();
        } else {
            res.append(ch);

            if (ch != ' ') {

                cur++;
                start++;
            }
        }

        boolean flag = false;

        while (cur < start) {

            flag = true;

            if (sentence.charAt(i) != ' ')
                cur++;

            i++;
        }

        if (flag)
            i--;
    }

    return res.toString().trim();
}
枉我第一题还写了半天,看其他朋友说直接输出就可以AC?
第二题不给行数?
Scanner scan = new Scanner(System.in);
int n = Integer.parseInt(scan.nextLine());
System.out.println(scan.nextLine());
我直接尬过去33%

全部评论

(5) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐