
NewCoder OJ
牛客OJ
ShellDawn
Gu-Ah
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
No.184 - 牛客OJ - candy - 分糖
题目描述 有N个小朋友站在一排,每个小朋友都有一个评分 你现在要按以下的规则给孩子们分糖果: 每个小朋友至少要分得一颗糖果 分数高的小朋友要他比旁边得分低的小朋友分得的糖果多 你最少要分发多少颗糖果? 思路:正扫一次,倒扫一次 class Solution { public: /** * * @param ratings int整型vector * @return int整型 */ int candy(vector<int>&am原创 2020-05-21 23:35:56 · 279 阅读 · 0 评论 -
牛客OJ:字符串的枚举排列
class Solution { public: vector<string> Permutation(string str) { if(str.length() < 1) return *(new vector<string>); char s[10]; memset(s,0,sizeof(s)); ...原创 2019-04-01 10:28:07 · 294 阅读 · 0 评论 -
牛客OJ:两个堆完成数据流中位数的查询
#include <bits/stdc++.h> using namespace std; priority_queue<int> qmax; // 多放一个 priority_queue<int, vector<int>, greater<int> > qmin; void Insert(int num) { if(qma...原创 2019-04-01 15:29:40 · 361 阅读 · 0 评论 -
牛客OJ:正则表达式匹配
这题没写出来。。。。 class Solution { public: bool solve(char* a,char* b){ if(*a == 0 && *b == 0) return true; if(*a != 0 && *b == 0) return false; if(*(b+1) == '*'){...原创 2019-04-06 22:20:57 · 414 阅读 · 0 评论 -
牛客OJ:丑数
方法一,类似筛法素数,只能通过84%的数据。 const int maxn = 10000001; bool num[maxn]; class Solution { public: int GetUglyNumber_Solution(int index) { if(index <= 0) return 0; memset(num,0,sizeof(...原创 2019-04-06 23:42:54 · 380 阅读 · 0 评论 -
牛客OJ:判断序列是否为一棵树的后序遍历
有个规律,前序遍历,中序遍历,后序遍历都是沿着树边逆时针转一圈。 后序遍历只需要判断最后一个元素是否可以划分前面序列为两部分: 代码: class Solution { public: // 最后一个数字是否能划分数据; bool solve(vector<int> s,int l,int r){ if(l >= r-1) return true;...原创 2019-04-01 22:06:15 · 295 阅读 · 0 评论 -
牛客OJ:序列化二叉树
#include <bits/stdc++.h> using namespace std; struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(N...原创 2019-04-07 13:58:42 · 250 阅读 · 0 评论 -
牛客OJ:二叉搜索树第K个节点
#include<bits/stdc++.h> using namespace std; struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(N...原创 2019-04-07 14:27:05 · 169 阅读 · 0 评论 -
牛客OJ:判断子结构和子树
判断子结构: class Solution { public: bool solve(TreeNode* a, TreeNode* b){ if(b == NULL) return true; if(a == NULL) return false; if(a->val == b->val){ return ...原创 2019-04-07 15:02:04 · 196 阅读 · 0 评论 -
牛客OJ:二叉搜索树转双向列表
采用中序遍历: /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: void dfs(vector<...原创 2019-04-07 16:17:17 · 176 阅读 · 0 评论 -
牛客OJ:判断字符串是否为数值
这题不想敲第二遍,居然“-.123”都算数值 class Solution { public: bool isNumeric(char* string) { int loc = 0; while(string[loc] !=0){ if((string[loc]>='0'&&string[loc]<...原创 2019-04-02 00:22:11 · 245 阅读 · 0 评论 -
牛客OJ:把数组排成最小的树(greedy)
贪心,32和323的比较等同于,322和323的比较。 struct Node{ int v1; int v2; Node(int a,int b){ v1 = a,v2 =b; } }; bool cmp(Node a,Node b){ if(a.v2 < b.v2) return true; return false; }...原创 2019-04-02 10:13:58 · 210 阅读 · 0 评论 -
牛客OJ:和为S的连续正整数(prefix sum)
前缀和: class Solution { public: vector<vector<int> > FindContinuousSequence(int sum) { vector<int> s; s.push_back(0); for(int i=1;i<sum/2+2;i++){ ...原创 2019-04-02 11:39:19 · 244 阅读 · 0 评论 -
牛客OJ:逆序数对
shell排序变体 #include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int solve(vector<int>& data,int l,int r){ if(l>=r-1) return 0; int mid = (l+r+1)/2; ...原创 2019-03-28 23:38:09 · 447 阅读 · 0 评论 -
牛客OJ:矩阵中的路径(DFS模版题)
需要注意的地方,字符串结尾作为dfs跳出会有bug,当字符串长度等于矩阵所有元素时。 #include <bits/stdc++.h> using namespace std; const int dirX[] = {-1,1,0,0}; const int dirY[] = {0,0,-1,1}; class Solution { public: bool dfs...原创 2019-04-03 10:59:51 · 357 阅读 · 0 评论 -
牛客:小易喜欢的数列
动态规划入门题: #include<bits/stdc++.h> using namespace std; #define maxn 100005 #define LL long long #define MM(x) memset(x,0,sizeof(x)); #define mod 1000000007 LL N,M; LL dp[12][maxn];// dp[i][j] ...原创 2019-06-23 03:48:13 · 296 阅读 · 0 评论 -
No.28 - 牛客OJ- 2019校招真题-拼多多-选靓号
暴力+贪心: 枚举变为K次0~9,每次都最小费用,最小字典序 最小字典序,贪心思路,同费用下,先大值变小值,从前往后边,再小值变大值,从后往前边。 注:\red{注:}注: string初始化 string s(S) 相当于赋值 string初始化 string s(L,‘9’) 相当于最大序 #include<cstdio> #include<cstring> #incl...原创 2019-07-25 23:58:12 · 965 阅读 · 0 评论 -
牛客OJ:二叉树和为某值的路径
BFS: struct Node{ TreeNode* p; int sum; vector<int> v; }; class Solution { public: vector<vector<int> > FindPath(TreeNode* root,int expectNumber) { if(root =...原创 2019-04-05 22:06:50 · 214 阅读 · 0 评论 -
牛客OJ:机器人的运动范围
BFS: #include <bits/stdc++.h> using namespace std; const int dirX[] = {-1,1,0,0}; const int dirY[] = {0,0,-1,1}; bool solve(int x,int y,int T){ int sum = 0; while(x>0){ sum ...原创 2019-04-05 21:50:41 · 218 阅读 · 0 评论 -
牛客OJ:删除链表中重复的节点
两指针循环(感觉还是三指针好写一些): /* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* deleteDuplication...原创 2019-03-31 19:02:49 · 179 阅读 · 0 评论 -
牛客OJ:最大子序列和(dp)
状态独立在于,一个已知序列,加不加两边新元素在于,新元素是否小于0. #include <bits/stdc++.h> using namespace std; class Solution { public: int FindGreatestSumOfSubArray(vector<int> array) { int l = array.size...原创 2019-03-29 16:25:08 · 381 阅读 · 0 评论 -
牛客OJ:判断平衡二叉树
递归比较左右子树,维护flag,并返回深度。 #include <bits/stdc++.h> using namespace std; class Solution { public: int FindGreatestSumOfSubArray(vector<int> array) { int l = array.size(); ...原创 2019-03-29 19:31:16 · 274 阅读 · 0 评论 -
牛客OJ:统计n以内所有数中各个位上1出现的个数
数学题: 三种情况讨论即可 if(num[i] > 1) ans += (sumfront[i]+1)*base[i]; if(num[i] == 1) ans += sumfront[i]*base[i] + sumback[i] + 1; if(num[i] == 0) ans += sumfront[i]*base[i]; 代码: #include <bits/stdc++.h...原创 2019-03-29 22:58:17 · 346 阅读 · 0 评论 -
牛客OJ:可以返回当前最小值的栈
维护两个栈: #include <bits/stdc++.h> using namespace std; class Solution { public: stack<int> s; stack<int> m; void push(int value) { s.push(value); if(!m.emp...原创 2019-03-30 11:59:30 · 175 阅读 · 0 评论 -
牛客OJ:字符串左移
有坑,注意空串的特殊样例。 #include <bits/stdc++.h> using namespace std; class Solution { public: string LeftRotateString(string str, int n) { int l = str.length(); n = n%l; stri...原创 2019-03-30 12:25:05 · 215 阅读 · 0 评论 -
牛客OJ:对称二叉树
先构造一颗对称二叉树,再判断; class Solution { public: TreeNode* solve(TreeNode* b){ if(b == NULL) return NULL; TreeNode* a = new TreeNode(b->val);; a->left = solve(b->right); ...原创 2019-03-30 12:48:23 · 188 阅读 · 0 评论 -
牛客OJ:树打印多行
看着像BFS,其实是DFS。 学到了一句,二维数组插入空一维数组: v.push_back(vector<int>()); class Solution { public: vector<vector<int> > v; void dfs(TreeNode* p,int deep){ if(p==NULL) return ; ...原创 2019-03-30 13:07:48 · 148 阅读 · 0 评论 -
牛客OJ:在排序数组中查找数字
STL: class Solution { public: int GetNumberOfK(vector<int> data ,int k) { return upper_bound(data.begin(),data.end(),k) - lower_bound(data.begin(),data.end(),k); ...原创 2019-03-30 13:15:23 · 276 阅读 · 0 评论 -
牛客OJ:环每次去除第m个,最后剩下?
映射找规律: 采用递归解决: #include <bits/stdc++.h> using namespace std; int solve(int n,int m){ if(n <= 0) return -1; if(n == 1) return m%2; int t = solve(n-1,m); return (t+m)%(n+1); } ...原创 2019-03-30 15:30:53 · 229 阅读 · 0 评论 -
牛客OJ:中序遍历二叉树某节点下一个节点
先找到根,然后中序遍历: 在这里插入代码片/* struct TreeLinkNode { int val; struct TreeLinkNode *left; struct TreeLinkNode *right; struct TreeLinkNode *next; TreeLinkNode(int x) :val(x), left(NULL), ri...原创 2019-03-30 15:55:29 · 274 阅读 · 0 评论 -
牛客OJ:和为S的两个数字
返回空数组: return *(new vector<int>); class Solution { public: bool bsearch(vector<int> v,int l,int r,int target,int &key){ if(l>=r-1) if(target == v[l]){ ...原创 2019-03-31 11:30:07 · 287 阅读 · 0 评论 -
牛客OJ:判断两个序列是否为栈压入序列和弹出序列
class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { int l = pushV.size(); int loc1,loc2; loc1=loc2 = 0; stack<int> s; ...原创 2019-03-31 11:39:53 · 220 阅读 · 0 评论 -
牛客OJ:滑动窗口最大值(segment tree)
线段树: #include <bits/stdc++.h> using namespace std; const int maxn = 1000000; int segtree[maxn]; void segadd(int now,int l,int r,int loc,int key){ if(l>=r-1){ segtree[now] = max...原创 2019-04-04 17:18:22 · 404 阅读 · 0 评论 -
牛客OJ:复杂链表的复制
哈希: /* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { } }; */ class Solution { public...原创 2019-04-04 19:03:12 · 156 阅读 · 0 评论 -
牛客OJ:把正方形矩阵按顺时针输出
#include <bits/stdc++.h> using namespace std; vector<int> printMatrix(vector<vector<int> > matrix) { if(matrix.size()<=0) return *(new vector<int>); // right...原创 2019-03-31 17:13:44 · 314 阅读 · 0 评论 -
牛客OJ:把矩形按顺时针输出
#include <bits/stdc++.h> using namespace std; class Solution { public: vector<int> printMatrix(vector<vector<int> > matrix) { if(matrix.size()<=0) return *(new vec...原创 2019-03-31 17:58:06 · 214 阅读 · 0 评论 -
牛客OJ:不用加减乘除法做加法
程序中,除了四则运算,还有位运算,模运算,取内存运算。 代码: class Solution { public: int Add(int num1, int num2) { int ans = num1; while(num2!=0){ ans = num1^num2; //相加 num2 =...原创 2019-03-29 14:58:29 · 186 阅读 · 0 评论