摘要:给定一个字符串,请将字符串里的字符按照出现的频率降序排列。 class Solution: def frequencySort(self, s: str) -> str: d={} res='' for i in s: d[i]=d.get(i,0)+1 d=sorted(d.items(),key
阅读全文
摘要:给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: dict = {} for item in strs: k
阅读全文
摘要:给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 class Solution: def isAnagram(self, s: str, t: str) -> bool: s1=list(s) s1.sort()#注意一下 s1=''.join(s1) t1=list
阅读全文
摘要:给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。 class Solution: def findTheDifference(self, s: str, t: str) -> str: # 初始化 an
阅读全文
摘要:给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1 错解: 理解错题意了 class Solution: def firstUniqChar(self, s: str) -> int: for i in range(len(s)): for j in range(i
阅读全文
摘要:错解 class Solution: def reverseWords(self, s: str) -> str: a=[] s=s.split() for i in s: if i!='': left=0 right=len(i)-1 while left <right: i[left],i[ri
阅读全文
摘要:给定一个字符串 s 和一个整数 k,你需要对从字符串开头算起的每隔 2k 个字符的前 k 个字符进行反转。 如果剩余字符少于 k 个,则将剩余字符全部反转。如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样 来源:力扣(LeetCode)链接:https://lee
阅读全文
摘要:实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,xn)。 class Solution: def myPow(self, x: float, n: int) -> float: res = 1 if n < 0: x,n = 1/x,-n while n: # 通过折半计算,每次把 n
阅读全文
摘要:作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求: 1. 你设计的矩形页面必须等于给定的目标面积。 2. 宽度 W 不应大于长度 L,换言之,要求 L >= W 。 3. 长度
阅读全文