老菜鸟的笔试记录-2021/03/06

笔试记录

参加某公司的招聘,三道笔试题,两个半小时,还在第一道题debug,真的太菜了。记录一下笔试题目,慢慢积累。

  • 求一个环形字符串中包含两个 o 的最大子字符串长度,如 alolobo 中最大子字符串为aloloblxobaolaaoaa d的最大子字符串为baolaaoaalx
   public class Exam1 {
    public static void main(String[] args) {
        String str = "alolobo";
        String[] strings = new String[]{
                "alolobo",//6
                "oaaaoaaao",//8
                "o", "a",//1
                "oaaaaoa",//7
                "oaaaoaaaoaaaosx",//11
        };
        for (String s : strings) {
            System.out.println(">>>>>>>>>>");
            int result = getMaxLength(s);
            System.out.println(result);
            System.out.println(">>>>>>>>>>");

        }
    }

    /**
     * 思路:
     * 1.重构字符串,将第一位 `o` 字符之前的内容拼接到原字符串后
     * 2.遍历重构后的字符串,通过四个指针记录 `o` 字符的位置,并且记录指针移动的次数
     * 2.1 如果指针移动的次数少于等于3次,说明最大长度就是字符串本身  xxoxxxoxxx xxo
     * 2.2 如果指针移动的次数大于等于4次,说明最大长度为指针1 idx1和指针4 idx4的差值,差值还需减一表示移除第四个 `o`
     *
     * @param str 输入的字符串
     * @return 返回的长度
     */
    private static int getMaxLength(String str) {
        if (str == null || str == "") {
            return 0;
        }
        System.out.println(str);
        String reStr = str + str.substring(0, str.indexOf("o") + 1);
        System.out.println(str + ">>>>>>" + reStr);
        int idx1 = 0, idx2 = 0, idx3 = 0, idx4 = 0, count = 0, result = 0;
        for (int i = 0; i < reStr.length(); i++) {
            if (reStr.charAt(i) == 'o') {
                idx4 = idx3;
                idx3 = idx2;
                idx2 = idx1;
                idx1 = i;
                count++;
                if (count >= 4) {
                    result = Math.max(result, idx1 - idx4 - 1);
                    System.out.println("start " + idx4 + " end " + idx1 + " " + reStr.substring(idx4, idx1));
                }
            }
        }
        if (count < 4) {
            return str.length();
        }
        return result;
    }

我在网上没找到具体的题目,我也不知道是否还有漏洞,希望大神可以解答一下。

  • 给出一个字符串数组,求出字符串组合的最大长度,字符串的首字符是前一个字符串的尾字符,当组合字符串的长度一致时,优先选择字典顺序小的字符串组合
    [ a,baasb,abword,ds,kas,stopword,bdso ]
    aabwordds //9
    baasbbdso //9
    kasstopword //11
    结果为 9 时选 aabwordds
    结果为 11 时为 kasstopword
    /**
     * 思路:
     * 1.构建类似索引的结构,首字符+数组位置+尾字符---字符串
     * 2.迭代数组,取出数据构成索引,再遍历map,如果数据符合规则(首字符匹配)并且两个数据的索引不相等,则拼接索引和值存入map中
     * 3.遍历数组,查看结果字符串,找出最长的字符串,如果字符串长度相等,则找字典顺序小的
     *
     * @param strArr
     * @return
     */
    private static String getMaxStr(String[] strArr) {
        int length = strArr.length;
        if (length == 0) {
            return "";
        }
        if (length == 1) {
            return strArr[0];
        }
        Map<String, String> map = new ConcurrentHashMap<String, String>();
        for (int i = 0; i < length; i++) {
            int strLength = strArr[i].length();
            String end = strArr[i].substring(strLength - 1);
            String first = strArr[i].substring(0, 1);
            map.put(first + i + end, strArr[i]);
        }
        for (int i = 0; i < length; i++) {
            int strLength = strArr[i].length();
            String end = strArr[i].substring(strLength - 1);
            String first = strArr[i].substring(0, 1);
            String newKey = first + i + end;
            for (String key : map.keySet()) {
                if (key.endsWith(first) && !key.contains(newKey)) {
                    map.put(key + first + i + end, map.get(key) + strArr[i]);
                }
            }
        }
        int strLength = 0;
        String result = "";
        for (String key : map.keySet()) {
            String s = map.get(key);
            if (strLength < s.length()) {
                strLength = s.length();
                result = s;
            } else if (strLength == s.length()) {
                int i = result.compareTo(s);
                if (i > 0) {
                    result = s;
                }
            }
            System.out.println(key + "  " + s);
        }
        return result;
    }

自己想到的方法,感觉总是不好,不知道怎么优化,如果有其他思路的,帮忙解决下。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值