2020-12-17 题目类型:简单
1
给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是 一致字符串 。
请你返回 words 数组中 一致字符串 的数目。
class Solution(object):
def countConsistentStrings(self, allowed, words):
"""
:type allowed: str
:type words: List[str]
:rtype: int
"""
ans = 0
for i in range(len(words)):
k = 1
for j in range(len(words[i])):
if words[i][j] not in allowed:
k = 0
ans += k
return ans
竟然拿到了双一百~~
2
有 n 个 (id, value) 对,其中 id 是 1 到 n 之间的一个整数,value 是一个字符串。不存在 id 相同的两个 (id, value) 对。
设计一个流,以 任意 顺序获取 n 个 (id, value) 对,并在多次调用时 按 id 递增的顺序 返回一些值。
实现 OrderedStream 类:
OrderedStream(int n) 构造一个能接收 n 个值的流,并将当前指针 ptr 设为 1 。
String[] insert(int id, String value) 向流中存储新的 (id, value) 对。存储后:
如果流存储有 id = ptr 的 (id, value) 对,则找出从 id = ptr 开始的 最长 id 连续递增序列 ,并 按顺序 返回与这些 id 关联的值的列表。然后,将 ptr 更新为最后那个 id + 1 。
否则,返回一个空列表。
class OrderedStream(object):
def __init__(self, n):
self.s = [None] * (n+2)
self.ptr = 1
def insert(self, id, value):
self.s[id] = value
ans = []
if id == self.ptr: # 首先是判断这个地方
while self.s[self.ptr] and self.ptr <= (len(self.s) - 2): #然后是在这里判断一下
ans.append(self.s[self.ptr])
self.ptr += 1
return ans