
Algorithm
猫和鱼爪
想学自己喜欢的一切
展开
-
【ACM】从零做起——poj1002
487-3279Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 253135 Accepted: 45096DescriptionBusinesses like to have memorable telephone numbers. One way to原创 2015-03-23 18:40:24 · 706 阅读 · 0 评论 -
【leetcode】Unique Paths II
QuestionTipsI think still use dynamic programming.class Solution {public: int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) { int m = obstacleGrid.size(); int n = ob原创 2015-05-30 02:13:10 · 443 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal
QuestionThis may not be a tough problem,but that rack my brain by a while… So I want to record it down for myself.Code struct TreeNode { int val; struct TreeNode *left; struct TreeN原创 2015-06-27 11:10:41 · 385 阅读 · 0 评论 -
【leetcode】House Robber && House Robber II
ProblemHintUsing Dynamic Programming.Codeint rob(int* nums, int numsSize) { if(numsSize == 0) return 0; else if(numsSize == 1) return nums[0]; int res[numsSize]; res[0]原创 2015-06-29 21:05:20 · 400 阅读 · 0 评论 -
【leetcode】Convert Sorted Array/List to Binary Search Tree
ProblemCodeSolution1/* * Dynamic programming,divide problem as two smaller */struct TreeNode* sortedArrayToBST(int* nums, int numsSize) { if(!numsSize) return NULL; struct TreeNode *pNode原创 2015-06-30 13:05:38 · 574 阅读 · 0 评论 -
【leetcode】Distinct Subsequences
ProblemHintDP对于动态规划,我一直都是有些“望而生畏”的,不过人就是要这么迎难而上不是么,刺激!这次利用的是二维数组的备忘录形式,令path[i][j]表示t[i]在s[j]中的匹配情况。 状态转移方程 path[i][j] = path[i][j-1]+t[i-1]==s[j-1]?path[i-1][j-1]:0;如果能把状态方程转化为自己的语言,把所有的情况都能用文字表达出来,原创 2015-07-04 01:49:29 · 419 阅读 · 0 评论 -
【LeetCode】Linked List Cycle I&II
ProblemCode/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */bool hasCycle(struct ListNode *head) { if(head==NULL) retur原创 2015-07-11 00:48:19 · 476 阅读 · 0 评论 -
【leetcode】Populating Next Right Pointers in Each Node I&&II
ProblemHintDFSCode/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * struct TreeLinkNode *left, *right, *next; * }; * */ void DFS(struct TreeLinkNode *ro原创 2015-07-04 15:02:32 · 373 阅读 · 0 评论 -
【leetcode】Best Time to Buy and Sell Stock I&II&III
Problem 1HintDPCodeint maxProfit(int* prices, int pricesSize) { if(!pricesSize) return 0; int cur = 0; int max = 0; for(int i=1;i<pricesSize;++i){ cur += prices[i]-prices[i-1];原创 2015-07-06 23:06:59 · 447 阅读 · 0 评论 -
【leetcode】Binary Tree Maximum Path Sum
ProblemHintDFSCode/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int maxPath(struct TreeNode *root原创 2015-07-07 10:22:19 · 443 阅读 · 0 评论 -
【leetcode】Candy
Problem Code/*#include <stdio.h>#include <stdlib.h>#include <stdbool.h> * 1、找出“两边高,中间低”的那个点,设为1(可能有很多这样的点); * 2、满足“任意一个孩子都至少有一颗糖”,那么就是从1中这些点出发,分别向两边递增; * 3、每两个点之间以较长的序列为基准! */void print(int n原创 2015-08-01 23:36:02 · 379 阅读 · 0 评论 -
【leetcode】Min Stack
ProblemCodetypedef struct { int *value; //Array to put elements int minIndex; //The minimum's index unsigned int length; //The elements' number unsigned int size; //The ca原创 2015-07-26 00:49:06 · 386 阅读 · 0 评论 -
【leetcode】Product of Array Except Self
ProblemCode/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */int* productExceptSelf(int* nums, int numsSize, int* returnSize) {原创 2015-07-20 23:47:35 · 418 阅读 · 0 评论 -
【leetcode】Kth Smallest Element in a BST
ProblemCode/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */bool getTheNumber(struct TreeNode *root,原创 2015-07-14 23:57:53 · 367 阅读 · 0 评论 -
【leetcode】No. 235 LCABST
ProblemCode/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */struct TreeNode* lowestCommonAncestor(st原创 2015-07-11 10:20:55 · 467 阅读 · 0 评论 -
【leetcode】Text Justification
Question 我觉得还是翻译一下好 给一个字符串数组,一个L整数 1、一行最多有L个字符 2、把这些字符串尽可能多得放在一行 3、每个字符串之间都有空格分开 4、字符串之间空格的个数平均分配,如果不能除尽,那么左边的空格数大于右边的 5、最后一行字符串左对齐,之间以一个空格分隔,剩余用空格补齐TipsNo way, just let it be complex : )Codecla原创 2015-05-31 10:32:17 · 398 阅读 · 0 评论 -
【leetcode】Unique Paths
QuestionTips Using the dynamic programmingCodeclass Solution {public: int uniquePaths(int m, int n) { int **res; res = new int*[m]; for(int i = 0; i < m; ++i){原创 2015-05-30 01:00:54 · 374 阅读 · 0 评论 -
【leetcode】Valid Sudoku
题目让你判断二维数组是否满足数独要求(其中存在'.',表示没有填数字)。提示用哈希表做,那我就思考,最少也要遍历完一遍二维数组你才能知道结果对吧。那么如何才能在O(N2)内解决问题呢。想一想,对一行做一个哈希数组。恩,行的问题就解决了。仔细一看,发现这是个n*n矩阵啊,那么列的问题不就同时能和行一起解决了么。对的,再看,一行有三个3*3矩阵,每三行重置一次,又把3*3矩阵给解决了。那就写一下代原创 2015-04-14 08:06:26 · 575 阅读 · 0 评论 -
【leetcode】Trapping Rain Water
【leetcode】Trapping Rain Water 最近做做leetcode玩,虽然才做40+,但是觉得进步还是挺大的,数学知识增加了,思维方式发散了,打算继续坚持下去吧。有些事也不尽如人意,索性做自己喜欢的事得了。就像一起努力的同学说的一样,立志做一个“全栈工程师”,做个独立开发者,在深山绣湖前敲敲代码,到处旅行。这样,其实挺好。原创 2015-04-25 20:05:40 · 403 阅读 · 0 评论 -
【leetcode】Sudoku Solver
【leetcode】Sudoku Solver 题目如上,我先说一句实话,在想算法和实现的过程中,遇到各种小错误。写算法和做应用程序实在是不同,平时做个应用,借助各种工具还能debug个所以然来,可是写算法题目要么想不出,想出了debug起来和再重新想一遍更不没差别……折腾了两小时总算是把这个心结解开了。题目用的是回溯法。其实不论是回溯法还是DP(我是指这一类绕来绕去的),想清楚出口,原创 2015-04-24 19:47:36 · 529 阅读 · 0 评论 -
【leetcode】Multiply Strings
【leetcode】Multiply Strings 大数相乘,似乎是笔面试经常光顾的题目,就记一下吧。string multiply(string num1, string num2) { string sum(num1.size() + num2.size(), '0'); for (int i = num1.size() - 1; 0 <= i; -原创 2015-04-25 21:59:14 · 382 阅读 · 0 评论 -
【leetcode】First Missing Positive
【leetcode】First Missing Positive找第一个没有出现的正数,我感觉很简单啊,为什么是hard?我打算用一个数组把正数作为下表映射到数组中。然后么,直接遍历数组,找到第一个没出现的就ok。class Solution {public: int firstMissingPositive(vector& nums) { if(nums.si原创 2015-04-24 22:53:20 · 407 阅读 · 0 评论 -
【leetcode】Combination Sum
【leetcode】Combination Sum还是一道回溯题,趁热打铁吧算是,题目我就不解读了。重点还是几个:退出条件、状态查询、状态记录、记录回滚、递归尝试(好吧,我每次都会说得不一样,但是回溯就这几点,特别容易出错的是状态不满足时的记录回滚)。代码其实写得挺丑的,我是从后往前遍历,这样可以省去一些“从前往后遍历时不必要的尝试”,但是leetcode总是说我错误,其实错在排序原创 2015-04-24 21:39:53 · 338 阅读 · 0 评论 -
【leetcode】Jump Game II
【leetcode】Jump Game II 恩……没看tips之前,我看了题目就觉得是DP,和《算法导论》上的切钢条几乎每什么两样。 状态转移方程 |-> 0 (i=the last position)m[i]= |-> 1 (i+jump distance >= the last position) |->原创 2015-04-26 08:42:15 · 499 阅读 · 0 评论 -
【LeetCode】Permutations
QuestionTips BacktrackingCodeclass Solution{ public: void BT(int depth,vector<int> &nums){ if(depth==nums.size()){ res.push_back(tmp); return; }else{ for(int i原创 2015-05-04 23:00:03 · 315 阅读 · 0 评论 -
【LeetCode】Rotate list
QuestionMethod1 Using to pointers to make the distance equal k%ListNode.length()//This is got from a loop/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *原创 2015-05-12 22:57:13 · 357 阅读 · 0 评论 -
【POJ】Parencodings
ParencodingsTime Limit: 1000MS Memory Limit: 10000K Total Submissions: 21004 Accepted: 12590Description Let S = s1 s2…s2n be a well-formed string of parentheses. S can be encoded in two d原创 2015-05-05 17:11:00 · 405 阅读 · 0 评论 -
Rotate Image
Questionclass Solution {public: void rotate(vector<vector<int> >& matrix) { for(int layer=0;layer<matrix.size()/2;++layer){ //View matrix like many circles int row=matrix.size()-2*l原创 2015-05-05 13:57:09 · 284 阅读 · 0 评论 -
【LeetCode】Permutations II
QuestionTipsStill the backtrackingSource Codeclass Solution {public: void BT(int d,vector<int> &nums){ if(d==nums.size()){ vector<int> t; for(int i=0;i<tmp.size();++i)原创 2015-05-05 11:19:51 · 353 阅读 · 0 评论 -
【LeetCode】Anagrams
QuestionTipsHash tableCodeclass Solution {public: vector<string> anagrams(vector<string>& strs) { res.clear(); ht.clear(); if(strs.size()<=1) return res;原创 2015-05-05 20:34:49 · 437 阅读 · 0 评论 -
N-Queens && N-Queens II
QuestionTipsBacktracking //You know ,I can do it well : ) Codeclass Solution {public: Solution(){ res.clear(); rec=NULL; } ~Solution(){ if(rec!=NULL) delete []rec;原创 2015-05-06 20:18:53 · 376 阅读 · 0 评论 -
【latched】Single Number II
ProblemCodeint singleNumber(int* nums, int numsSize) { int tmp = 0; for(int i=1;i<numsSize;++i){ tmp = nums[0] & (tmp ^ nums[i]); nums[0] = tmp | (nums[0] ^ nums[i]); }原创 2015-08-05 14:56:04 · 446 阅读 · 0 评论