随笔分类 -  刷题

1 2 3 4 5 ··· 8 下一页

最小的K个数
摘要:题目描述:输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。 思路:堆排序中每趟可以输出最小元素,所以,借助堆排序可以得到最小的K个数。 步骤: 1 创建无元素的ArrayList对象。 2 如果数组为空或者k不是正整数,则返回指 阅读全文

posted @ 2025-11-29 15:12 王景迁 阅读(3) 评论(0) 推荐(0)

跳台阶
摘要:题目描述:一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。 思路:类似于斐波那契序列,跳上第n(n>3)级台阶,之前最后一步跳1级或2级。 步骤: 1 如果台阶级数n<=2,则返回n。 2 根据f(n)=f(n-1)+f(n-2),计算结果并返回。 Java 阅读全文

posted @ 2025-11-29 15:11 王景迁 阅读(4) 评论(0) 推荐(0)

替换空格
摘要:C++代码: class Solution { public: void replaceSpace(char *str,int length) { if (str == NULL || length <= 0) { return; } int oriLength = 1, blankCount = 阅读全文

posted @ 2025-11-29 15:09 王景迁 阅读(3) 评论(0) 推荐(0)

两个链表的第一个公共结点
摘要:题目描述:输入两个链表,找出它们的第一个公共结点。 思路:如果两个链表有公共结点,则第一个公共结点以及之后的所有结点都是重合的,即至少它们的尾结点是重合的。因为两个链表长度不一定相等,所以同步遍历时不能保证两个链表同时到达尾结点。假设一个链表比另一个多k个结点,先在长的链表上遍历k个结点即尾部对齐, 阅读全文

posted @ 2025-11-29 15:09 王景迁 阅读(3) 评论(0) 推荐(0)

链表中倒数第k个结点
摘要:题目描述:输入一个链表,输出该链表中倒数第k个结点。 思路:指向首结点的第1个指针走k-1步,如果后继结点为空,则返回null,第1个指针指向第k个结点。指向首结点的第2个指针与第1个指针同步遍历,当第1个指针指向最后一个结点时,第2个指针走了n-k步,指向了倒数第k个结点(n-(n-k+1)=k- 阅读全文

posted @ 2025-11-29 15:08 王景迁 阅读(3) 评论(0) 推荐(0)

连续子数组的最大和
摘要:题目描述:例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。 Java代码: public class Solution { public int FindGreatestSumOfSubArray(int[] array) { if (arra 阅读全文

posted @ 2025-11-29 15:07 王景迁 阅读(4) 评论(0) 推荐(0)

和为S的两个数字
摘要:题目描述:输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。对应每个测试案例,输出两个数,小的先输出。 思路:数列满足递增条件,设两个头尾指针i和j,若ai + aj == sum,则是答案(相差越远乘积越小)。若ai + 阅读全文

posted @ 2025-11-29 15:06 王景迁 阅读(3) 评论(0) 推荐(0)

合并两个排序的链表
摘要:题目描述:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。 思路:类似于2路归并排序的合并操作,先取两个链表首结点值小者作为新链表的首结点,然后遍历两个链表,取小者作为其后继结点,直到至少有一个链表为空,最后把至多一个非空链表首结点作为其后继结点。 步骤: 阅读全文

posted @ 2025-11-29 15:05 王景迁 阅读(1) 评论(0) 推荐(0)

反转链表
摘要:题目描述:输入一个链表,反转链表后,输出链表的所有元素。 思路:遍历链表时通过头插法来插入链表结点。 步骤: 1 如果链表为空,返回null。 2 当前首结点设为尾结点。 3 遍历链表,通过头插法把当前链表结点插入到当前首结点的前面。 4 返回反转链表后的首结点。 时间复杂度:O(n)。 Java代 阅读全文

posted @ 2025-11-29 15:04 王景迁 阅读(6) 评论(0) 推荐(0)

二叉树的深度
摘要:题目描述:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。 思路:通过广度优先遍历(BFS)来获取二叉树的深度。 步骤: 1 如果根结点为空,则返回0。 2 创建实现了Queue接口的LinkedList对象。 3 通过队列来执 阅读全文

posted @ 2025-11-29 15:03 王景迁 阅读(5) 评论(0) 推荐(0)

二叉树的镜像
摘要:题目描述:操作给定的二叉树,将其变换为源二叉树的镜像。 思路:通过BFS来遍历每个结点,交换当前结点的左右孩子结点即交换左右子树。 步骤: 1 如果根结点为空或者只有一个结点,则结束。 2 创建实现了Queue接口的LinkedList对象。 3 根结点入队列。 4 通过BFS来遍历每个结点,交换当 阅读全文

posted @ 2025-11-29 15:03 王景迁 阅读(5) 评论(0) 推荐(0)

5-18 Hashing - Hard Version (30分)
摘要:#include <iostream> #include <vector> #include <queue> using namespace std; struct node { int key, index; bool operator < (const node &nod) const { re 阅读全文

posted @ 2025-11-29 15:00 王景迁 阅读(3) 评论(0) 推荐(0)

5-14 电话聊天狂人 (25分)
摘要:#include <iostream> #include <map> using namespace std; int main() { int n; scanf("%d", &n); int i; long long num; map<long long, int> m; for(i = 1; i 阅读全文

posted @ 2025-11-29 15:00 王景迁 阅读(2) 评论(0) 推荐(0)

5-11 关键活动 (30分)
摘要:#include <iostream> #include <vector> #include <queue> using namespace std; struct node { int next, time; }; int degree[2][110], t[2][110], maxtime; v 阅读全文

posted @ 2025-11-29 14:59 王景迁 阅读(2) 评论(0) 推荐(0)

5-10 公路村村通 (30分)
摘要:#include <iostream> #include <algorithm> using namespace std; struct edge { int a, b, cost; }e[3010]; int sum[1010], tree[1010], res; int cmp(edge e1, 阅读全文

posted @ 2025-11-29 14:59 王景迁 阅读(1) 评论(0) 推荐(0)

5-10 Saving James Bond - Easy Version (25分)
摘要:#include <iostream> #include <math.h> using namespace std; struct node { int x, y, flag; }vis[110]; int flag, d, n; double getdis(int index0, int inde 阅读全文

posted @ 2025-11-29 14:59 王景迁 阅读(2) 评论(0) 推荐(0)

5-9 Huffman Codes (30分)
摘要:#include <iostream> #include <queue> #include <vector> #include <string.h> using namespace std; int judge(char a[], char b[], int alen, int blen) { in 阅读全文

posted @ 2025-11-29 14:58 王景迁 阅读(3) 评论(0) 推荐(0)

5-8 哈利·波特的考试 (25分)
摘要:#include <iostream> #include <vector> #include <string.h> using namespace std; struct node { int next, dis; }; int tree[110], sum[110], flag[110]; int 阅读全文

posted @ 2025-11-29 14:58 王景迁 阅读(4) 评论(0) 推荐(0)

5-8 File Transfer (25分)
摘要:#include <iostream> using namespace std; int tree[10010], sum[10010]; void init(int n) { int i; for(i = 1; i <= n; i++) { tree[i] = -1; sum[i] = 1; } 阅读全文

posted @ 2025-11-29 14:57 王景迁 阅读(3) 评论(0) 推荐(0)

5-7 六度空间 (30分)
摘要:#include <iostream> #include <vector> #include <queue> #include <string.h> using namespace std; vector<int> v[10010]; queue<int> q; int vis[10010], le 阅读全文

posted @ 2025-11-29 14:57 王景迁 阅读(2) 评论(0) 推荐(0)

1 2 3 4 5 ··· 8 下一页

导航