1.4 2800.3800
本来不是很有信心,哦不,是做好了陪跑的准备。因为最后10分了还有错,觉得没戏了,结果哈哈哈,天道好轮回,苍天绕过谁,哦不,死气白咧,必有回响。废话不多说,开始分析下这个仅存的硕果。感觉思路是没毛病的,所以是基本python知识点不熟练导致的语法问题。还是要多读python的代码尤其是大段的,一定会有帮助。老铁,撸起袖子加油干
1160. Find Words That Can Be Formed by Characters
- User Accepted:2730
- User Tried:2866
- Total Accepted:2776
- Total Submissions:3902
- Difficulty:Easy
You are given an array of strings words and a string chars.
A string is good if it can be formed by characters from chars (each character can only be used once).
Return the sum of lengths of all good strings in words.
Example 1:
Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
Explanation:
The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
Example 2:
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation:
The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
Note:
1 <= words.length <= 10001 <= words[i].length, chars.length <= 100- All strings contain lowercase English letters only.
Python version:
BONUS:
1.self代表类的实例,而非类。==只有在类里面会有self出现
同理只有在类的函数里会有self这个多余参数的出现,正常的函数参数声明是不需要self的
2.注意题目给的注释对输入输出变量的描述。The type of the variable limits the function use of this
3.辨析五种基本数据类型,7种运算符类型
https://www.cnblogs.com/snaildev/p/7553087.html
https://www.cnblogs.com/snaildev/p/7544558.html
4.base on 3.different types of parentheses means different initialization type
d={}
d=[]
d=()
5.List-->intersection union complement交集并集补集运算
https://www.cnblogs.com/jlf0103/p/8882896.html
class Solution(object): def countCharacters(self, words, chars): """ :type words: List[str] :type chars: str :rtype: int """ ans=0 Dic=self.Comp(chars) for tmp in words: Dic1=self.Comp(tmp) if self.CompDic(Dic,Dic1): ans+=len(tmp) return ans def CompDic(self,dict, dict1): keys = list(set(dict).intersection(set(dict1))) if len(keys) != len(dict1): return False for i in keys: if dict1[i] > dict[i]: return False else: dict1[i] += 1 return True def Comp(self,chars): """input string return dict""" d = {} # 给出字典 for x in chars: if x in d: d[x] = d[x] + 1 else: d[x] = 1 # print(d) return d solu=Solution() n=solu.countCharacters(words=["cat","bt","hat","tree"],chars="atach") print(n)
不自知
浙公网安备 33010602011771号