@@ -320,6 +320,7 @@ def letterCombinations(self, digits):
320
320
:rtype: List[str]
321
321
dfs实践
322
322
"""
323
+
323
324
def _dfs (num , string , res ):
324
325
if num == length :
325
326
res .append (string )
@@ -396,6 +397,7 @@ def generateParenthesis(self, n):
396
397
:type n: int
397
398
:rtype: List[str]
398
399
"""
400
+
399
401
def _dfs (left , right , out , res ):
400
402
if left < 0 or right < 0 or left > right :
401
403
return
@@ -554,12 +556,11 @@ def findSubstring(self, s, words):
554
556
else :
555
557
tmp [w ] += 1
556
558
557
-
558
- for i in range (sLen - wordsLen * wordLen + 1 ):
559
+ for i in range (sLen - wordsLen * wordLen + 1 ):
559
560
current = {}
560
561
j = 0
561
562
while j <= wordsLen :
562
- word = s [i + j * wordLen :i + j * wordLen + wordLen ]
563
+ word = s [i + j * wordLen :i + j * wordLen + wordLen ]
563
564
if word not in words :
564
565
break
565
566
if word not in current :
@@ -633,8 +634,26 @@ def struct_sequence(self, B, i, j, s1, s2):
633
634
elif B [i , j ] == 0 :
634
635
self .struct_sequence (B , i - 1 , j , s1 , s2 )
635
636
637
+ # ex.递归求解字符串的子串
638
+ def getSubset (self , arr , start , end , b ):
639
+ s = ''
640
+ if start == end :
641
+ for i in range (len (b )):
642
+ if b [i ]:
643
+ s += arr [i ]
644
+ print s
645
+ print ' '
646
+ return
647
+ else :
648
+ b [start ] = False
649
+ self .getSubset (arr , start + 1 , end , b )
650
+ b [start ] = True
651
+ self .getSubset (arr , start + 1 , end , b )
652
+
636
653
637
654
if __name__ == '__main__' :
638
- s = "barfoothefoobarman"
639
- words = ["foo" , "bar" ]
640
- print Solution ().findSubstring (s , words )
655
+ from collections import defaultdict
656
+ d = defaultdict (list )
657
+ for k ,v in {'cai' :['li' ,'mc' ], 'min' :'zi' , 'chao' :'pei' }.items ():
658
+ d [k ].append (v )
659
+ print d
0 commit comments