![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法与数据结构
happylife39
这个作者很懒,什么都没留下…
展开
-
LeetCode -146 LRU Cache
class LRUCache //least recently used{public: LRUCache(int capacity): _capacity(capacity) {} int get(int key) { HIPII::iterator iter = cache.find(key); if (iter原创 2016-08-18 22:36:02 · 457 阅读 · 0 评论 -
Sort List(归并排序)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* sor原创 2016-11-21 17:05:24 · 254 阅读 · 0 评论 -
32. Longest Valid Parentheses
class Solution {public: int longestValidParentheses(string s) { //使用DP,vector<int> res(s.size()), res[i]代表从s[j->i]的匹配圆括号数目 int n = s.size(); if(n<=1) return 0;原创 2016-11-21 14:44:21 · 199 阅读 · 0 评论 -
Best Time to Buy and Sell Stock IV-- LeetCode
这里我们先解释最多可以进行k次交易的算法,然后最多进行两次我们只需要把k取成2即可。我们还是使用“局部最优和全局最优解法”。我们维护两种量,一个是当前到达第i天可以最多进行j次交易,最好的利润是多少(global[i][j]),另一个是当前到达第i天,最多可进行j次交易,并且最后一次交易在当天卖出的最好的利润是多少(local[i][j])。下面我们来看递推式,全局的比较简单, global[i]转载 2016-11-19 15:52:15 · 202 阅读 · 0 评论 -
《编程之美: 求二叉树中节点的最大距离》的另一个解法
1.问题定义如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义”距离”为两节点之间边的个数。写一个程序求一棵二叉树中相距最远的两个节点之间的距离。#include <iostream>using namespace std;struct NODE{ NODE *pLeft; NODE *pRight;};struct RESULT{ int nMa转载 2016-10-16 11:29:23 · 199 阅读 · 0 评论 -
最长回文字串(Manacher算法)
class Solution {public: string longestPalindrome(string s) { if(s.empty() || s.size()==1) return s; int strLen = s.size(); vector<char> s1(2*strLen+2,'#');原创 2016-09-27 20:03:13 · 222 阅读 · 0 评论 -
检查一个数是不是回文数,很巧妙地方法
class Solution {public: bool isPalindrome(int x) { if(x<0|| (x!=0 &&x%10==0)) return false; int sum=0; while(x>sum) { sum = sum*10+x%10; x =翻译 2016-10-09 17:01:34 · 410 阅读 · 0 评论 -
基数排序(Radix Sort)的代码实现
可以帮助我理解基数排序 http://www.cnblogs.com/sun/archive/2008/06/26/1230095.html转载 2016-09-15 21:37:54 · 581 阅读 · 0 评论 -
并查集(Union-Find)算法介绍
并查集(Union-Find)算法介绍转载 2016-09-03 20:52:21 · 297 阅读 · 0 评论 -
219. Contains Duplicate II
class Solution { public: bool containsNearbyDuplicate(vector& nums, int k) { //map原创 2016-08-23 11:26:13 · 175 阅读 · 0 评论 -
从B树、B+树、B*树谈到R 树
http://blog.csdn.net/v_JULY_v/article/details/6530142转载 2016-09-01 14:51:00 · 158 阅读 · 0 评论 -
226. Invert Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2016-08-22 20:21:07 · 141 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int size1 = nums1.size(); int size2 = nums2.size(); if(size1 > size2)原创 2016-08-22 12:50:31 · 152 阅读 · 0 评论 -
Trie树
一.Trie树1.Trie 定义> Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。> > Trie的核心思想是空间换时间。利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。> >原创 2016-08-31 21:10:43 · 178 阅读 · 0 评论 -
65. Valid Number
class Solution { public: bool isNumber(string s) {if (s.size() == 0){ return false;}int len = s.size();int index = 0;while(index < len && s[index] == ' ') //去空格 index+原创 2016-08-22 09:15:36 · 171 阅读 · 0 评论 -
283. Move Zeroes
#include#include#include#include#include#includeusing namespace std;void moveZeroes(vector& nums) { if (nums.size() == 0 || nums.size() == 1) { retur原创 2016-08-19 15:01:05 · 150 阅读 · 0 评论 -
59. Spiral Matrix II
class Solution {public: vector> generateMatrix(int n) { vector> matrix; if(n return matrix; matrix.resize(n); for (int i = 0; i {原创 2016-08-19 10:48:08 · 139 阅读 · 0 评论 -
LeetCode-54.Spiral Matrix I
class Solution {public: vector spiralOrder(vector>& matrix) { vector result; if(matrix.size() == 0 || matrix[0].size() == 0) //对输入参数的检查 return result;原创 2016-08-19 10:23:15 · 215 阅读 · 0 评论 -
Substring with Concatenation of All Words
class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { int n = s.size(); int cnt = words.size(); vector<int> ans; if(n<=0 || cnt<=0)原创 2016-11-28 12:43:37 · 184 阅读 · 0 评论