分享经济网站怎么建设,做网站用地图,网站的优势是什么,网站前期准备工作记录了初步解题思路 以及本地实现代码#xff1b;并不一定为最优 也希望大家能一起探讨 一起进步 目录 6/17 522. 最长特殊序列 II6/18 2288. 价格减免6/19 2713. 矩阵中严格递增的单元格数6/20 2748. 美丽下标对的数目6/21 LCP 61. 气温变化趋势6/22 2663. 字典序最小的美丽字…记录了初步解题思路 以及本地实现代码并不一定为最优 也希望大家能一起探讨 一起进步 目录 6/17 522. 最长特殊序列 II6/18 2288. 价格减免6/19 2713. 矩阵中严格递增的单元格数6/20 2748. 美丽下标对的数目6/21 LCP 61. 气温变化趋势6/22 2663. 字典序最小的美丽字符串6/23 520. 检测大写字母 6/17 522. 最长特殊序列 II check(a,b)判断b是否包含子序列a 较长的序列肯定不是短序列的子序列 将数组内序列从长倒短排序 判断某一序列是否满足独有子序列条件 def findLUSlength(strs)::type strs: List[str]:rtype: intdef check(a,b):loca,locb 0,0while localen(a) and locblen(b):if a[loca]b[locb]:loca1locb1return localen(a)strs.sort(keylambda x:len(x),reverseTrue)for i,s in enumerate(strs):tag Truefor j,t in enumerate(strs):if len(t)len(s):breakif i!j and check(s,t):tag Falsebreakif tag:return len(s)return -1 6/18 2288. 价格减免 按空格分词 判断一个单词是否是价格 如果是价格那么打折 def discountPrices(sentence, discount)::type sentence: str:type discount: int:rtype: strlsentence.split( )ans []for s in l:if s[0]$and s[1:].isdigit():v float(s[1:])*(100-discount)/100ans.append($format(v,.2f))else:ans.append(s)return .join(ans) 6/19 2713. 矩阵中严格递增的单元格数 row,col分别记录每一行 每一列的结果最大值 mp[v]记录数值v出现的位置 将数值v从小打到排序考虑 对于位置i,j 他的值为row[i] col[j]最大值1 同时更新这个位置的最大值row,col def maxIncreasingCells(mat)::type mat: List[List[int]]:rtype: intfrom collections import defaultdictmpdefaultdict(list)m,nlen(mat),len(mat[0])row [0]*mcol [0]*nfor i in range(m):for j in range(n):mp[mat[i][j]].append((i,j))for _,pos in sorted(mp.items(),keylambda x:x[0]):ans [max(row[i],col[j])1 for i,j in pos]for (i,j),d in zip(pos,ans):row[i]max(row[i],d)col[j]max(col[j],d)return max(row) 6/20 2748. 美丽下标对的数目 gcd得到两数最大公约数 m[x]记录第一个数字为x的元素个数 从前往后依次分析 def countBeautifulPairs(self, nums)::type nums: List[int]:rtype: intdef gcd(x,y):if xy:x,yy,xwhile True:x,yy,x%yif y0:return xans 0m [0]*10for num in nums:for v in range(1,10):if gcd(num%10,v)1:ans m[v]while num10:num//10m[num]1return ans 6/21 LCP 61. 气温变化趋势 使用-1表示下降 0表示平稳 1表示上升 cur 记录变化趋势连续相同的天数 def temperatureTrend(temperatureA, temperatureB)::type temperatureA: List[int]:type temperatureB: List[int]:rtype: intdef func(x,y):if xy:return 0return -1 if xy else 1n len(temperatureA)ans cur 0for i in range(1,n):a func(temperatureA[i-1],temperatureA[i])b func(temperatureB[i-1],temperatureB[i])if ab:cur 1ans max(ans,cur)else:cur 0return ans 6/22 2663. 字典序最小的美丽字符串 回文串判断s[i]!s[i-1] s[i]!s[i-2]即可 字典序最小 尽量改右侧的字符 def smallestBeautifulString(s, k)::type s: str:type k: int:rtype: strk ord(a)s list(map(ord,s))nlen(s)i n-1s[i]1while in:if s[i]k:if i0:return s[i]ord(a)i-1s[i]1elif i and s[i]s[i-1] or i1 and s[i]s[i-2]:s[i]1else:i1return .join(map(chr,s)) 6/23 520. 检测大写字母 依次判断三个条件 全是大写;全是小写;首字母大写后续小写 def detectCapitalUse(word)::type word: str:rtype: boolif wordstr.upper(word):return Trueif wordstr.lower(word):return Trueif Aword[0]Z and word[1:]str.lower(word[1:]):return Truereturn False