本篇就是题主无聊刷LeetCode的记录,各种奇淫巧计:
美团2023年9月2日秋招第4场算法第4题
题目描述:
给定长度为n的数组,从中删除k个元素使得剩下的n-k元素两两之间存在倍数关系,给出有多少中删除方法
思路分析:
其实说有多少种删除方法,无非就是能够生成多少个给定长度符合要求的列表
1、因此我们从一开始将所有元素单列为一个列表
2、然后以递归的手法不断地进行列表合并,直到列表长度到达限制的长度,直接弹出
3、由于前面是无序可重复,因此最后需要校验去重,所以主要的是Counter操作带来的好处
代码思路:
from collections import Counter
#这里需要做下去重复
def remove_same(child_list):
huancun=[child_list[0]]
for i in range(len(child_list)):
test_list=[]
for j in range(len(huancun)):
test=Counter(child_list[i]+huancun[j])
if sum(test.values())==2*len(test.keys()):
test_list.append(1)
else:
test_list.append(0)
if sum(test_list)==0:
huancun.append(child_list[i])
return huancun
#递归生成,事实上就是不断地merge
def child(child_list:[list],limit:int)->int:
Result=[]
for i in range(len(child_list)):
child_list_s=child_list[i+1:len(child_list)]
for j in range(len(child_list_s)):
test=child_list[i]+child_list_s[j]
if len(set(test))<=len(test)-1 or len(test)==2:
child_set = Counter(test)
child_R = [k for k in child_set.keys() if child_set[k] == 1]
if child_R!=[] and len(child_R)==2:
if child_R[0] % child_R[1] == 0 or child_R[1] % child_R[0] == 0:
Result.append(list(child_set.keys()))
child_list=Result
if len(child_list[0])==limit:
huancun=remove_same(child_list)
return len(huancun)
else:
return child(child_list,limit)
D_limit=3
input=[1,4,2,3,6,7,12]
start=[[input[i]] for i in range(len(input))]
print(child(start,3))
两个数之和:
class Solution:
def twoSum(self,nums: List[int], target: int) -> List[int]:
result=[0,0]
for i in range(len(nums)):
try:
j=nums.index(target - nums[i])
if i!=j:
result[0]=i
result[1]=j
except Exception as e:
continue
if sum(result)!=0:
break
return result
结果:
最大回文序列查询:
class Solution:
def longestPalindrome(self, s: str) -> str:
if len(set(s))==1:
return s
elif s[::-1]==s:
return s
elif len(s)==1:
return s
elif len(set(s))==len(s):
return s[0]
else:
result_huancun=[]
for i in range(len(s)):
if s.count(s[i])>1:
huancun=s[i]
for j in range(len(s)):
if j>i and s[i]!=s[j]:
huancun+=s[j]
elif j>i and s[i]==s[j]:
huancun+=s[j]
if huancun[::-1]==huancun:
result_huancun.append([huancun,len(huancun)])
if i==len(s)-1 and len(huancun)==1 and result_huancun==[]:
result = s[0]
return result
return sorted(result_huancun,key=lambda x:x[1])[-1][0]
结果:
执行用时:2516 ms, 在所有 Python3 提交中击败了51.84%的用户
内存消耗:110 MB, 在所有 Python3 提交中击败了5.08%的用户
整数反转:
class Solution:
def reverse(self, x: int) -> int:
if -2147483648 < x < 2147483647 and x!=0:
index=str(x)
if '-' in index:
Q= 0-int(index.replace('-','')[::-1])
else:
Q= int(index[::-1])
if -2147483648<Q< 2147483647:
return Q
else:
return 0
else:
return 0
结果:
执行用时:32 ms, 在所有 Python3 提交中击败了96.27%的用户
内存消耗:15.9 MB, 在所有 Python3 提交中击败了41.98%的用户
盛最多水的容器:
class Solution:
def maxArea(self, height: List[int]) -> int:
l,r,res=0,len(height)-1,0
while l<r:
if height[l]<height[r]:
res=max(height[l]*(r-l),res)
l+=1
else:
res=max(height[r]*(r-l),res)
r-=1
return res
结果:
执行用时:160 ms, 在所有 Python3 提交中击败了71.75%的用户
内存消耗:26.5 MB, 在所有 Python3 提交中击败了33.98%的用户
最长公共子前缀:
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if list(set(strs))==[""]:
return ""
elif len(strs)==1 or len(list(set(strs)))==1:
return strs[0]
else:
max_len = max([len(strs[i]) for i in range(len(strs))])
buqi = [strs[i] + '0' * (max_len - len(strs[i])) if len(strs[i]) < max_len else strs[i] for i in range(len(strs)) ]
string = ''
for i in range(len(buqi[0])):
check = [buqi[j][i] for j in range(len(buqi))]
if len(set(check)) != 1:
return string
else:
string += check[0]
结果:
执行用时:36 ms, 在所有 Python3 提交中击败了88.88%的用户
内存消耗:16.2 MB, 在所有 Python3 提交中击败了10.73%的用户