
刷题笔记
文章平均质量分 57
PAT真题 / LeetCode 练习记录
zyq-lucky
好好学习,天天向上
展开
-
【LeetCode】meituan-002. 小美的仓库整理
原题链接小美是美团仓库的管理员,她会根据单据的要求按顺序取出仓库中的货物,每取出一件货物后会把剩余货物重新堆放,使得自己方便查找。已知货物入库的时候是按顺序堆放在一起的。如果小美取出其中一件货物,则会把货物所在的一堆物品以取出的货物为界分成两堆,这样可以保证货物局部的顺序不变。已知货物最初是按 1~n 的顺序堆放的,每件货物的重量为 w[i] ,小美会根据单据依次不放回的取出货物。请问根据上述操作,小美每取出一件货物之后,重量和最大的一堆货物重量是多少?格式:输入:- 输入第一行包含一个正整数 n原创 2022-03-19 01:11:26 · 650 阅读 · 0 评论 -
【LeetCode】5. 最长回文子串
5. 最长回文子串原题链接戳这⬅️给你一个字符串 s,找到 s 中最长的回文子串。示例 1:输入:s = “babad”输出:“bab”解释:“aba” 同样是符合题意的答案。示例 1:输入:s = “cbbd”输出:“bb”解题思路解法一:动态规划dp[i][j]dp[i][j]dp[i][j] 表示字符串 s[i:j+1]s[i:j + 1]s[i:j+1] 是否为回文字符串,其状态转移公式为:若 s[i]==s[j]s[i] == s[j]s[i]==s[j],dp[i原创 2022-02-25 17:16:16 · 383 阅读 · 0 评论 -
【LeetCode】 剑指 Offer 51. 数组中的逆序对
原题链接戳这⬅️在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。示例 1:输入: [7,5,6,4]输出: 5解题思路利用归并排序的思想合并左右两个有数子数组时,每当右侧子数组的数字并入,则逆序对数量 += 左侧数组剩余的数字个数,此时并入的数字小于左侧数组剩余的所有数字,均可构成逆序对class Solution: def reversePairs(self, nums: List[int]) -&原创 2022-02-22 15:13:14 · 368 阅读 · 0 评论 -
【leetCode】剑指 Offer 44. 数字序列中某一位的数字
剑指 Offer 44. 数字序列中某一位的数字原题链接戳这⬅️数字以0123456789101112131415…的格式序列化到一个字符序列中。在这个序列中,第5位(从下标0开始计数)是5,第13位是1,第19位是4,等等。请写一个函数,求任意第n位对应的数字。解题思路 1假设序列化的时候,每个数字根据最大位数自动以 000 补全,则所有的数字位数相同(假设均为 iii ),此时下标为 nnn 的数字为 n//in // in//i 的第 n%in \% in%i 位数字。若数字的最大位数为原创 2022-02-18 17:14:40 · 223 阅读 · 0 评论 -
【LeetCode】剑指 Offer 43. 1~n 整数中 1 出现的次数
原题链接戳这⬅️输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数。例如,输入12,1~12这些整数中包含1 的数字有1、10、11和12,1一共出现了5次。示例1:输入:n = 12输出:5示例2:输入:n = 13输出:6解题思路首先,将问题转化为求解 1~n1~n1~n 这 nnn 个整数中,第 iii 位上出现 111 的次数,即当第 iii 位取值为 111 时,求解存在多少个 <n<n<n 的不同整数。则,依次对个位、十位、百位… 求原创 2022-02-18 15:17:51 · 266 阅读 · 0 评论 -
【LeetCode】688. 骑士在棋盘上的概率
原题链接在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始,并尝试进行 k 次移动。行和列是 从 0 开始 的,所以左上单元格是 (0,0) ,右下单元格是 (n - 1, n - 1) 。象棋骑士有8种可能的走法,如下图所示。每次移动在基本方向上是两个单元格,然后在正交方向上是一个单元格。每次骑士要移动时,它都会随机从8种可能的移动中选择一种(即使棋子会离开棋盘),然后移动到那里。骑士继续移动,直到它走了 k 步或离开了棋盘。返回 骑士在棋盘停止移动后仍留原创 2022-02-17 14:37:05 · 199 阅读 · 0 评论 -
【LeetCode】刷题总结
广度优先遍历2045. 到达目的地的第二短时间投票法剑指 Offer 39. 数组中出现次数超过一半的数字数学题1447. 最简分数原创 2022-01-24 13:00:16 · 197 阅读 · 0 评论 -
【LeetCode】1447. 最简分数
原题地址戳这 ⬅️给你一个整数 n ,请你返回所有 0 到 1 之间(不包括 0 和 1)满足分母小于等于 n 的 最简 分数 。分数可以以 任意 顺序返回。示例1:输入:n = 2输出:[“1/2”]解释:“1/2” 是唯一一个分母小于等于 2 的最简分数。示例2:输入:n = 4输出:[“1/2”,“1/3”,“1/4”,“2/3”,“3/4”]解释:“2/4” 不是最简分数,因为它可以化简为 “1/2” 。思路本题主要是如何判断是否位最简分数,即判断两个数字的最大公约数原创 2022-02-10 15:38:53 · 391 阅读 · 0 评论 -
【LeetCode】 2045. 到达目的地的第二短时间
城市用一个 双向连通 图表示,图中有 n 个节点,从 1 到 n 编号(包含 1 和 n)。图中的边用一个二维整数数组 edges 表示,其中每个 edges[i] = [ui, vi] 表示一条节点 ui 和节点 vi 之间的双向连通边。每组节点对由 最多一条 边连通,顶点不存在连接到自身的边。穿过任意一条边的时间是 time 分钟。每个节点都有一个交通信号灯,每 change 分钟改变一次,从绿色变成红色,再由红色变成绿色,循环往复。所有信号灯都 同时 改变。你可以在 任何时候 进入某个节点,但是 只原创 2022-01-24 12:53:56 · 364 阅读 · 0 评论 -
PAT真题练习(甲级)1068 Find More Coins (30 分)
PAT真题练习(甲级)1068 Find More Coins (30 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976思路用DFS做的最后一个测试点刚开始超时了,做了特例判断(若所有数加起来小于M,直接跳出)AC代码#include <stdio.h>...原创 2019-09-07 23:23:04 · 335 阅读 · 0 评论 -
PAT真题练习(甲级)1026 Table Tennis (30 分)(含详细注释)
PAT真题练习(甲级)1026 Table Tennis (30 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805472333250560A table tennis club has N tables available to the public. The tables are number...原创 2019-09-07 15:58:56 · 592 阅读 · 0 评论 -
PAT真题练习(甲级)1110 Complete Binary Tree (25 分)
PAT真题练习(甲级)1110 Complete Binary Tree (25 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805489282433024Given a tree, you are supposed to tell if it is a complete binary tree....原创 2019-09-06 15:00:37 · 275 阅读 · 0 评论 -
PAT真题练习(甲级)1018 Public Bike Management (30 分)
PAT真题练习(甲级)1018 Public Bike Management (30 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805489282433024There is a public bike service in Hangzhou City which provides great ...原创 2019-09-06 11:16:26 · 328 阅读 · 0 评论 -
PAT真题练习(甲级)1107 Social Clusters (30 分)
PAT真题练习(甲级)1107 Social Clusters (30 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805361586847744When register on a social network, you are always asked to specify your hobb...原创 2019-09-05 17:05:23 · 281 阅读 · 0 评论 -
PAT真题练习(甲级)1057 Stack (30 分) (使用multiset和stack实现)
PAT真题练习(甲级)1057 Stack (30 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592Stack is one of the most fundamental data structures, which is based on the principle ...原创 2019-09-05 12:18:47 · 536 阅读 · 0 评论 -
PAT真题练习(甲级)1049 Counting Ones (30 分)
PAT真题练习(甲级)1049 Counting Ones (30 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456The task is simple: given any positive integer N, you are supposed to count th...原创 2019-09-05 09:34:56 · 259 阅读 · 0 评论 -
PAT真题练习(甲级)1146 Topological Order (25 分)
PAT真题练习(甲级)1146 Topological Order (25 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760This is a problem given in the Graduate Entrance Exam in 2018: Which of th...原创 2019-09-03 16:18:58 · 307 阅读 · 0 评论 -
PAT真题练习(甲级)1145 Hashing - Average Search Time (25 分)
PAT真题练习(甲级)1145 Hashing - Average Search Time (25 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744The task of this problem is simple: insert a sequence of disti...原创 2019-09-03 15:43:22 · 235 阅读 · 0 评论 -
PAT真题练习(甲级)1013 Battle Over Cities (25 分)
PAT真题练习(甲级)1013 Battle Over Cities (25 分)It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are ...原创 2019-08-27 10:37:27 · 201 阅读 · 0 评论 -
PAT真题练习(甲级)1010 Radix (25 分)
PAT真题练习(甲级)1010 Radix (25 分)Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number.Now f...原创 2019-08-26 17:15:10 · 270 阅读 · 0 评论 -
PAT真题练习(甲级)1006 Sign In and Sign Out (25 分)
PAT真题练习(甲级)1006 Sign In and Sign Out (25 分)At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Give...原创 2019-08-26 13:56:52 · 297 阅读 · 0 评论 -
PAT真题练习(甲级)1003 Emergency (25 分)
PAT真题练习(甲级)1003 Emergency (25 分)As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of re...原创 2019-08-26 10:44:21 · 258 阅读 · 0 评论 -
PAT真题练习(甲级)1004 Counting Leaves
PAT真题练习(甲级)1003 Emergency (25 分)As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of re...原创 2019-08-23 20:23:06 · 188 阅读 · 0 评论 -
PAT真题练习(甲级)1015 Reversible Primes (20 分)
PAT真题练习(甲级)1015 Reversible Primes (20 分)A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible ...原创 2019-08-27 15:29:12 · 251 阅读 · 0 评论 -
PAT真题练习(甲级)1016 Phone Bills (25 分)
PAT真题练习(甲级)1016 Phone Bills (25 分)A long-distance telephone company charges its customers by the following rules:Making a long-distance call costs a certain amount per minute, depending on the time ...原创 2019-08-27 18:03:23 · 420 阅读 · 0 评论 -
PAT真题练习(甲级)1136 A Delayed Palindrome (20 分)
PAT真题练习(甲级)1136 A Delayed Palindrome (20 分)Input Specification:Each input file contains one test case which gives a positive integer no more than 1000 digits.Output Specification:For each test ca...原创 2019-08-28 10:11:43 · 237 阅读 · 0 评论 -
PAT真题练习(甲级)1027 Colors in Mars (20 分)
PAT真题练习(甲级)1027 Colors in Mars (20 分)People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first ...原创 2019-08-28 10:43:12 · 198 阅读 · 0 评论 -
PAT真题练习(甲级)1064 Complete Binary Search Tree (30 分)
PAT真题练习(甲级)1064 Complete Binary Search Tree (30 分)A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes...原创 2019-08-28 12:19:19 · 210 阅读 · 0 评论 -
PAT真题练习(甲级)1127 ZigZagging on a Tree (30 分)
PAT真题练习(甲级)1127 ZigZagging on a Tree (30 分)Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder t...原创 2019-08-28 17:47:44 · 338 阅读 · 0 评论 -
## PAT真题练习(甲级)1066 Root of AVL Tree (25 分)(平衡二叉树)
PAT真题练习(甲级)1066 Root of AVL Tree (25 分)原题网址: https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888An AVL tree is a self-balancing binary search tree. In an AVL tree, the he...原创 2019-08-30 15:27:07 · 219 阅读 · 1 评论 -
PAT真题练习(甲级)1021 Deepest Root (25 分)
PAT真题练习(甲级)1021 Deepest Root (25 分)原题网址: https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856A graph which is connected and acyclic can be considered a tree. The height of...原创 2019-09-01 15:00:59 · 316 阅读 · 1 评论 -
PAT真题练习(甲级)1033 To Fill or Not to Fill (25 分)
PAT真题练习(甲级)1033 To Fill or Not to Fill (25 分)原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805458722734080A graph which is connected and acyclic can be considered a tree. The ...原创 2019-09-02 12:28:51 · 323 阅读 · 0 评论 -
PAT真题练习(甲级)1014 Waiting in Line (30 分)
PAT真题练习(甲级)1014 Waiting in Line (30 分)Sample Input:2 2 7 51 2 6 4 3 534 23 4 5 6 7Sample Output:08:0708:0608:1017:00Sorry#include<iostream>#include <string>#include <...原创 2019-08-27 14:40:43 · 232 阅读 · 0 评论