![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++ 算法
Yang_Snow
这个作者很懒,什么都没留下…
展开
-
[Leetcode]Largest Rectangle in Histogram
//用栈实现class Solution {public: int largestRectangleArea(vector& height) { stack index; vector high=height; int result=0; int temp; high.push_back(0); for(int i=0;原创 2015-07-25 06:46:02 · 471 阅读 · 0 评论 -
[Leetcode]Subsets II
//DFS,要改变temp和result的值,所以要用到&//每次循环最后要pop出最后一个元素class Solution {private: vectortemp; vector>result; void sub_set(vector S, vector&temp, vector>&result,int index) { r原创 2015-01-27 06:32:18 · 353 阅读 · 0 评论 -
[Leetcode]Path Sum II
//DFS和二叉树class Solution {private: vectortemp; vector>result; void solve_dfs(TreeNode *root,int sum,vector temp,int add) { if(root==NULL) return; if(r原创 2015-01-27 06:35:44 · 297 阅读 · 0 评论 -
[Leetcode]Validate Binary Search Tree
//需要判断zuozis/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}原创 2014-10-13 08:28:27 · 448 阅读 · 0 评论 -
[Leetcode]ZigZag Conversion
//用codeblockclass Solution {public: string convert(string s, int nRows) { int len=s.length(); string res=""; if(s.empty())return s; if(len in原创 2014-10-03 07:26:08 · 322 阅读 · 0 评论 -
[Leetcode]Populating Next Right Pointers in Each Node
思路:很棒的一道题,可以认为是先序遍历。(1)根据题述:左孩子为空,则右孩子一定为空,所以左孩子为空,则return(2)如果左孩子不为空,则右孩子一定不为空,所以链接左孩子和右孩子即可(左孩子的next赋值为右孩子)(3)由于先序遍历,所以父节点的next比子节点的next先被设置,故父节点不同的两个子节点进行连接,就可以用到父节点的next,整题的精华便转载 2014-10-13 09:01:15 · 392 阅读 · 0 评论 -
[Leetcode]Generate Parentheses
//网上很多人写的是递归,class Solution {public: vector generateParenthesis(int n) { vector res; generate(n,n,"",res); return res; } void generate(int left, int right原创 2014-10-13 05:16:09 · 367 阅读 · 0 评论 -
[Leetcode]3Sum
//因为疏忽经常得不到[0,0,0]class Solution {public: vector > threeSum(vector &num) { sort(num.begin(),num.end()); vector> result; vectortemp; int n=num.size();原创 2014-10-03 08:19:35 · 260 阅读 · 0 评论 -
[Leetcode]Longest Palindromic Substring
//最长回转子串原创 2014-10-02 07:37:57 · 273 阅读 · 0 评论 -
[leetcode]Permutations
大家好像都在转载这篇...写的不错http://blog.csdn.net/tuantuanls/article/details/8717262原创 2014-09-30 08:35:44 · 333 阅读 · 0 评论 -
[Leetcode]Linked List Cycle II
//list链表的题目class Solution {public: ListNode *detectCycle(ListNode *head) { if(head==NULL) return NULL; ListNode *first=head; ListNode *second=head;原创 2015-01-28 06:50:39 · 311 阅读 · 0 评论 -
[Leetcode]Linked List Cycle
//判断链表是否有环class Solution {public: bool hasCycle(ListNode *head) { ListNode *first=head; ListNode *second=head; while(second!=NULL&&second->next!=NULL) {原创 2015-01-28 06:54:03 · 349 阅读 · 0 评论 -
[Leetcode]Pascal's Triangle II
//similar with the last one.class Solution {public: vector getRow(int rowIndex) { vector result; vector last; result.push_back(1); if(rowIndex==0)原创 2015-01-23 04:35:12 · 294 阅读 · 0 评论 -
[Leetcode]Power of Two
//位运算,不断找出最后一位看是否是0,然后右移一位//要注意n=0的情况,为falseclass Solution {public: bool isPowerOfTwo(int n) { if(n==0)return false; while(!(n&1)) n>>=1; if(n==1)return原创 2015-07-16 13:30:48 · 513 阅读 · 0 评论 -
[Leetcode]Basic Calculator II
//取vector作为一个栈,然后不断读取和pop。先把乘积或商算好再存入栈中//ps.好久没有不参考别的直接自己写出AC代码了...各种bug...果然要好好练...//1.考虑空格情况;2.考虑数位情况;3.考虑index的边界情况class Solution {public: int calculate(string s) { int len=s.原创 2015-07-16 12:27:35 · 620 阅读 · 0 评论 -
[Leetcode]Kth Smallest Element in a BST
//用search计算左子树的节点个数,加上根节点本身若为k则输出,否则//(1)若k大于目前个数,则k-左子树节点个数,再计算右子树//(2)若k小于目前个数,则直接计算左子树/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left;原创 2015-07-29 03:04:10 · 687 阅读 · 1 评论 -
[Leetcode]Reorder List
//1.把list存到数组中,然后重新整理数组得到最后的结果;2.第二个while里面不是class Solution {public: void reorderList(ListNode* head) { vector node; if(head==nullptr)return; ListNode* cur=head;原创 2015-07-14 04:40:58 · 299 阅读 · 0 评论 -
[Leetcode]Reverse Linked List II
//将list存入vector,然后翻转中间部分数列class Solution {public: ListNode* reverseBetween(ListNode* head, int m, int n) { vector node; ListNode* cur = head; for(int i=0;i原创 2015-07-14 08:54:20 · 600 阅读 · 0 评论 -
[Leetcode]Minimum Window Substring
class Solution {public: string minWindow(string s, string t) { vector found(128,0); vector needfound(128,0); for(int i=0;i needfound[t[i]]++; found[翻译 2015-07-26 03:32:01 · 384 阅读 · 0 评论 -
[Leetcode]Insert Interval
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */原创 2015-02-26 02:47:18 · 381 阅读 · 0 评论 -
[Leetcode]Anagrams
//回文,主要注意map的使用class Solution {public: vector anagrams(vector &strs) { map temp; vector res; int num = strs.size(); string s; for(int i=0;i原创 2015-02-26 03:35:23 · 325 阅读 · 0 评论 -
[Leetcode]Pascal's Triangle
// 比较直白的一道题。缓存一个last,memory为O(n),把前一排数字存入以确定下一排数字class Solution {public: vector > generate(int numRows) { vector temp; vector last; temp.push_back(1); ve原创 2015-01-23 04:34:40 · 311 阅读 · 0 评论 -
[Leetcode]Maximum Product Subarray
//用curmin和curmax来记录class Solution {public: int maxProduct(int A[], int n) { if(n==0)return 0; if(n==1)return A[0]; int curmin=A[0]; int curmax=A[0];原创 2014-10-01 08:54:01 · 366 阅读 · 0 评论 -
[Leetcode]Substring with Concatenation of All Words
class Solution {public: vector findSubstring(string S, vector &L) { vector res; int l_len=L[0].length(); map word_count;//建两个map,存储L里面的string出现的次数 map counti原创 2014-10-09 03:26:41 · 418 阅读 · 0 评论 -
[Leetcode]Valid Sudoku
class Solution {public: bool isValidSudoku(vector > &board) { bool row[9]={0}; bool col[9]={0}; for(int i=0;i { for(int j=0;j {原创 2014-09-30 07:19:56 · 269 阅读 · 0 评论 -
[Leetcode]Combination sum
//DFSclass Solution {public: vector > combinationSum(vector &candidates, int target) { vector>result; vectortemp; sort(candidates.begin(),candidates.end());原创 2014-07-25 06:59:35 · 360 阅读 · 0 评论 -
[转载]树的先序遍历,中序遍历,后序遍历的非递归写法
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive soluti转载 2014-07-05 09:34:13 · 587 阅读 · 0 评论 -
[Leetcode]Maximum Depth of Binary Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *///同原创 2014-07-03 12:17:56 · 416 阅读 · 0 评论 -
[Leetcode] Same Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *///遍原创 2014-07-03 12:10:54 · 329 阅读 · 0 评论 -
[Leetcode] Word Break
//DP,先设置n+1class Solution {public: bool wordBreak(string s, unordered_set &dict) { int m=dict.size(); if(m==0)return false; int n=s.size(); vector f(n+1,fa原创 2014-07-23 02:23:52 · 316 阅读 · 0 评论 -
[Leetcode] Restore IP Addresses
class Solution {public: vector restoreIpAddresses(string s) { int len=s.size(); vector result; if(len=13)return result; int first=0; int second=0;原创 2014-06-30 02:58:37 · 339 阅读 · 0 评论 -
[Leetcode] Decode Ways
//类似斐波那契数列//考虑多种情形,单个0,连续2个0,class Solution {public: int numDecodings(string s) { int len=s.size(); if(s.empty()||s[0]=='0') return 0; if(len==1)原创 2014-07-20 07:30:48 · 420 阅读 · 0 评论 -
[Leetcode] Climbing Stairs
//斐波那契数列class Solution {public: int climbStairs(int n) { int first,second; if(n==0)return 0; if(n==1)return 1; if(n==2)return 2; first=0;secon原创 2014-07-20 01:49:11 · 431 阅读 · 0 评论 -
[Leetcode]Palindrome Partitioning II
class Solution {public: int minCut(string s) { //int result=INT_MAX; const int num=s.size(); bool pal[num][num]; int f[num+1]; for(int i=0;i翻译 2014-07-19 02:54:39 · 410 阅读 · 0 评论 -
[Leetcode]Unique Path II
//类似于unique path,只是加了//1.class Solution {public: int uniquePathsWithObstacles(vector > &obstacleGrid) { int m=obstacleGrid.size(); int n=obstacleGrid[0].size(); v原创 2014-07-26 02:24:57 · 386 阅读 · 0 评论 -
[Leetcode]Unique Path
//DP每一个class Solution {public: int uniquePaths(int m, int n) { vectorf(n,0); f[0]=1; for(int i=0;i for(int j=1;j f[j]=f[j-1]+f[j]; return原创 2014-07-26 02:24:21 · 511 阅读 · 0 评论 -
[Leetcode]Combination Sum II
Leetcode Combination Sum II,略有疑惑求讨论原创 2014-07-26 06:23:13 · 686 阅读 · 0 评论 -
[Leetcode]Divide Two Integers
//位运算的好例子,记作翻译,以后来看看class Solution {public: int divide(int dividend, int divisor) { long long a = abs(double(dividend)); long long b = abs(double(divisor)); long lo翻译 2014-10-09 01:01:36 · 301 阅读 · 0 评论 -
[leetcode]Sum Root to Leaf Numbers
//树的遍历。切记,传值时候需要&/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL)原创 2014-10-08 22:26:44 · 242 阅读 · 0 评论 -
[Leetcode]4Sum
//zhe xuyao class Solution {public: vector > fourSum(vector &num, int target) { sort(num.begin(),num.end()); vector> res; for(int i=0;i { if(i原创 2014-10-08 11:51:21 · 331 阅读 · 0 评论