String II为什么暴力只能过66%的数据

    public static int string2 (int k, String s) {
        char[] ch=s.toCharArray();
        Arrays.sort(ch);
        int max=0;
        for (int i = 0; i < 26; i++) {
            char t= (char) (i+'a');//26个字母
            max=Math.max(max,help(ch,t,k));//维护max
        }
        return max;
    }

    //26个字母分别求对应的最长相等子序列
    private static int help(char[] ch,char c,int k){
        int count=0;
        int temp=0;
        int sum=0;
        for (int i = 0; i < ch.length; i++) {
            temp=Math.abs(ch[i]-c);
            sum+=temp;//操作的次数
            if (sum > k) {//如果操作次数大于k了跳出循环
                break;
            }
            count++;//每次++
        }
        return count;
    }


谁知道怎么回事啊,感觉代码没问题,但是不能过全部数据

全部评论
排序应当在确定是哪一个字母(26)之后,按照距离的大小从小到大,不然就可能先计算操作次数的太大超过了k,实际上后面还有可以处理的
点赞 回复 分享
发布于 2020-12-04 23:20

相关推荐

烤点老白薯:这种东西到时候公众号搜索都有的
点赞 评论 收藏
分享
喜欢疯狂星期四的猫头鹰在研究求职打法:短作业优先
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务