LeetCode-76-Minimum Window Substring 尺取法+字典
2017-09-21 20:25
411 查看
class Solution(object):
def minWindow(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
Map={}
Lens=len(s)
Lent=len(t)
p=-1
OK=0
for i in range(Lent):
if t[i] not in Map:
Map[t[i]]=(1,0)#(should be,now have)
OK+=1
else:
Map[t[i]]=(Map[t[i]][0]+1,0)
for i in range(Lens):
if s[i] in Map:
Map[s[i]]=(Map[s[i]][0],Map[s[i]][1]+1)
if Map[s[i]][0]==Map[s[i]][1]:OK-=1
if OK==0:
p=i
break
if p==-1:return ""
curl=0
curr=p
Len=p+1
ansl=0
ansr=p
#print Map
while(True):
if OK==0:
if s[curl] in Map:
Map[s[curl]]=(Map[s[curl]][0],Map[s[curl]][1]-1)
if Map[s[curl]][1]<Map[s[curl]][0]:
OK+=1
curl+=1
continue
curl+=1
if curr-curl<Len:
Len=curr-curl+1
ansl=curl
ansr=curr
else:
curr+=1
if curr==Lens:
break
if s[curr] in Map:
Map[s[curr]]=(Map[s[curr]][0],Map[s[curr]][1]+1)
if Map[s[curr]][1]==Map[s[curr]][0]:
OK-=1
if OK==0:
if curr-curl<Len:
Len=curr-curl+1
ansl=curl
ansr=curr
#print ansl,ansr
return s[ansl:ansr+1]
def minWindow(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
Map={}
Lens=len(s)
Lent=len(t)
p=-1
OK=0
for i in range(Lent):
if t[i] not in Map:
Map[t[i]]=(1,0)#(should be,now have)
OK+=1
else:
Map[t[i]]=(Map[t[i]][0]+1,0)
for i in range(Lens):
if s[i] in Map:
Map[s[i]]=(Map[s[i]][0],Map[s[i]][1]+1)
if Map[s[i]][0]==Map[s[i]][1]:OK-=1
if OK==0:
p=i
break
if p==-1:return ""
curl=0
curr=p
Len=p+1
ansl=0
ansr=p
#print Map
while(True):
if OK==0:
if s[curl] in Map:
Map[s[curl]]=(Map[s[curl]][0],Map[s[curl]][1]-1)
if Map[s[curl]][1]<Map[s[curl]][0]:
OK+=1
curl+=1
continue
curl+=1
if curr-curl<Len:
Len=curr-curl+1
ansl=curl
ansr=curr
else:
curr+=1
if curr==Lens:
break
if s[curr] in Map:
Map[s[curr]]=(Map[s[curr]][0],Map[s[curr]][1]+1)
if Map[s[curr]][1]==Map[s[curr]][0]:
OK-=1
if OK==0:
if curr-curl<Len:
Len=curr-curl+1
ansl=curl
ansr=curr
#print ansl,ansr
return s[ansl:ansr+1]
相关文章推荐
- LeetCode 76 Minimum Window Substring
- leetcode 76:Minimum Window Substring
- Leetcode-76 Minimum Window Substring
- leetcode 76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- [leetcode] 76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring (尺取法 推荐)
- LeetCode 76 Minimum Window Substring(Python详解及实现)
- LeetCode_OJ【76】Minimum Window Substring
- leetcode76题 Minimum Window Substring
- LeetCode76——Minimum Window Substring
- leetcode 76: Minimum Window Substring
- leetcode 76 Minimum Window Substring/ 209 Minimum Size Subarray Sum
- Leetcode 76 Minimum Window Substring(最小窗口的子串)
- [leetcode-76]Minimum Window Substring(java)
- Leetcode 76-MinimumWindowSubstring 解题报告
- leetcode[76] Minimum Window Substring
- LeetCode76 Minimum Window Substring(两种解法)
- leetcode-76 Minimum Window Substring
- LeetCode(76) Minimum Window Substring