题解 | #牛群名字覆盖#

牛群名字覆盖

https://www.nowcoder.com/practice/e6cef4702e7841f59cf47daf9bafb241

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @param t string字符串 
     * @return string字符串
     */
    public String minWindow (String s, String t) {
        // write code here
        int[] hash = new int[128];
        for (int i = 0; i < t.length(); i++) {
            hash[t.charAt(i)]--;
        }
        int slow = 0 , fast = 0;
        boolean isFind = false;
        String res = s;
        while(fast < s.length()){
            hash[s.charAt(fast)]++;
            while(check(hash)){
                isFind = true;
                if(res.length() > fast - slow + 1){
                    res = s.substring(slow , fast + 1);
                }
                hash[s.charAt(slow)]--;
                slow++;
            }
            fast++;
        }
        return isFind ? res : "";
    }

    boolean check(int[] hash){
        for (int i : hash) {
            if(i < 0)
                return false;
        }
        return true;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
2025-12-19 21:53
门头沟学院 Java
想做OpenGL:不要一来就把自己定位这么低吧,把大厂当成目标,不断去学技术做项目,最后你至少能学到能找到中小厂的技术水平,你一上来就找这种两千块还要前后端都会的,其实对你用处不会很大,真去了也是打杂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务