- 博客(55)
- 资源 (1)
- 收藏
- 关注
原创 学习go语言
准备开始学习go语言,另外希望能坚持把博客写下来。 参考书:The Go Programming Language 编译器: Goland
2017-11-27 21:17:20
240
原创 Leetcode Freedom Trail
class Solution { public: int findRotateSteps(string ring, string key) { int lenring = ring.size(); int lenkey = key.size(); vector> dp(200, vector(200, 1000000)); for (int i = 0; i < lenkey;
2017-03-05 16:29:52
1161
原创 剑指offer 数组中的逆数对
链接:https://www.nowcoder.com/profile/8740530/codeBookDetail?submissionId=9156674 来源:牛客网 class Solution { public: int InversePairs(vector& data) { tem = data; int high = data.size()
2017-02-27 21:34:31
384
原创 Leetcode 529. Minesweeper
class Solution { public: vector> updateBoard(vector>& board, vector& click) { int x = click[0]; int y = click[1]; int high = board.size(); int len = board[0].size(); vector> ans(board); i
2017-02-26 15:35:53
683
原创 Leetcode 524. Longest Word in Dictionary through Deleting
class Solution { public: static bool cmp(const string& s1, const string& s2) { if (s1.size() == s2.size()) return s1 < s2; return s1.size()>s2.size(); } string findLongestWord(string s, vec
2017-02-26 15:34:48
410
原创 Leetcode 523. Continuous Subarray Sum
class Solution { public: bool checkSubarraySum(vector& nums, int k) { int len = nums.size(); vector sum(len+5); for(int i= 1;i<=len;i++) sum[i] = sum[i-1] + num
2017-02-26 15:33:22
610
原创 Leetcode 530. Minimum Absolute Difference in BST
class Solution { public: int getMinimumDifference(TreeNode* root) { vector tem; dfs(root,tem); int ans; int len = tem.size(); ans = abs(tem[1]-tem[0]);
2017-02-26 15:32:14
373
原创 剑指offer 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
class Solution { public: bool IsBalanced_Solution(TreeNode* root) { if(!root) return true; int tem = getheight(root->left) - getheight(root->right); int ans = (
2017-02-25 00:03:18
244
原创 二叉树的深度
class Solution { public: int TreeDepth(TreeNode* pRoot) { if(!pRoot) return 0; return max(TreeDepth(pRoot->left),TreeDepth(pRoot->right))+1; } };
2017-02-24 23:31:04
218
原创 剑指offer 丑数
class Solution { public: int GetUglyNumber_Solution(int index) { int cur1 = 0; int cur2 = 0; int cur3 = 0; vector tem; int cur; tem.push_back(1); for (int i = 1; i <= index; i++) { c
2017-02-24 21:55:25
250
原创 剑指offer 把数组排成最小的数
bool mycmp(const string &A, const string &B) { return (A + B) < (B + A); } class Solution { public: string PrintMinNumber(vector numbers) { vector tem; string ans; int len = numbers.size();
2017-02-24 21:08:25
226
原创 剑指offer 整数中1出现的次数(从1到n整数中1出现的次数)
class Solution { public: int NumberOf1Between1AndN_Solution(int n) { int divide = 1; int cur; int ans = 0; int pre; int remain; while (n >= divide) { remain = n % divide; pre = n /
2017-02-24 20:24:14
215
原创 Leetcode 107. Binary Tree Level Order Traversal II
class Solution { public: vector> levelOrderBottom(TreeNode* root) { vector> ans; if(!root) return ans; queue aux; aux.push(root); while(!aux.emp
2017-02-23 20:45:09
240
原创 Leetcode 525. Contiguous Array
class Solution { public: int findMaxLength(vector& nums) { int len = nums.size(); unordered_map aux; aux[0] = -1; int cnt = 0; int ans = 0; for(int
2017-02-22 20:00:47
563
原创 剑指offer 字符串的排列
class Solution { public: vector Permutation(string str) { sort(str.begin(), str.end()); string tem = str; vector ans; while (tem != "") { ans.push_back(tem); getnext(tem); } return
2017-02-21 20:41:48
233
原创 剑指offer 复杂链表的复制
class Solution { public: RandomListNode* Clone(RandomListNode* pHead) { map aux; RandomListNode* cur = new RandomListNode(pHead->label); RandomListNode* head = cur; RandomListNode* tem = pHe
2017-02-20 20:09:14
283
原创 剑指offer 二叉树中和为某一值的路径
class Solution { public: vector > FindPath(TreeNode* root, int expectNumber) { vector > ans; vector test; dfs(ans, test, root, expectNumber); return ans; } void dfs(vector > &ans, vector &t
2017-02-19 21:43:48
429
原创 Leetcode Beautiful Arrangement
class Solution { public: int countArrangement(int N) { vector test(N+5,0); for (int i = 1; i <= N; i++) test[i] = i; int ans = 0; vector> ansvec; vector> avec; create(test, 1, N, ans);
2017-02-19 14:43:30
741
原创 leetcode 232. Implement Queue using Stacks
class MyQueue { public: /** Initialize your data structure here. */ stack pstack; stack auxstack; MyQueue() { } /** Push element x to the back of queue. */ void push(i
2017-02-17 20:01:20
402
原创 Leetcode 122. Best Time to Buy and Sell Stock II
class Solution { public: int maxProfit(vector& prices) { int len = prices.size(); if(!len) return 0; vector dp(len+5); for(int i=1;i<len;i++)
2017-02-16 21:39:17
206
原创 字符串转化为数学表达式并求值(后缀表达式)
主要用到后缀表达式和利用后缀表达式求值。 #include #include #include #include #include #include using namespace std; class Convert { public: Convert(string tot) :test(tot) {}; void init() { pri["("] = 0; pri[")"]
2015-11-23 00:29:02
1144
原创 LeetCode 290 Word Pattern
最近开始学习C++,然后开始算法题了。好像两年没有做过算法题了。。 题目链接 https://leetcode.com/problems/word-pattern/ 我的方法比较笨,首先将str转化为一个单词组,如果pattern 里面哪两个字符相等,那么单词组的单词也要相等。。 讨论区里大家的算法比较好,是纵向比较。就是一个映射。比如我pattern 里面的a对应的是str的dog
2015-10-07 16:51:07
426
原创 usaco 4.4.3
好久 没有写了,拓扑排序所有可能的结果写出来就好了。。 /* ID: zwfars1 LANG: C TASK:frameup */ #include #include #include typedef struct node { int xmin,ymin; int xmax,ymax; }comb; comb ars[30]={{0}}; char tem[20
2013-10-19 17:41:47
450
原创 usaco 4.3.1 buylow
#include #include #include struct node { int spend; char big[100]; }; struct node ans[5010]={{0}}; void add(char *a,char *b) //大数加法 { char sum[100]={0}; int len1 = strlen(a);//数
2013-09-10 16:49:27
504
原创 USACO 4.2.4/cowcycle
#include #include #include int f,r; //前后轮的齿数 int f1,f2; int r1,r2; //后轮的范围 double min=1000000; int ansf[100]={
2013-09-10 16:46:25
503
原创 USACO 4.2.2 stall4
#include #include #include int con[210][210]={{0}}; int visit[210]={0}; //记录是否相连,即饱和点 int tie[210]={0}; //tie[a]表示与a相连的点 int check(int a,int
2013-09-10 16:45:23
475
原创 USACO4.2.1/ditch
/* ID: zwfars1 LANG: C TASK:ditch */ #include #include #include #define inf 100000000 int ans=0; int queue[1000]={0}; int re[1000]={0}; //表示到i点的最大流 int pa[1000]={0};
2013-09-10 16:42:48
470
原创 USACO4.2.3/job
/*依次考虑每个需要的时间,然后分别算出a,b两道工序的 */ #include #include #include struct node { int cost; int end; }; struct node ma[1010]={{0}}; struct node mb[1010]={{0}}; int num; int numa,numb; void sort(stru
2013-09-10 16:42:13
601
原创 hdu1874(dijkstra)
发现好多算法都不会写了。 #include #include #include #define max 10000000 int m,n; int dis[205][205]; int s,t; void init(int ars[][205]) { int i ,j; for(i=0;i<202;i++) for(j=0;j<202;j++)
2013-07-17 23:03:33
362
原创 Number Sequence
和我以前写的斐波拉契数列一样的,就是改变下数组就好 #include #include void multiply(int a[][3],int b[][3]) { int i ,j,k; int c[3][3]={{0}}; for(i=0;i<2;i++) for(j=0;j<2;j++) for(k=0;k<2;k
2013-07-15 13:30:31
320
原创 Max sum
简单的动态规划 #include #include #include int sum,first[100005],last; void deal(int *a,int *ans,int n) { int i; for(i=2;i<=n;i++) { if(ans[i-1]+a[i]>=a[i]) { ans
2013-07-15 12:35:56
367
原创 0 1背包问题
Description 由于种种原因,Qinz大牛的Topcoder 的Rating又掉了....为了能够尽快使rating变红然后赶超各路教主,Qinz决定在以后的比赛中制订严格的比赛计划,以便尽早的实现这一愿望。 涨Rating可以通过做Topcoder的比赛来进行,一场Topcoder的比赛中n(1 Input 首先是一个数字T,表明测试数据的组数。 接下来的T组测试
2013-06-27 17:42:50
658
原创 USACO fence8 4.1.2
这个题好难我看了题解的,迭代搜索和二分法。 #include #include #include int boardnum; int railnum; int board[60]={0}; int rail[1050]={0}; int waste[1050]={0}; int check; int sum,total; int max; /*=====================
2013-06-10 23:46:32
669
原创 关于斐波那契数列的复杂度最低算法
Description zyf总是有很多奇异的想法,他最近常常幻想着以后能开这么一个工厂,可以把前三天里生产出来的东西拿到今天来拼在一起作为今天生产的东西。假如前三天生产出来的产品数分别是x,y,z,那么今天就能生产出x+y+z个。这样一来只要前三天的投入,接下来的工厂每一天都是0成本运作,但产品数却在极速增加,相当暴利。 当然,为了防止地球被破坏,为了维护世界的和平,zyf是不会
2013-06-07 00:30:12
1127
原创 USACO/nuggets 4.1.1
这个题算是运气吧,因为我就设了个上界10W。 没想到过了,但是不知道什么原理。 题解: 只要你知道以下的数论结论,这道题就是水背包了: 有两个数p,q,且gcd(q,p)=1,则最大无法表示成px+qy(x>=0,y>=0)的数是pq-q-p (对于n>pq-q-p,都可以表示成px+qy;而pq-q-p,就无法表示成px+qy)。 而且数越多,这个值只会越小。 所以我们只需考虑
2013-05-28 22:39:32
609
原创 USACO/rockers 3.4.4 动态规划
状态方程总是难想的。 #include #include int max(int a,int b) { if(a<b) return b; return a; } int main() { FILE *fin=fopen("rockers.in","r"); FILE *fout=fopen("rockers.out","w");
2013-05-26 00:33:15
424
原创 USACO/fence9 3.4.3
一个简单的题,没什么算法有的话估计就是积分思想吧。 但是别的题解说有定理的,我没有用,这题数据小,我这个 O(n)的就行了。 #include #include /*枚举横坐标,然后看纵坐标是否为整数就好*/ int main() { FILE *fin=fopen("fence9.in","r"); FILE *fout=fopen("fence9.out","w");
2013-05-25 09:01:04
454
原创 USACO/heritage 3.4.2 前序 中序 后序
一个简单的由先序和中序求后序的题,但是我多加了一个1,弄了半天 智商捉急。。。 #include #include int search(char s[],char key) { int i ; for(i=0;s[i];i++) { if(s[i]==key) { return i;
2013-05-24 22:46:47
400
原创 USACO/game1 3.3.5 博弈
这是一个博弈的问题,第一次做这种题,看了题解的。 最后思想什么的都在代码里的。 #include #include /* dp[i][j]=sum[i][j]-min(dp[i][j-1],dp[i+1][j]); 这个原理就是假设先假设取第一个, 然后第二个人取的话最大就是dp[i][j-1]; 假设先取的是最后一个最大就是dp[i+1][j]; 然后取两者最小的,就是第一个的最优
2013-05-23 22:59:16
350
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人