携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第4天,点击查看活动详情
522. 最长特殊序列 II
题目 给定字符串列表 strs ,返回其中 最长的特殊序列 的长度。如果最长特殊序列不存在,返回 -1 。
特殊序列 定义如下:该序列为某字符串 独有的子序列(即不能是其他字符串的子序列)。
s 的 子序列可以通过删去字符串 s 中的某些字符实现。
例如,"abc" 是 "aebdc" 的子序列,因为您可以删除"aebdc"中的下划线字符来得到 "abc" 。"aebdc"的子序列还包括"aebdc"、 "aeb" 和 "" (空字符串)。
示例 1:
输入: strs = ["aba","cdc","eae"] 输出: 3 示例 2:
输入: strs = ["aaa","aaa","aa"] 输出: -1
提示:
2 <= strs.length <= 50 1 <= strs[i].length <= 10 strs[i] 只包含小写英文字母
代码
class Solution:
def findLUSlength(self, strs: List[str]) -> int:
# 数据量很小,可以直接暴力
length = len(strs)
max_len = -1
def check(s1:str, s2:str) ->bool:
i = 0
j = 0
l1 = len(s1)
l2 = len(s2)
while i < l1 and j < l2:
if s1[i] == s2[j]:
i += 1
j += 1
else:
j += 1
return j==l2 and i != l1 # 如果返回值为真,说明s2匹配到末尾都没有完全匹配上s1
for i in range(length):
flag = True
for j in range(length):
if i != j:
flag &= check(strs[i], strs[j]) # 这个函数现在写
if flag:
max_len = max(max_len, len(strs[i]))
return max_len
视频
324. 摆动排序 II
题目
给你一个整数数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序。
你可以假设所有输入数组都可以得到满足题目要求的结果。
示例 1:
输入:nums = [1,5,1,1,6,4] 输出:[1,6,1,5,1,4] 解释:[1,4,1,5,1,6] 同样是符合题目要求的结果,可以被判题程序接受。 示例 2:
输入:nums = [1,3,2,2,3,1] 输出:[2,3,1,3,1,2]
提示:
1 <= nums.length <= 5 * 104 0 <= nums[i] <= 5000 题目数据保证,对于给定的输入 nums ,总能产生满足题目要求的结果 代码
class Solution:
def wiggleSort(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
arr = sorted(nums)
# 排序后截取后一半作为大于的数,前一半作为小于的数
length = len(nums)
length1 = (length + 1) // 2
j = length1 - 1 # 从小于的数里取
k = length - 1 # 从大于的数里取
for i in range(0, length, 2):
# 先取小的
nums[i] = arr[j] # 这是从后取,其实前后取无所谓的
if i + 1 < length:
nums[i+1] = arr[k]
j -= 1
k -= 1
视频
535. TinyURL 的加密与解密
题目
TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL leetcode.com/problems/de… 时,它将返回一个简化的URL tinyurl.com/4e9iAk 。请你设计一个类来加密与解密 TinyURL 。
加密和解密算法如何设计和运作是没有限制的,你只需要保证一个 URL 可以被加密成一个 TinyURL ,并且这个 TinyURL 可以用解密方法恢复成原本的 URL 。
实现 Solution 类:
Solution() 初始化 TinyURL 系统对象。 String encode(String longUrl) 返回 longUrl 对应的 TinyURL 。 String decode(String shortUrl) 返回 shortUrl 原本的 URL 。题目数据保证给定的 shortUrl 是由同一个系统对象加密的。
示例:
输入:url = "leetcode.com/problems/de…" 输出:"leetcode.com/problems/de…"
解释: Solution obj = new Solution(); string tiny = obj.encode(url); // 返回加密后得到的 TinyURL 。 string ans = obj.decode(tiny); // 返回解密后得到的原本的 URL 。
提示:
1 <= url.length <= 104 题目数据保证 url 是一个有效的 URL
代码
class Codec:
def __init__(self):
self.Dict = dict()
self.V2K = dict()
self.cur = 0
def encode(self, longUrl: str) -> str:
"""Encodes a URL to a shortened URL.
"""
idx = len(longUrl) - 1
st1 = ""
while idx >= 0 and longUrl[idx] != '/':
st1 = longUrl[idx] + st1
idx -= 1
if st1 in self.V2K.keys():
return longUrl[:idx+1] + self.V2K.get(st1)
else:
self.cur += 1
self.Dict[str(self.cur)] = st1
self.V2K[st1] = str(self.cur)
return longUrl[:idx+1] + self.V2K.get(st1)
def decode(self, shortUrl: str) -> str:
"""Decodes a shortened URL to its original URL.
"""
idx = len(shortUrl) - 1
st1 = ""
while idx >= 0 and shortUrl[idx] != '/':
st1 = shortUrl[idx] + st1
idx -= 1
return shortUrl[:idx+1] + self.Dict[st1]
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))
视频