提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
寒假决定学习一下python 为了更有效率的学习 决定不纯靠刷网课学习 而是在学习一些基础知识后找一些题来进行训练 在熟练语法的同时也做一些算法训练 下面是一些习题的做题过程和心路历程
一、题干与代码修改过程
题干:
给你两个字符串数组 words1
和 words2
,请你返回在两个字符串数组中 都恰好出现一次 的字符串的数目。
示例 1:
输入:words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"] 输出:2 解释: - "leetcode" 在两个数组中都恰好出现一次,计入答案。 - "amazing" 在两个数组中都恰好出现一次,计入答案。 - "is" 在两个数组中都出现过,但在 words1 中出现了 2 次,不计入答案。 - "as" 在 words1 中出现了一次,但是在 words2 中没有出现过,不计入答案。 所以,有 2 个字符串在两个数组中都恰好出现了一次。
示例 2:
输入:words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"] 输出:0 解释:没有字符串在两个数组中都恰好出现一次。
示例 3:
输入:words1 = ["a","ab"], words2 = ["a","a","a","ab"] 输出:1 解释:唯一在两个数组中都出现一次的字符串是 "ab" 。
初版代码:
class Solution(object):
def countWords(self, words1, words2):
""" :type words1: List[str] :type words2: List[str] :rtype: int """
lenth1 = len(words1)
lenth2 = len(words2)
i = 0
j = 0
count = 0
if lenth1 >= lenth2:
for i in range(lenth2):
for j in range(lenth1):
if words2[i] == words1[j]:
count += 1
else: continue
else:
for i in range(lenth1):
for j in range(lenth2):
if words1[i] == words2[j]:
count += 1
else:
continue
return count
值得庆幸的是有输出结果 但是也只有输出结果 有着大量的的逻辑错误
主要的错误在读题的时候没有注意是返回在两个字符串组中都恰好只有一次的数目 我的代码逻辑是如果两个列表中的单词相等就增加计数。这样做实际上是计算两个列表中所有匹配的单词总数,而不是计算只出现一次的单词。
修改思路:
首先在第二层循环内 找到第一个相同的单词就退出第二层循环 这样防止一个单词在另一个数组中出现两次 导致增加两次无效计数
在退出循环之前,检查字符串组内有没有其他位置的单词与当前位置相同 有的话则取消这次计数
具体实现:
为了实现上述功能 增加了两个函数
checkname:为了检查数组中有没有和当前下角标元素相同的单词
logicor: self.logicor(self.checksame(words1,j),self.checksame(words2,i)) 为了实现这行代码中的与运算所编写的函数 因为or的返回值是true/false 用在整数的加减法运算中可能出现问题 便设计了一个利用与运算逻辑返回值的函数 两个输入分别为两个字符串组的checksame返回值 其中有1or2个1的时候返回1,0个1返回0
if lenth1 >= lenth2:
for i in range(lenth2):
for j in range(lenth1):
if words2[i] == words1[j]:
count += 1
count = count - self.logicor(self.checksame(words1,j),self.checksame(words2,i))
break
else:
continue
def checksame(self, words, i):
j = 0
lenth1 = len(words)
for j in range(lenth1):
if words[i] == words[j] and j != i:
return 1
return 0
def logicor(self, i, j):
if i + j >= 1:
return 1
else:
return 0
完整代码
class Solution(object):
def countWords(self, words1, words2):
"""
:type words1: List[str]
:type words2: List[str]
:rtype: int
"""
lenth1 = len(words1)
lenth2 = len(words2)
i = 0
j = 0
count = 0
if lenth1 >= lenth2:
for i in range(lenth2):
for j in range(lenth1):
if words2[i] == words1[j]:
count += 1
count = count - self.checksame(words1,j) or self.checksame(words2,i)
break
else:
continue
else:
for i in range(lenth1):
for j in range(lenth2):
if words1[i] == words2[j]:
count += 1
count = count - self.checksame(words1,i) or self.checksame(words2,j)
break
else:
continue
return count
def checksame(self,words,i):
j = 0
lenth1 = len(words)
for j in range(lenth1):
if words[i] == words[j] and j != i:
return 1
return 0
def logicor(self,i,j):
if i+j>=1:
return 1
else:
return 0
二、关于语法的总结
1.定义函数时的参数
def countWords(self, words1, words2):
def checksame(self,words,i):
def logicor(self,i,j):
在定义的时候加上self代表实例本身,在类的实例方法中使用self是一种惯例。这告诉 Python 这个方法是关于对象实例的,而不是属于整个类的(也就是说,它不是一个静态方法)
在当前类中引用的时候使用self.函数名即可
三 、其他更好的解题方法
1.字典
class Solution(object):
def countWords(self, words1, words2):
"""
:type words1: List[str]
:type words2: List[str]
:rtype: int
"""
count1 = {}
count2 = {} #定义两个字典用来存放单词
count = 0 #用来计算都恰好出现一次的数目
for words in words1:
count1[words] = count1.get(words,0)+1
for words in words2:
count2[words] = count2.get(words,0)+1
for words in words1:
if count1[words] == 1 and count2.get(words) == 1:
count += 1
return count
建立两个字典 用于保存字符串组中的字符串和其在组内出现的数目
通过for循环初始化字典 初始化之后在遍历其中一个字典找到初始化为1的单词 找到后在调用get函数 检查另一个字符串组中有没有相同的单词 且只出现一次 如果符合全部条件 count+1
注:Python字典的get函数是一个非常有用的方法,它用于从字典中获取指定键(key)的值。这个函数的一个关键特性是,如果指定的键不存在于字典中,它可以返回一个默认值,而不是引发错误。这使得get方法在处理可能不存在的键时非常方便。
在代码中get函数出现的形式有如下两种:get(words,0);get(words) 指定的words如果不存在与字典中 则返回设定的值0或默认的null
还在学习其他方法 等到学到了在做补充
总结
以上便是对2085这道leetcode习题的感悟 以后也会时不时更新我的学习记录 随缘更新