【Python】459.重复的子字符串(字典学习!!)

# 字典的添加与创建:
# s = "ababba"
if len(s)==1:
    return False
dict = {}
for i in s:
    if i not in dict.keys():
        dict[i] = 1
    else:
        count = dict[i]
        count += 1
        dict[i] = count
print(dict) # {'a': 3, 'b': 3}
print(dict.keys()) # dict_keys(['a', 'b'])
print(dict.items()) # dict_items([('a', 3), ('b', 3)])
print(dict.values()) # dict_values([3, 3])
tmp = list(dict.values())
print(tmp) # [3, 3]

字典批量初始化

name = ['a','b','c']
dicta = dict.fromkeys(name,0)
print(dicta)
#{'a': 0, 'b': 0, 'c': 0}
p = ['name','id']
dict = {'name':'sh','id':2,'flag':True}
print([dict.pop(key) for key in p])
# ['sh', 2]

 


虽然这道题不用字典。 

  • i从1循环至字符串长度的一半,所有字符串长度能整除的i即代表所有可能的子字符串长度;
  • 判断子字符串延长给定倍数后是否等于原字符串;

【Python】459.重复的子字符串(字典学习!!)_第1张图片

# i从1循环至字符串长度的一半,所有字符串长度能整除的i即代表所有可能的子字符串长度;
# 判断子字符串延长给定倍数后是否等于原字符串;

class Solution(object):
    def repeatedSubstringPattern(self, s):
        """
        :type s: str
        :rtype: bool
        """
        for i in range(1,len(s)//2+1):
            if len(s)%i == 0:
                if s[:i]*(len(s)//i) == s:
                    return True

        return False

参考的:

【Python】459.重复的子字符串(字典学习!!)_第2张图片

你可能感兴趣的:(leetcode,编程语言,leetcode解题记录)