360笔试题(子串最多的次数 + 散步)(ac1.64%)

1. 子串出现最多的次数。
解法1:暴力解法通过64,求所有的子串,然后用map来计数。
 static void solve() throws IOException {
        String line = nextLine();
        int count = 0, maxCount = 0;
        HashMap<String, Integer> map = new HashMap<>();

        for (int i = 1; i <= line.length(); i++) {
            for (int j = 0; j < line.length(); j++) {
                if (j + i > line.length()) break;
                String key = line.substring(j, j+i);
                count = map.getOrDefault(key, 0) + 1;
                maxCount = Math.max(count, maxCount);
                map.put(key, map.getOrDefault(key, 0) + 1);
            }
        }

        System.out.println(maxCount);
    }
解法2:巧解:统计字符出现频率即可,ac100%。
static void solve() throws IOException {
        String line = nextLine();
        int maxCount = 0;
        char[] chars = line.toCharArray();
        
        int[] bucket = new int[26];
        for (int i = 0 ; i < chars.length; i++) {
            bucket[chars[i]-'a'] ++;
            maxCount = Math.max(maxCount, bucket[chars[i]-'a']);
        }
        
        System.out.println(maxCount);
    }

第2题,散步。
你懂得


#360公司##笔试题目#
全部评论
第二题 回溯有什么问题吗
点赞 回复
分享
发布于 2019-08-31 18:12
第一个解法我也没通过,最后就用的第二个解法😂
点赞 回复
分享
发布于 2019-08-31 18:31
联易融
校招火热招聘中
官网直投

相关推荐

点赞 5 评论
分享
牛客网
牛客企业服务