记录自己的周赛水平!
这一周没怎么刷题,做题水平下降了!
1309. Decrypt String from Alphabet to Integer Mapping
题意:把数字字符串如s = “10#11#12”,转化为字母字符串"jkab"。转化为规则是:
- Characters (‘a’ to ‘i’) are represented by (‘1’ to ‘9’) respectively.
- Characters (‘j’ to ‘z’) are represented by (‘10#’ to ‘26#’) respectively.
很简单的题:考点就是字典dict, 只要把数字和字母一一映射就行了!
class Solution(object):
def freqAlphabets(self, s):
dicts = {'1':'a','2':'b','3':'c','4':'d','5':'e','6':'f','7':'g','8':'h','9':'i','10#':'j','11#':'k','12#':'l','13#':'m','14#':'n','15#':'o','16#':'p','17#':'q','18#':'r','19#':'s','20#':'t','21#':'u','22#':'v','23#':'w','24#':'x','25#':'y','26#':'z'}
ans = ""
i = 0
while i < (len(s)-2):
if s[i] != '#':
if s[i+2] == '#':
ans += dicts[s[i:i+3]]
i += 3
else:
ans += dicts[s[i]]
i += 1
# 处理最后两个字符
if s[-1] != '#':
if s[-2] != '#':
ans += dicts[s[-2]]
ans += dicts[s[-1]]
return ans
1310. XOR Queries of a Subarray
题意:
给定一维数组arr, 再给一个二维数组queries, 里面每一行为arr数组的下标范围,求arr[i]~arr[j]的异或结果。
具体请看以下的输入输出及解释!
Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
Output: [2,7,14,8]
Explanation:
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
思路:
我感觉就是考线段树,但是这部分的知识我还没复习到,所以不会做!
311. Get Watched Videos by Your Friends
题意:
给定n个人,每一个人的编号是0~n-1, 再给定两个数组,分别是watchedVideos 和friends,id = i 时,watchedVideos[i]代表编号为 i 的人看过的视频, friends[i] 代表编号为 i 的人的朋友的编号,给定id和level, 求编号为id的人他的第level层的朋友所看过的视频,并且按照视频次数排序,若次数相同,则按照字典序排序!
思路:
一开始我没看懂题目,静下心来读了两遍才读懂,就是从编号为id这个人出发,求他的第level层的朋友,然后求他的朋友看过的视频,再排序输出就行!
考点是层次遍历,然后就是排序!
class Solution(object):
def watchedVideosByFriends(self, watchedVideos, friends, id, level):
queue = [id] # 第一层
vis = set()
L = 0
width = 1 # 记录每一层的节点数量!
while queue:
width -= 1
id1 = queue.pop()
vis.add(id1)
for id2 in friends[id1]:
if id2 not in vis: # 扩展下一层节点,先进先出为队列
queue = [id2] + queue
vis.add(id2)
if width ==0 : # 下一层了
L += 1
width = len(queue)
if L == level:
break
movies = []
for id in queue:
for m in watchedVideos[id]:
movies.append(m)
# 如何先按次数排序,次数相同再按照字典序排序啊
dict_movies = collections.Counter(movies)
ans = [k for k, v in sorted(dict_movies.items(), key=lambda item: (item[1],item[0]))]
return ans
1312. Minimum Insertion Steps to Make a String Palindrome
题意:
给定一个字符串s, 你可以在任何位置插入任何字母,然后使得字符串s变成回文串!求最少插入次数。
思路:
感觉这道题不是很难啊,