 # [刷题记录板] [leetcode] （Dec,10）每天两道题目，配 python参考解答

20主题 586积分 586 发表于 12-10-2014 01:07 PM | 显示全部楼层 |阅读模式

### 亲！马上注册或者登录会查看更多内容！

x

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

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).

For example,
T = "ABC"
Minimum window is "BANC".

Note:
If there is no such window in S that covers all characters in T, return the emtpy string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.  python参考解答：

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
# @param k, an integer
# @return a ListNode
if k == 0:
dummy = ListNode(0)
p = dummy
count = 0
while p.next:
p = p.next
count += 1
p.next = dummy.next
step = count - ( k % count )
for i in range(0, step):
p = p.next
p.next = None

[code]class Solution:
# @return a string
def minWindow(self, S, T):
count1={}; count2={}
for char in T:
if char not in count1: count1[char]=1
else: count1[char]+=1
for char in T:
if char not in count2: count2[char]=1
else: count2[char]+=1
count=len(T)
start=0; minSize=100000; minStart=0
for end in range(len(S)):
if S[end] in count2 and count2[S[end]]>0:
count1[S[end]]-=1
if count1[S[end]]>=0:
count-=1
if count==0:
while True:
if S[start] in count2 and count2[S[start]]>0:
if count1[S[start]]<0:
count1[S[start]]+=1
else:
break
start+=1
if minSize>end-start+1:
minSize=end-start+1; minStart=start
if minSize==100000: return ''
else:
return S[minStart:minStart+minSize][/code]

### 本帖被以下淘专辑推荐:

 本版积分规则 回帖后跳转到最后一页   