查找
查找
2023-06-13 09:14:28 时间
LeetCode 349 Intersection Of Two Arrays 1
题目描述
题解:
1、循环一个列表,循环的值in判断另一个数组是否包含,如果包含set进行add
class Solution(object):
def intersection(self, nums1, nums2):
rdata = set();
nums1 = set(nums1)
for i in nums1:
if i in nums2:
rdata.add(i)
return rdata
2、使用set原生的&获得交集
class Solution(object):
def intersection(self, nums1, nums2):
nums1 = set(nums1);
nums2 = set(nums2);
return nums1 & nums2
LeetCode 242 Intersection Of Two Arrays 2
题解1:将字符串解析成dist,如果有重复的数值+1最后对比两个字典
class Solution(object):
def isAnagram(self, s, t):
if len(s) != len(t):
return 1 != 1;
sd = {}
for i in s:
if len(sd) == 0:
sd[i] = 1
else:
if i in sd.keys():
sd[i] = sd[i] + 1;
else:
sd[i] = 1
td = {}
for i in t:
if len(td) == 0:
td[i] = 1
else:
if i in td.keys():
td[i] = td[i] + 1;
else:
td[i] = 1
return sd == td
题解2:使用计数工具Python Counter()
class Solution(object):
def isAnagram(self, s, t):
from collections import Counter
s = Counter(s)
t = Counter(t)
return s == t
LeetCode 202 Happy number
题解:
class Solution(object):
def isHappy(self, n):
times = 0
while times <= 50:
n = sum([int(i)**2 for i in str(n)])
times += 1
if n == 1:
break
return n == 1
LeetCode 205 Isomorphic Strings
题解
class Solution(object):
def isIsomorphic(self, s, t):
for i in range(len(s)):
if s.index(s[i]) != t.index(t[i]):
return False
return True
LeetCode 451 Sort Characters By Frequency451
题解
class Solution(object):
def frequencySort(self, s):
lis = collections.Counter(s).most_common()
strs = ''
for i,j in lis :
strs += i*j
return strs
LeetCode 1 Two Sum
题解
class Solution(object):
def twoSum(self, nums, target):
l=len(nums)
if not l:
return []
nums,ind=zip(*sorted(zip(nums,list(range(l)))))
i=0
j=l-1
while i<j:
if nums[i]+nums[j]==target:
return [ind[i],ind[j]]
elif nums[i]+nums[j]>target:
j-=1
else:
i+=1
return []
LeetCode 15 3Sum
题解
class Solution(object):
def threeSum(self, nums):
if not nums or len(nums) < 3:
return []
nums.sort()
res = []
for i in range(len(nums)):
if nums[i] > 0:
return res
if i > 0 and nums[i] == nums[i-1]:
continue
L = i + 1
R = len(nums) - 1
while R > L:
if nums[i] + nums[L] + nums[R] == 0:
res.append([nums[i], nums[L], nums[R]])
while R > L and nums[L] == nums[L+1]:
L = L + 1
while R > L and nums[R] == nums[R - 1]:
R = R - 1
L = L + 1
R = R - 1
elif nums[i] + nums[L] + nums[R] > 0:
R = R - 1
else:
L = L + 1
return res
相关文章
- Java 数组、排序和查找(1)
- 三款文本批量查找替换工具
- 数据结构实验之查找一:二叉排序树 (SDUT 3373)
- 中数据深入Oracle:查找表中隐藏的数据(oracle查找表)
- 快速找到需要的文件:Linux系统查找技巧(linux系统查找文件)
- linux使用find命令查找指定文件(linux查找某文件命令)
- 深入理解SQL Server:查找表名(sqlserver查表名)
- 找SQLServer中如何快速查找所需信息(sqlserver中查)
- MySQL如何查找重复数据?(mysql查找数据重复)
- Oracle 数据库中查找空值的方法(oracle中如何查空值)
- 文件Oracle 下载失败查找不到文件(oracle下载后找不到)