- 博客(49)
- 收藏
- 关注
原创 [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
753
1
翻译 [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
412
原创 [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
503
原创 [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
547
原创 [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
648
原创 [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
645
原创 [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
334
原创 [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
353
原创 [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
405
原创 [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
372
原创 [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
333
原创 [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
321
原创 [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
376
原创 [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
314
原创 [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
333
转载 [Leetcode]Populating Next Right Pointers in Each Node
思路:很棒的一道题,可以认为是先序遍历。(1)根据题述:左孩子为空,则右孩子一定为空,所以左孩子为空,则return(2)如果左孩子不为空,则右孩子一定不为空,所以链接左孩子和右孩子即可(左孩子的next赋值为右孩子)(3)由于先序遍历,所以父节点的next比子节点的next先被设置,故父节点不同的两个子节点进行连接,就可以用到父节点的next,整题的精华便
2014-10-13 09:01:15
415
原创 [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
483
原创 [Leetcode]Spiral Matrix
//从Spiral MatriSpiral Matrix IIclass Solution {public: vector spiralOrder(vector > &matrix) { vector result; int m=matrix.size(); if(m==0)return result;
2014-10-13 07:37:16
382
原创 [Leetcode]Spiral Matrix II
//用Java模拟,写起来比较方便public class Solution { public int[][] generateMatrix(int n) { int [][] res= new int[n][n]; if(n //int res[5050][5050]; int total = n/2;
2014-10-13 07:01:20
460
原创 [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
390
原创 [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
465
翻译 [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
321
原创 [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
266
原创 [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
350
原创 [Leetcode]3Sum Closest
class Solution {public: int threeSumClosest(vector &num, int target) { sort(num.begin(),num.end()); int min=INT_MAX; int res; for(int i=0;i {
2014-10-08 10:40:48
333
原创 [Leetcode]String to Integer (atoi)
//要注意一个地方就是class Solution {public: int atoi(const char *str) { if(*str == '\0'||str== NULL)return 0; long long int res = 0; while(*str == ' ') str++;
2014-10-08 10:05:53
292
原创 [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
283
原创 [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
348
原创 [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
392
原创 [leetcode]Permutations
大家好像都在转载这篇...写的不错http://blog.youkuaiyun.com/tuantuanls/article/details/8717262
2014-09-30 08:35:44
356
原创 [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
299
原创 [Leetcode]Letter Combinations of a phone number
class Solution { const vector yang= { " ", "", "abc", "def", "ghi", "jkl", "mno",
2014-07-26 08:57:55
497
原创 [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
447
原创 [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
552
原创 [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
387
原创 [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
342
原创 [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
442
原创 [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
474
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人