def countSubstrings(self, s): count = 0 for center in xrange(2*len(s) - 1): i = center / 2 j = i + center % 2 while 0 <= i <= j < len(s) and s[i] == s[j]: count += 1 i -= 1 j += 1 return count
return all palindromic subsequences of a string. for exp: String s="abcdbac" valid palindromic subsequences are: a, b,c,d,aca,bb,bcb,cc,cac,etc note: subsequece, not substring. "24" is a subsequence of "1234","42" is not.
【在 m**********s 的大作中提到】 : just curious, LZ为啥把5D MarkII的牌牌给黑胶了?
a*a
23 楼
是有点麻烦,居然写了15行 def all_palindromic_sub_sequence(s): """ :type s: str :rtype: list[str] """ if not s: return [''] dic = collections.defaultdict(list) for i, c in enumerate(s): dic[c].append(i) ans = set() for k in dic.keys(): index_list = dic[k] ans.add(k) for i, j in itertools.combinations(index_list, 2): sub = all_palindromic_sub_sequence(s[i+1:j]) ans.update(k + w + k for w in sub) del dic[k] return list(ans)
d*u
24 楼
低调一贯是再生大神的作风
H*5
25 楼
看不懂,能翻译成java吗?
: 是有点麻烦,居然写了15行
: def all_palindromic_sub_sequence(s):
: """
: :type s: str
: :rtype: list[str]
: """
: if not s:
: return ['']
: dic = collections.defaultdict(list)
: for i, c in enumerate(s):
【在 a**a 的大作中提到】 : 是有点麻烦,居然写了15行 : def all_palindromic_sub_sequence(s): : """ : :type s: str : :rtype: list[str] : """ : if not s: : return [''] : dic = collections.defaultdict(list) : for i, c in enumerate(s):
r*v
26 楼
我考,这都看出来了。。。 ps. 清仓完了,但是又很杯具的入了个1Ds。。。这回只剩下个Sigma 28-135了。。。 发10个包子
【在 a**a 的大作中提到】 : 是有点麻烦,居然写了15行 : def all_palindromic_sub_sequence(s): : """ : :type s: str : :rtype: list[str] : """ : if not s: : return [''] : dic = collections.defaultdict(list) : for i, c in enumerate(s):
D*M
30 楼
da bz
H*5
31 楼
List results = findAllPalindromicSubsequences(" abcbabcbaxsafqwfeqcwecewfweg53wf43g35t53gr3g54ghb54"); // for(String str: results){ // System.out.println(str); //} long endTime=System.currentTimeMillis(); System.out.println("time: "+(endTime-startTime)+"ms"); 3ms 话说此题时间复杂度最优能到什么程度?
z*2
32 楼
吃,都是大神
a*a
33 楼
其实这种题就是乱编的,楼主别太在意,面试重要的还是基础概念、主流方法。Coding 随便准备一下,Leetcode高频题吃透就差不多了,大包裹还是要靠系统设计搞得扎实。 代码再精简一下,13行就够了: def all_palindromic_sub_sequence(s): if not s: return [''] dic = defaultdict(list) for i, c in enumerate(s): dic[c].append(i) ans = set() for c, index_list in dic.iteritems(): ans.add(c) for i, j in combinations(index_list, 2): sub = all_palindromic_sub_sequence(s[i+1:j]) ans.update(c + w + c for w in sub) return list(ans)