光电网站设计/深圳哪里有网络推广渠避
目录
1. 生成随机正整数 ※
2. 新浪微博热门话题 ※
3. 恢复二叉搜索树 🌟🌟
🌟 每日一练刷题专栏 🌟
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
标注 ※ 的题目为基础入门题
1. 生成随机正整数
生成100个2位随机正整数,按每行十个输出,并求出个位数字分别为0,1,2,3,4,5,6,7,8,9的正整数的个数
出处:
https://edu.csdn.net/practice/24565197
代码:
def fun():random_list = [random.randint(10, 99) for n in range(100)]statistics = {n: 0 for n in range(10)}for index, x in enumerate(random_list):print(x, end=' ')statistics[int(x % 10)] = statistics[int(x % 10)] + 1if ((index + 1) % 10 == 0):print()print(statistics)
if __name__ == '__main__':fun()
输出: (随机产生)
75 74 98 32 46 53 10 47 25 41
93 19 80 78 16 96 97 31 59 85
41 63 41 31 66 32 44 88 89 24
66 79 38 68 92 70 27 90 77 38
83 62 56 19 62 27 39 53 36 27
35 23 40 76 34 47 92 65 29 15
50 90 17 57 67 45 85 83 96 53
30 34 61 28 92 35 22 78 27 28
13 13 88 84 84 28 61 68 41 11
83 32 17 37 26 46 42 42 23 76
{0: 8, 1: 9, 2: 11, 3: 12, 4: 7, 5: 9, 6: 12, 7: 13, 8: 12, 9: 7}
2. 新浪微博热门话题
新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表,并将最热门的话题放在醒目的位置推荐大家关注。
本题目要求实现一个简化的热门话题推荐功能,从大量英文(因为中文分词处理比较麻烦)微博中解析出话题,找出被最多条微博提到的话题。
输入格式:
输入说明:输入首先给出一个正整数N(≤105),随后N行,每行给出一条英文微博,其长度不超过140个字符。任何包含在一对最近的#中的内容均被认为是一个话题,输入保证#成对出现。
输出格式:第一行输出被最多条微博提到的话题,第二行输出其被提到的微博条数。如果这样的话题不唯一,则输出按字母序最小的话题,并在第三行输出And k more ...,其中k是另外几条热门话题的条数。
输入保证至少存在一条话题。
注意:两条话题被认为是相同的,如果在去掉所有非英文字母和数字的符号、并忽略大小写区别后,它们是相同的字符串;同时它们有完全相同的分词。输出时除首字母大写外,只保留小写英文字母和数字,并用一个空格分隔原文中的单词。
输入样例:
4
This is a #test of topic#.
Another #Test of topic.#
This is a #Hot# #Hot# topic
Another #hot!# #Hot# topic
输出样例:
Hot
2
And 1 more
出处:
https://edu.csdn.net/practice/24565195
代码:
import re
a = int(input('输入微博数量(小于等于105的正整数):'))
b = []
c = []
while len(b)<a:x = input('请输入微博内容,小于140字:')if len(x)<140:b.append(x)else:print('信息超出140字限制,请从新输入。')c += re.findall('#[^#]+#',x)
d = [{'n':n,'c':len(c)-len(re.findall('#[^#]+#',re.sub(n,'',''.join(c.copy()))))} for n in set(c)]
e = sorted(d,key=lambda x:x['c'],reverse=True)
print(e[0]['n'].title())
print(e[0]['c'])
输出:
略
3. 恢复二叉搜索树
给你二叉搜索树的根节点 root
,该树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。
进阶:使用 O(n) 空间复杂度的解法很容易实现。你能想出一个只使用常数空间的解决方案吗?
示例 1:
输入:root = [1,3,null,null,2] 输出:[3,1,null,null,2] 解释:3 不能是 1 左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。
示例 2:
输入:root = [3,1,4,null,null,2] 输出:[2,1,4,null,null,3] 解释:2 不能在 3 的右子树中,因为 2 < 3 。交换 2 和 3 使二叉搜索树有效。
提示:
- 树上节点的数目在范围
[2, 1000]
内 -2^31 <= Node.val <= 2^31 - 1
出处:
https://edu.csdn.net/practice/24565196
代码:
已对原题代码中to_list()作了2处小改动,使输出结果更符合题目。
import sys
class TreeNode(object):def __init__(self, x):self.val = xself.left = Noneself.right = Nonedef to_list(self, count):queue = []queue.append(self)result = []while len(queue) > 0:if count == 0:breaknode = queue.pop(0)if node is None:result.append(None) #直接用None代替'null'else:count -= 1result.append(node.val)queue.append(node.left)queue.append(node.right)#增加去掉尾部None的处理while result[-1] is None:result.pop()return result
class List2Tree(object):def __init__(self, nums: list):self.nums = numsself.queue = []if len(nums) == 1:self.root = TreeNode(self.nums.pop(0))else:a = self.nums.pop(0)b = self.nums.pop(0)c = self.nums.pop(0)self.root = TreeNode(a)if b is not None:self.root.left = TreeNode(b)else:self.root.left = bif c is not None:self.root.right = TreeNode(c)else:self.root.right = cself.queue.append(self.root.left)self.queue.append(self.root.right)def convert(self):while len(self.nums) > 0 and len(self.queue) > 0:node = self.queue.pop(0)if node is not None:num = self.nums.pop(0)if num is not None:node.left = TreeNode(num)else:node.left = numif len(self.nums) > 0:num = self.nums.pop(0)else:num = Noneif num is not None:node.right = TreeNode(num)else:node.right = numself.queue.append(node.left)self.queue.append(node.right)return self.root
class Solution(object):def __init__(self):self.first = self.second = Noneself.pre = TreeNode(-sys.maxsize - 1)def recoverTree(self, root):length = len(root)root = List2Tree(root).convert()self.traverse(root)self.first.val, self.second.val = self.second.val, self.first.valreturn root.to_list(length)def traverse(self, root):if root is None:returnself.traverse(root.left)if self.pre.val >= root.val:if self.first is None:self.first = self.preif self.first is not None:self.second = rootself.pre = rootself.traverse(root.right)
# %%
s = Solution()
print(s.recoverTree(root=[1, 3, None, None, 2]))
s = Solution()
print(s.recoverTree(root=[3, 1, 4, None, None, 2]))
输出:
[3, 1, None, None, 2]
[2, 1, 4, None, None, 3]
🌟 每日一练刷题专栏 🌟
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
![]() | Golang每日一练 专栏 |
![]() | Python每日一练 专栏 |
![]() | C/C++每日一练 专栏 |
![]() | Java每日一练 专栏 |