- 博客(43)
- 收藏
- 关注
原创 HDU-4027 线段树
一般的线段树模版题改了下, 不需要开lazy数组,而且执行到一定步数后可以剪枝,因为开方是个很快的步骤,后面就一直是1了,这题基本就需要改下update函数就行了#include<cstring>#include<iostream>#include<cstdio>#include<string>#include<vector>...
2019-04-01 15:08:00
188
原创 PAT 1067 Sort with Swap(0, i)
#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <stdio.h>#include <cstring>#include <stack>#include <queue&a
2019-02-19 20:36:45
169
原创 PAT 1109 Group Photo 集体照
这题可以用双向队列,很容易就解决!具体看代码#include <iostream>#include <vector>#include <algorithm>#include <cstdio>#include <string>#include <queue>#include <unordered_se...
2019-01-07 22:14:10
210
原创 PAT 1090 危险品装箱
这个和甲级的一道题重复了。今天真的不适合写代码吧,一个下午就ac了3道题,头很大。这道题有点像leetcode朋友圈,应该可以用并查集做,我用了map的方法,具体看代码吧。#include <iostream>#include <vector>#include <algorithm>#include <cstdio>#inclu...
2019-01-07 21:07:48
402
原创 Leetcoe 863. All Nodes Distance K in Binary Tree
dfs和bfs版本,不过我是先转化成图的,以为效率会低很多,没想到还打败了90多的人/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(...
2019-01-06 18:46:35
167
原创 PAT 1144 The Missing Numbe
我是用哈希表做的。有几个细节要注意下的,a的范围。#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <stdio.h>#include <cstring>#include <stack&g
2019-01-03 23:20:56
228
原创 PAT 快速排序
记录每个位置之前和之后的最大最小值。#include <cstdio>#include <algorithm>#include <vector>using namespace std;vector<int> v;int main(){ int n; scanf("%d",&n); for (int ...
2019-01-03 15:21:51
306
原创 PAT 1127ZigZagging on a Tree
leetcode上有一题和这个基本一样,感觉这题有点水。#include <cstdio>#include <iostream>#include <cmath>#include <algorithm>#include <vector>#include <queue>using namespace std;...
2019-01-02 22:50:46
242
原创 PTA Root of AVL Tree
就是基础的avl操作#include <cstdio>#include <iostream>#include <cmath>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef struct n...
2019-01-02 22:49:08
231
原创 leetcode 有序链表转换二叉搜索树
一个思路是先转成数组,还有就是用快慢指针确定链表的中点,还是二分的思想,递归,代码写得很烂好吧/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} ...
2018-12-27 15:05:59
190
原创 Student List for Course
调了好久,最后一个点一直超时,借鉴了下别人的。#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <algorithm>#include <queue>#include <set>#incl
2018-12-22 19:32:41
191
原创 Build A Binary Search Tree
静态数组,不多说了#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <algorithm>#include <queue>using namespace std;const int MAXN = 1...
2018-12-22 16:37:15
179
原创 Complete Binary Search Tree
#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <algorithm>using namespace std;const int MAXN = 10010;vector<int> v;int ar...
2018-12-22 16:14:10
173
原创 PAT Tree Traversals Again
用了两种方法,一个是复原了二叉树,一个是直接根据前中序求后序。#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <stdio.h>#include <cstring>#include <s...
2018-12-21 14:36:50
201
原创 PAT Invert a Binary Tree
我是先复原了二叉树,再对其进行遍历。这里有个技巧,%*c可以读取换行符,然后存储到一个结构数组里,方便后面构造二叉树,这里读取的时候要左右颠倒,因为题目就是要翻转二叉树,干脆读取的时候就翻转了。后面就很基础了。#include <cstdio>#include <iostream>#include <vector>#include <...
2018-12-21 13:52:04
176
原创 PAT 输出PATest
用了哈希表的思想,但最后一个测试点一直没过,为什么啊啊啊啊啊#include <cstdio>#include <stdio.h>#include <iostream>#include <algorithm>#include <cstring>#include <queue>using namespace s...
2018-12-19 17:36:23
345
原创 PAT 1029 旧键盘
#include <stdio.h>int hash_table[130] = {0};int main(){// printf("hello\n"); char s1[90]; char s2[90]; gets(s1); gets(s2); for (int i=0; i<strlen(s2); i++) { ...
2018-12-18 20:34:19
160
原创 PAT Boys vs Girls c++
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stdlib.h>#include <vector>using namespace std;struct student{ char...
2018-12-18 09:56:52
203
原创 PAT 1011 World Cup Betting
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stdlib.h>#include <vector>using namespace std;double first[3];double ...
2018-12-18 09:41:30
188
原创 leetcode Sort List
没想到什么好办法,要将O(ologn)时间里完成,我先遍历一遍将链表存储到数组里,再对数组进行排序,排序方法写了快排和归并的,过是能过,但是时间要140ms,最快的就是直接对链表进行归并排序,第一次写比较难受,用快慢指针来分成两个链表,具体思想还是和数组的归并排序一样。/** * Definition for singly-linked list. * struct ListNode {...
2018-12-17 15:40:19
185
1
原创 leetcode Remove Duplicates from Sorted List II
我的做法比较差吧,用了4个指针,分了好几个情况,还是太菜了。具体做法就是先存储当前结点的值和后面的进行比较,直到next到不同的值,再对current的val值更新成next的val值,有几个特殊的点想了比较久,就变成下面判断比较复杂的情况。。。/** * Definition for singly-linked list. * struct ListNode { * int ...
2018-12-17 15:12:09
133
原创 leetcode 存在重复
bool containsDuplicate(int* nums, int numsSize) { if(numsSize == 0 || numsSize == 1) return false; int temp[numsSize]; temp[0] = nums[0]; int count = 1; for(int i=1;i<n...
2018-12-15 14:23:45
267
原创 leetcode 旋转数组
题目说到要用3种以上方法,又有一个要求是O(1)空间复杂度,有一种要用原地算法了,稍微复杂一点这里第一个是构造辅助数组void rotate(int* nums, int numsSize, int k) { k = k % numsSize; if(!k) return; int temp[numsSize]; int index = 0...
2018-12-15 13:51:26
182
原创 leetcode 从排序数组中删除重复项
原地算法int removeDuplicates(int* nums, int numsSize) { // if(numsSize == 1) // return 1; if(numsSize == 0) return 0; int index = 0; for(int i=1;i<numsSize;i++){ ...
2018-12-15 13:23:58
131
原创 leetcode Minimum Distance Between BST Nodes
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */void help(struct TreeNode* root,int *pre,int *re...
2018-12-14 23:57:46
187
原创 leetcode Validate Binary Search Tree 验证二叉搜索树
自己第一次想用递归写,但这里很容易错误,因为每次都只是判断当前子树和父结点值大小关系,容易忽略根结点值的大小,就换了c++用中序遍历每次把值放进vector 但这个解法有点垃圾,就去论坛看了别人的代码,学到了很多,第一个代码O(n)的复杂度,left其实就是最小值,right是最大值。/** * Definition for a binary tree node. * struct Tr...
2018-12-14 23:14:10
176
原创 PAT The Black Hole of Numbers
#include <iostream>#include <cstdio>#include <string>#include <algorithm>using namespace std;bool cmp(int a,int b){ return a>b;}void to_array(int n,int num[]){...
2018-12-14 17:36:39
158
原创 1042 Shuffling Machine
#include <cstdio>#include <iostream>#include <cstring>int cards[55];void init(){ for (int i=1; i<55; i++) cards[i] = i;}char shape[] = {'S','H','C','D','J'};...
2018-12-14 15:49:42
146
原创 leetcode nvert Binary Tree 翻转二叉树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */struct TreeNode* invertTree(struct TreeNode* root...
2018-12-13 18:49:51
163
原创 leetcode Maximum Depth of N-ary Tree
/*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) { val = _val; childr...
2018-12-13 16:23:32
151
原创 leetcode 104. Maximum Depth of Binary Tree 二叉树的深度
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int maxDepth(struct TreeNode* root) { if(!roo...
2018-12-13 16:12:47
170
原创 leetcode 100. Same Tree 相同的树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */bool isSameTree(struct TreeNode* p, struct TreeNo...
2018-12-13 16:03:56
157
原创 leetcode Symmetric Tree 对称二叉树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */bool judge(struct TreeNode *left,struct TreeN...
2018-12-13 15:51:17
149
原创 leetcode 旋转链表
具体思路就是先遍历链表求出长度,然后对k取余。通过3个指针实现旋转链表/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* rotateRight(struct ListNod...
2018-12-13 15:35:24
183
原创 leetcode Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* a...
2018-12-13 14:56:58
122
原创 1012 数字分类
data=list(map(int,input().split()))a1=0a2=0a2_count=0a3=0a4=0a4_count=0a5=0out=''for each in data[1:]: if each % 5==0 and each % 2==0: a1+=each if each % 5==1: a2_count+=1 a2+=each*(...
2018-12-12 21:57:34
180
原创 1008 数组元素循环右移问题
a=input().split()data=input().split()for i in range(int(a[1])): data=[data[-1]]+data[:-1]for i in range(len(data)-1): print(data[i],end=' ')print(data[-1])#include <iostream>#include ...
2018-12-12 21:35:28
200
原创 PAT乙级1046 划拳
#include <iostream>#include <cstdio>int main(){ int n; scanf("%d",&n); int a,b,c,d; int x = 0; int y = 0; while (n--) { scanf("%d%d%d%d",&a,&...
2018-12-12 17:47:13
183
原创 PAT 乙级1026 程序运行时间
#include <iostream>#include <cstdio>int main(){ int start,end; scanf("%d%d",&start,&end); int t = end-start; int hours = t / 360000; t = t-(t / 360000)*3600...
2018-12-12 17:42:19
226
原创 PAT 乙级1016 部分A+B
data=input().split()n1=data[0].count(data[1])n2=data[2].count(data[3])s1=n1*data[1]s2=n2*data[3]if s1=='': s1=0if s2=='': s2=0s=int(s1)+int(s2)print(s)python3和c的#include <iostream&g...
2018-12-12 17:26:11
180
1
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人