博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 76-Minimum Window Substring(hard)
阅读量:4552 次
发布时间:2019-06-08

本文共 1246 字,大约阅读时间需要 4 分钟。

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

 

cases:

when stand on the ith position of S:

1. if the character is not in T, do nothing;

2. if the character is in T:

  start->i contains all characters in T: 

    S[start] not in T or ((frequency of S[start] in start->i) > (frequency of S[start] in T))  start++

    maxlen=max(i-start+1,maxlen), start++

 

use hashmap or int[256/128] to record the number of characters in t, int[] record is better.

 

class Solution {    public String minWindow(String s, String t) {        int[] cnum=new int[256];        int count=t.length();        for(char c:t.toCharArray()){            cnum[c]++;        }        int start=0;        int l=0;        int minlen=Integer.MAX_VALUE;        for(int r=0;r
0){ count--; } cnum[s.charAt(r)]--; if(count==0){ while(cnum[s.charAt(l)]<0){ cnum[s.charAt(l)]++; l++; } if(r-l+1

 

注意不要老是笔误s.charAt()写成s.charAt[]!

cnum的index是字母的asc码,不要误写出cnum[l/r]之类,而是cnum[s.charAt(l/r)]!

return的时候不要掉以轻心,不要忘了如果没有满足条件的解的情况!

 

转载于:https://www.cnblogs.com/yshi12/p/9689915.html

你可能感兴趣的文章
记录下zend studio 的xdebug 在调试安装
查看>>
ES6阅读笔记
查看>>
数字基带信号分类
查看>>
移动HTML5前端性能优化指南(转)
查看>>
Jq 遍历each()方法
查看>>
Android源码分析:Telephony部分–phone进程
查看>>
关于 redis.properties配置文件及rule
查看>>
WebService
查看>>
关于Java中重载的若干问题
查看>>
Java中start和run方法的区别
查看>>
23种设计模式中的命令模式
查看>>
[转载]年薪10w和年薪100w的人,差在哪里?
查看>>
shell 日期参数
查看>>
package的使用
查看>>
括号生成
查看>>
优秀的前端需要做到什么?
查看>>
aws cli command line interface的安装与使用
查看>>
10)将地址换成常量
查看>>
箭头函数
查看>>
android MVC &amp;&amp; MVP &amp;&amp; MVVM分析和对照
查看>>