)set-----查找元素是否存在在迭代器中:
先将迭代器转化为集合,在使用代码:‘元素’ in 集合
2)dict-----查找对应关系:
通常用于查找元素在迭代器中出现了几次
3)map-----改变映射关系:
将原有关系序列统一改变为其它
class Solution():
def intersection_of_two_nums(self,nums1,nums2):
nums1=set(nums1)
return [i for i in nums1 if i in nums2]
#测试函数
my_intersection=Solution()
print(my_intersection.intersection_of_two_nums([1,2,2,1],[2,2]))
class Solution():
def intersection_of_two_nums(self,nums1,nums2):
from collections import Counter
nums1_dict=Counter(nums1)
res=[]
for num in nums2:
if nums1_dict[num]>0:
res.append(num)
nums1_dict[num]-=1
return res
#测试函数
my_intersection=Solution()
print(my_intersection.intersection_of_two_nums([1,2,2,1],[2,2]))
class Solution():
def ishappy(self,n:int)->bool:
already=set()
while n!=1:
sum=0
while n>0:
temp=n%10
sum+=temp**2
n//=10
if sum in already:
return False
else:
already.add(sum)
n=sum
return True
#测试函数
my_intersection1=Solution()
print(my_intersection1.ishappy(19))
my_intersection2=Solution()
print(my_intersection2.ishappy(101))
运算得到的数中,有和之前历史上得到的数有重合时,该数为无限循环数。
while n>0: #从后往前取位数
#取最后一位
tmp=n%10
#去掉最后一位
n//=10
class Solution():
def isAnagram(self,s:str,t:str)->bool:
from collections import Counter
s=Counter(s)
t=Counter(t)
if s==t:
return True
else:
return False
#测试函数
s='anagram'
t='nagaram'
my_anagram=Solution()
print(my_anagram.isAnagram(s,t))
代码1:
class Solution():
def wordpattern(self,s:str,pattern:str)->bool:
s=s.split()
n1=len(s)
n2=len(pattern)
if n1==n2:
n=n1
dic={}
for i in range(0,n):
if pattern[i] not in dic.keys():
dic[pattern[i]]=s[i]
else:
if dic[pattern[i]]!=s[i]:
t=False
break
else:
t=True
return t
#测试函数
s='dog cat cat dog'
pattern='abba'
my_word=Solution()
print(my_word.wordpattern(s,pattern))
代码2:
class Solution():
def wordpattern(self,s:str,pattern:str)->bool:
s=s.split()
return list(map(pattern.index,pattern))==list(map(s.index,s))
#测试函数
s='dog cat cat dog'
pattern='abba'
my_word=Solution()
print(my_word.wordpattern(s,pattern))
1.s是分隔符为空格的字符串,必须先把它的单词一个个分离出来,所以用到了split()函数;
map(function,iterable):对iterable中的每一个元素调用函数function,并将得到的函数值组成一个同类型的iterable返回。
eg.
def square(x):
return x**2
print(list(map(square,[1,2,3,4,5])))
map是通过hash存储的,不能直接进行比较,需要转换为list比较list.
class Solution():
def isIsomorphic(self,s:str,t:str)->bool:
return list(map(t.index,t))==list(map(s.index,s))
#测试函数
s='egg'
t='add'
my_string=Solution()
print(my_string.isIsomorphic(s,t))
s='foo'
t='bar'
my_string=Solution()
print(my_string.isIsomorphic(s,t))
class Solution():
def frequencySort(self,s:str)->str:
from collections import Counter
s_dict=Counter(s)
s=sorted(s_dict.items(),key=lambda item:item[1],reverse=True)
res=''
for key,value in s:
res+=key*value
return res
#测试函数
s='tree'
my_string=Solution()
print(my_string.frequencySort(s))
# 对value值由大到小排序
s = sorted(s_dict.items(), key=lambda item:item[1], reverse = True)
# 对key由小到大排序
s = sorted(s_dict.items(), key=lambda item:item[0])
顺序查找,二分查找,双指针
使用顺序:先使用顺序查找,再想另外两个方法
代码1:
class Solution():
def searchnum(self,nums,s:int)->int:
dic={}
n=len(nums)
lst=[i for i in range(n)]
for i in range(n):
dic[nums[i]]=lst[i]
if s in nums:
return dic[s]
else:
if (nums[0]nums[n-1]) or (nums[0]>nums[1] and snums[i] and snums[1] and snums[i+1]):
return i+1
break
#测试函数
nums=[1,3,5,6]
s=5
my_num=Solution()
print(my_num.searchnum(nums,s))
nums=[1,3,5,6]
s=2
my_num=Solution()
print(my_num.searchnum(nums,s))
nums=[1,3,5,6]
s=7
my_num=Solution()
print(my_num.searchnum(nums,s))
代码2:(二分查找)
class Solution:
def searchInsert(self, nums, target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
#测试函数
nums=[1,3,5,6]
target=5
mysearch=Solution()
print(mysearch.searchInsert(nums,target))
nums=[1,3,5,6]
target=7
mysearch=Solution()
print(mysearch.searchInsert(nums,target))
二分查找:
class Solution():
def splitArray(self, nums,m: int) -> int:
def helper(mid):
res = tmp = 0
for num in nums:
if tmp + num <= mid:
tmp += num
else:
res += 1
tmp = num
return res + 1
lo, hi = max(nums), sum(nums)
while lo < hi:
mid = (lo + hi) // 2
if helper(mid) > m:
lo = mid + 1
else:
hi = mid
return lo
#测试函数
nums = [7,2,5,10,8]
m = 2
my_max=Solution()
print(my_max.splitArray(nums,m))
代码1:
class Solution():
def single_num(self,nums)->int:
from collections import Counter
nums=Counter(nums)
n=len(nums)
for i in range(n):
if list(nums.values())[i]==1:
return int(list(nums.keys())[i])
#测试函数
nums=[1,1,2,3,3,4,4,8,8]
my_num=Solution()
print(my_num.single_num(nums))
nums=[3,3,7,7,10,11,11]
my_num=Solution()
print(my_num.single_num(nums))
代码2:二分查找
class Solution():
def single_num(self,nums)->int:
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] == nums[mid ^ 1]:
lo = mid + 1
else:
hi = mid
return nums[lo]
#测试函数
nums=[1,1,2,3,3,4,4,8,8]
my_num=Solution()
print(my_num.single_num(nums))
nums=[3,3,7,7,10,11,11]
my_num=Solution()
print(my_num.single_num(nums))