03 2021 档案
摘要:题目 分析 本题可以直接模拟,可以递归 代码 递归 1 class Solution { 2 public: 3 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 4 if(l1 == NULL) return l2; 5 if(l2 ==
阅读全文
摘要:题目 分析 利用哑结点方便操作。 head->n1->n2->n3->n4 交换步骤 1. n1指向n3 2. n2指向n1 3. head->next 指向n2 4. head指向n1(n3的前一个结点) 代码 1 class Solution { 2 public: 3 ListNode* sw
阅读全文
摘要:题目 分析 模拟题目 先建立哑结点,采用头插法。 代码 1 class Solution { 2 public: 3 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 4 ListNode *head = new ListNode(); 5
阅读全文
摘要:题目 代码 法一、中序遍历结果保存下来,查看是否是递增序列 1 class Solution { 2 public: 3 vector<int>res; 4 void dfs(TreeNode* root){ 5 if(root == NULL) return; 6 dfs(root->left);
阅读全文
摘要:题目 代码 1 class Solution { 2 public: 3 TreeNode* constructMaximumBinaryTree(vector<int>& nums) { 4 //数组长度是一直接返回 5 if(nums.size() == 1) {TreeNode *root =
阅读全文
摘要:题目 代码 1 class Solution { 2 public: 3 TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { 4 if(preorder.size() == 0) return NULL; 5 //找根
阅读全文
摘要:题目 代码 1 class Solution { 2 public: 3 4 TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { 5 //第一步 6 if(postorder.size() == 0) return
阅读全文
摘要:题目 代码 层次遍历(一) 1 class Solution { 2 public: 3 int getDepth(TreeNode* root){ 4 if(root == NULL) return 0; 5 return max(getDepth(root->left),getDepth(roo
阅读全文
摘要:题目 代码 线性dp 1 class Solution { 2 public: 3 int findLengthOfLCIS(vector<int>& nums) { 4 if(nums.size() <= 1) return nums.size(); 5 vector<int>dp(nums.si
阅读全文
摘要:题目 代码 1 class Solution { 2 public: 3 int lengthOfLIS(vector<int>& nums) { 4 if(nums.size() <= 1) return nums.size(); 5 vector<int>dp(nums.size()+1,1);
阅读全文
摘要:题目 代码 1 class Solution { 2 public: 3 int dfs(TreeNode* root){ 4 if(root == NULL) return 0; 5 int left = dfs(root->left); //左 6 int right = dfs(root->r
阅读全文
摘要:题目 分析 开一个栈来记录左括号位置,而不是记录左括号。扫描原字符串,遇到右括号,查看栈是否空,若不空则匹配,若空则不匹配。扫描结束后,查看栈是否为空,若不空,则栈中存放的是未匹配的的左括号。 代码 1 #include<iostream> 2 #include<string> 3 #include
阅读全文
摘要:题目 通过本题学习KMP 代码 1 #include<iostream> 2 using namespace std; 3 4 const int MAXM = 10000; 5 const int MAXN = 1000000; 6 7 int nextTable[MAXM]; 8 int pat
阅读全文
摘要:题目 2003年清华大学计算机考研上机 本题不可直接O(n)来搜索,数据量大,千万级别,1s过不去,所以采用二分查找。 代码 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5
阅读全文
摘要:题目 代码 网上别人 1 #include<iostream> 2 #include<vector> 3 4 using namespace std; 5 6 int main(){ 7 int n, m; 8 while(cin >> n >> m){ 9 // students[i] 表示第 i
阅读全文
摘要:题目 本题不能用快排,会超时,因为数据量大。若用快排将会是千万的复杂度,1s内过不去。本题采用哈希,将NlogN算法降低为N 代码 1 #include<iostream> 2 using namespace std; 3 #define OFFSET 500000 4 int h[1000001]
阅读全文
摘要:题目 2006年浙大机试题 代码 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 int main(){ 6 int n; 7 while(scanf("%d",&n) != EOF){ 8 int a[10
阅读全文
摘要:题目 代码 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 int ISYEAP(int x){ 6 if((x%100!=0 && x % 4 == 0) || (x % 400 == 0) )return
阅读全文
摘要:题目 代码 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 int ISYEAP(int x){ 6 if((x%100!=0 && x % 4 == 0) || (x % 400 == 0) )return
阅读全文
摘要:题目 2008年上海交大计算机上机题 代码 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 6 int ISYEAP(int x){ 7 if((x%100!=0 && x
阅读全文
摘要:题目 日期类的题目基本都要找一个起点,提前预处理 代码 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 //判断是否是闰年或平年 5 int ISYEAP(int x){ 6 if((x %100 != 0 &&
阅读全文
摘要:题目 代码 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int main(){ 6 char str[201]; 7 cin>>str; 8 int n = strle
阅读全文
摘要:题目 代码 1 #include<iostream> 2 #include<algorithm> 3 #include<string.h> 4 using namespace std; 5 typedef struct stu{ 6 char cno[10]; 7 char name[10]; 8
阅读全文
摘要:题目 输入一系列整数,将其中最大的数挑出(如果有多个,则挑出一个即可),并将剩下的数进行排序,如果无剩余的数,则输出-1。 代码 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 int main(){ 5 int
阅读全文
摘要:题目 成绩排序——2000年清华大学计算机机试真题 代码 1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 using namespace std; 5 typedef struct stu{ 6 char name[10
阅读全文
摘要:冒泡排序 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 int n; 5 int main(){ 6 int a[100]; 7 while(scanf("%d",&n) != EOF){ 8 for(int
阅读全文