文章分类 -  经典

1
摘要:dp,时间O(m*n),m为目标值,n为硬币数,空间O(m)/* * Given a list of 'N' coins, their values being in an array A[], return the minimum number of coins required to sum to 'S' (you can use as many coins you want). If it's not possible to sum to 'S', return -1 * * * Input #01: * Coin denomina 阅读全文
posted @ 2013-09-15 23:40 dmthinker 阅读(147) 评论(0) 推荐(0)
摘要:O(n)的算法#include #include #include #include using namespace std;int LongestPlalindrome(const string& str) { if (str.empty()) return 0; string recombined_str; max_len = 1; recombined_str.resize(2 * str.size() - 1); for (int i = 0; i pld_len; pld_len.resize(recombined_str.size(), 1); int farres... 阅读全文
posted @ 2013-09-15 22:43 dmthinker 阅读(153) 评论(0) 推荐(0)
摘要:1。 肯定不能用dijkstra算法,这是因为,Dijkstra算法的大致思想是每次选择距离源点最近的结点加 入,然后更新其它结点到源点的距离,直到所有点都被加入为止。当每次选择最短的路改为每次选择最长路的时候,出现了一个问题,那就是不能保证现在加入的结 点以后是否会被更新而使得到源点的距离变得更长,而这个点一旦被选中将不再会被更新。例如这次加入结点u,最长路为10,下次有可能加入一个结点v,使得 u通过v到源点的距离大于10,但由于u在之前已经被加入到集合中,无法再更新,导致结果是不正确的。 如果取反用dijkstra求最短路径呢,记住,dijkstra不能计算有负边的情况。。。2.可以用. 阅读全文
posted @ 2013-09-01 17:18 dmthinker 阅读(9737) 评论(0) 推荐(1)
摘要:对于数组A,A[i]的逆序对数量为i之前比它大的数的个数。如果已经知道A中的最大值max,当我们顺序的去求A[i]的逆序对的数量的时候,其实就是找此时A中i之前A[i]到max的数有多少个。当然,对于求区间内的事情,树状数组和线段树是最为擅长的。一下代码仅仅考虑A中的元素为整数的情况,如果数太大者有double的情形需要先离散化处理。#include #include #include #include using namespace std;struct Node { Node(int h, int t) : head(h), tail(t), left(NULL), right(NULL. 阅读全文
posted @ 2013-08-25 19:23 dmthinker 阅读(605) 评论(0) 推荐(0)
摘要:使用归并的逻辑,在两个子数组拍完序之后,归并之前,后一个数组中的每一个数字a,前一个数组中与a配对的逆序对的数量可以用归并排序的方式求得。#include #include using namespace std;int Merge(vector& arr, int b, int e) { if (b >= e) return 0; int reverse_pair_num = 0; int mid = (b + e) / 2; vector tmp; tmp.resize(e - b + 1); int i = b; int j = mid + 1; int pos = 0;. 阅读全文
posted @ 2013-08-25 19:07 dmthinker 阅读(142) 评论(0) 推荐(0)
摘要:使用并查集的经典案例,未经编译,仅供参考class UnionFind { public: UnionFind(int size) : size_(size) { fathers_.resize(size_ + 1); for (int i = 1; i size_ || b > size_ || a degrees_[father_b]) { fathers_[father_b] = father_a; rels_[father_b] = (rels_[a] - rels_[b] + 2 + rel) % 3; degrees_[father_... 阅读全文
posted @ 2013-08-22 15:40 dmthinker 阅读(111) 评论(0) 推荐(0)
摘要:代码未经编译,仅供参考。lca的tarjan算法很巧妙的运用了并查集,因为这种方法是使用深度优先遍历的方式递归的由下而上的使用并查集(在visit某一节点的同时,把当前节点与子树合并),所以在查找两个点A和B的最近公共祖先的时候,如果A已经visit,在visit B的时候A当前的祖先必定是其最近公共祖先tarjan算法比较适用于同时查找多对节点的最近公共祖先。只需要遍历一遍即可。class UnionFind { public: UnionFind(int size) : size_(size) { fathers_.resize(size_); for (int i = 0;... 阅读全文
posted @ 2013-08-22 15:34 dmthinker 阅读(405) 评论(0) 推荐(0)
摘要:Q:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, given n = 2, return [0,1,3,2]. Its gray 阅读全文
posted @ 2013-07-23 00:40 dmthinker 阅读(91) 评论(0) 推荐(0)
摘要:Q:Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]A:每次将所有相同的元素作... 阅读全文
posted @ 2013-07-22 21:26 dmthinker 阅读(92) 评论(0) 推荐(0)
摘要:Q:Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ?m?n? length of list.A:其实还是比较简单的,但是凭想象去交换元素的时候还是让我很烦躁,空间思维能力极差无比。。。。。。。。没 阅读全文
posted @ 2013-07-21 23:08 dmthinker 阅读(88) 评论(0) 推荐(0)
摘要:Q:A:mirrors中序遍历,发生错误的节点保存,最后交换,主要分两种情况,错误的两个节点相邻或者不相邻。还是有点烦躁的。class Solution { public: void recoverTree(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function TreeNode* pre = NULL; TreeNode* cur = root; TreeNode* wrong_node1 = NULL; Tre... 阅读全文
posted @ 2013-07-08 21:05 dmthinker 阅读(102) 评论(0) 推荐(0)
摘要:Q:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs.For example, givens="aab",Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut.A:跟I类似,还是先保 阅读全文
posted @ 2013-07-06 21:12 dmthinker 阅读(105) 评论(0) 推荐(0)
摘要:Q:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most wat 阅读全文
posted @ 2013-06-18 21:53 dmthinker 阅读(91) 评论(0) 推荐(0)
摘要:Q:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are 阅读全文
posted @ 2013-06-18 11:51 dmthinker 阅读(186) 评论(0) 推荐(0)
摘要:Q:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,1... 阅读全文
posted @ 2013-06-17 13:04 dmthinker 阅读(104) 评论(0) 推荐(0)
摘要:Q:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?A:F(n) = F(n - 1) + F(n - 2)写了3个解决办法,solution1显然会超时,因为有大量的重复计算,solution3是对它的改进,代码相当的简单。solution2是利用了传说中的fibonacci数列,fibonacci是个变态吧,还有那 阅读全文
posted @ 2013-06-17 12:22 dmthinker 阅读(99) 评论(0) 推荐(0)
摘要:Q:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units o 阅读全文
posted @ 2013-06-16 21:18 dmthinker 阅读(120) 评论(0) 推荐(0)
摘要:Q:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?A:假设为n*n矩阵,则从外至内有n/2个圈需要旋转,每个圈旋转的时候是有固定的规律的,在纸上写一写应该就能发现,matrix[i][j] = matrix[n - j - 1][i],代码就很easy了。时间复杂度o(n^2),空间复杂度o(1),ok!class Solution {public: void rotate(... 阅读全文
posted @ 2013-06-16 17:14 dmthinker 阅读(140) 评论(0) 推荐(0)
摘要:Q:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array.还是使用二分法,通过比较最左边,中间以及target的大小关 阅读全文
posted @ 2013-06-15 16:20 dmthinker 阅读(98) 评论(0) 推荐(0)
摘要:如果在递归的最后环节是求两个数的和与target最近的情况,应该会快一些。当然,主要是为了解决4Sum及以上的情形,所以使用了dp加递归的方式,3Sum的情形使用非递归的方式会更快也会比较简洁。class Solution { public: int threeSumClosest(vector<int> &num, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function sort(num.begin(), num.end()); retur... 阅读全文
posted @ 2013-06-13 15:15 dmthinker 阅读(125) 评论(0) 推荐(0)

1