算法
文章平均质量分 70
寻MEa
你要的明天,定会如约而至
展开
-
回文子串
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s ="aab",Return [ ["aa","b原创 2017-06-18 00:08:00 · 203 阅读 · 0 评论 -
scramble string (使用动态规划和递归做)
点击打开链接class Solution {public:bool solve(string s1, string s2, int left1, int right1, int left2)//串的位置{int dist = right1 - left1;if (dist == 1){return s1[left1] == s2[left2];}int la原创 2017-06-20 22:01:10 · 378 阅读 · 0 评论 -
pat 中求解最长回文串的长度
本以为暴力枚举会超时,但竟然过了。。。#include #include #include int longestPalindrome(string s) {int left = 0, right = s.length() - 1;//为奇数时int len1 = 0;int len2 = 0;int start1 = left;//标记起始点int sta原创 2017-06-20 22:26:38 · 213 阅读 · 0 评论 -
Word_ladder
题目链接参考了大神的代码#include #include class Solution {public:void gen_path(unordered_map>& father, vector& path, string start, string word,vector>& ret){path.push_back(word);if (start == w原创 2017-06-20 22:42:34 · 174 阅读 · 0 评论 -
PAT题目 有几个PAT(25)
题目链接有点动态规划的味道。#include int main(){char s[100];int p=0, pa=0, pat=0;scanf("%s", s);int len = strlen(s);for (int i = 0; i {switch (s[i]){case 'p':p += 1;break;case 'a':原创 2017-06-24 23:27:40 · 332 阅读 · 0 评论 -
二叉搜索树的性质
期末爆炸,昨晚作业来写一道题~~~题目描述输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。public class Solution { public boolean VerifySquenceOfBST(int [] sequence) {原创 2017-07-04 23:24:47 · 518 阅读 · 0 评论 -
剑指offer值路径和查找
好久不用Java来写了,花了好久才accept题目描述输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。public class TreeNode { int val = 0; TreeNode left = null; TreeNode right =原创 2017-07-05 00:07:46 · 156 阅读 · 0 评论 -
leetcode 字符串枚举
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s ="aab",Return [ ["aa","b原创 2017-07-06 16:48:38 · 212 阅读 · 0 评论 -
LeetCode word latter
题目描述Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start toend, such that:Only one letter can be changed at a timeEach inte原创 2017-07-06 16:50:12 · 294 阅读 · 0 评论 -
LeetCode maximum path num
题目描述Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3原创 2017-07-06 16:51:44 · 181 阅读 · 0 评论 -
leetcode
题目描述Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may on原创 2017-07-06 16:53:37 · 191 阅读 · 0 评论 -
剑指offer 旋转数组的最小数字
题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。旋转之后的数组实际上可以划分成两个有序的子数组:前面子数组的大小都大于后面子数组中的原创 2017-08-12 21:33:33 · 194 阅读 · 0 评论 -
LeetCode decode ways
点击打开链接class Solution {public: bool isdigit(char c){return c >= '0'&&c }int numDecodings(string s) {int dp[1000] = { 1 };//dp[i]表示前i个字符的方 if (s.length() == 0 || s[0] == '0')原创 2017-06-20 21:58:22 · 166 阅读 · 0 评论 -
LeetCode 重建BST
点击打开链接 struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} };class Solution {public: vector原创 2017-06-20 21:54:48 · 318 阅读 · 0 评论 -
链表复制
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list. struct RandomListNode { i原创 2017-06-17 23:50:54 · 296 阅读 · 0 评论 -
LeetCode算法搜索
Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surrounded region .For example,X X X XX O O XX X O XX原创 2017-06-18 00:13:03 · 168 阅读 · 0 评论 -
pat水题
自以为对二进制有很深的理解,面对这种水题,切了好久。。。题目链接#include #include typedef long long ll;bool compare(ll a, ll b, ll c)//防止大数相加溢出{if (a > 0 && b > 0){if (a > LLONG_MAX - b)return true;}if原创 2017-06-18 12:41:50 · 285 阅读 · 0 评论 -
pat stack模拟,老超时wa......
#include #include #include #include #include using namespace std;class st{private:vector vec;public:st(){};~st(){};void pop(){if (vec.size() == 0){printf("%s\n", "Inv原创 2017-06-18 22:47:30 · 205 阅读 · 0 评论 -
pat水题
题目链接int main(){char a[100], b[100];int a1, b1, c1, a2, b2, c2,a3,b3,c3;scanf("%d.%d.%d", &a1, &b1, &c1);scanf("%d.%d.%d", &a2, &b2, &c2);int ca1, ca2;//表示进位c3 = (c1 + c2) % 29;ca1 =原创 2017-06-19 22:39:31 · 148 阅读 · 0 评论 -
pat 精度问题
点击打开链接老TLE,可以看出浮点运算的复杂程度远大于整数运算。怎么优化。。。int main(){float f1, f2;int dignum;scanf("%d", &dignum);scanf("%f %f", &f1, &f2);float t1 = f1, t2 = f2;int count1 = 0, count2 = 0;while (t原创 2017-06-20 13:06:09 · 262 阅读 · 0 评论 -
count the pat
counting the patint main(){int p=0, pa=0, pat = 0;char s[100000] = { 0 };scanf("%s", s);int i = 0;while (s[i]!=0){switch (s[i]){case 'P':p++;break;case 'A':pa += p;pa原创 2017-06-20 21:37:25 · 170 阅读 · 0 评论 -
leetcode 八皇后问题
题目链接class Solution {public:int a[100];bool issuit(int a[], int n, int row, int col)//判断是否适合放置{for (int i = 0; i {if (abs(a[i] - col) == abs(row - i) || col == a[i])return false;}原创 2017-06-20 21:43:53 · 639 阅读 · 0 评论 -
LeetCode二叉树的恢复和重建(前序和中序)
点击打开链接 struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };class Solution {public: TreeN原创 2017-06-20 21:47:01 · 270 阅读 · 0 评论 -
LeetCode二叉树的层序遍历的输出
题目链接 struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };class Solution {public: vector原创 2017-06-20 21:49:51 · 709 阅读 · 0 评论 -
LeetCode动态规划
题目链接Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 ="aabcc",s2 ="dbbca",When s3 ="aadbbcbcac", return true.When s3 ="aadbbbac原创 2017-06-20 21:52:31 · 185 阅读 · 0 评论 -
快排的递归和非递归版
#include<iostream>#include<vector>#include<stack>#include<cstdlib>#include<algorithm>using namespace std; /**把数组分为两部分,轴pivot左边的部分都小于轴右边的部分**/template <typename Compara...转载 2018-04-28 12:25:01 · 355 阅读 · 0 评论