leetcode
lrj3763292194
这个作者很懒,什么都没留下…
展开
-
leetcode-Word Search(2014.2.18)
递归回溯,采用深度优先搜索的方法:class Solution {public: bool flag=false; bool visited[250][250]; bool exist(vector> &board, string word) { if(word.length()==0) return true; int原创 2014-04-13 22:01:26 · 405 阅读 · 0 评论 -
leetcode-Binary Tree Inorder Traversal(2014.1.23)
递归方法:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2014-04-20 19:35:41 · 414 阅读 · 0 评论 -
leetcode-3Sum Closest (2014.4.10)
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly原创 2014-04-19 23:12:19 · 489 阅读 · 0 评论 -
leetcode-Add Binary(2014.3.3)
字符串的相关操作class Solution {public: string addBinary(string a, string b) { int na=a.size(); int nb=b.size(); int overflow=0; string sum(na>nb?na:nb,'0');原创 2014-04-19 23:11:39 · 526 阅读 · 0 评论 -
leetcode-Best Time to Buy and Sell Stock II(2014.2.17)
即是求整个波浪线的上升过程,解法略显繁琐,当可以直接采用判断,若数组中元素后一个比前一个大,则不断累加的方法。class Solution {public: int maxProfit(vector &prices) { int low; int high; int maxPro=0; int flag=1;原创 2014-04-19 23:05:57 · 539 阅读 · 0 评论 -
LBSN 预测的想法(2013.11.11)
1.预测并不是对未来做工作,本质上来讲是对过去的分析。对过去的东西进行数据挖据和分析工作2.现在存在的问题,A.时间框架的选取。B.模型的选取,即是影响因素的确定。3.与董羿讨论,没必要全套LDA 的模型,一部分通过其处理即可以。4.用户的筛选问题。5.想法:当和时间维度有极大的关系。我所指的时间维度是指用户行为在时间上的重复性。6.是否可以将用户时间框架进行区分。根据时间状态原创 2014-04-19 23:20:07 · 720 阅读 · 0 评论 -
Spiral Matrix II(2014.3.2)
想来是有递归解法的,对递归的体会尚且不够。class Solution {public: vector > generateMatrix(int n) { vector> vec; if(n==0) return vec; vector vec1; for(int i=0;i vec1.pus原创 2014-04-19 23:16:28 · 530 阅读 · 0 评论 -
Single Number(2014.2.7)
class Solution {public: int singleNumber(int A[], int n) { int x=A[0]; for(int i=1;i x^=A[i]; } return x; }};原创 2014-04-19 23:17:16 · 489 阅读 · 0 评论 -
leetcode-3Sum (2014.4.7)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must b原创 2014-04-19 23:12:58 · 523 阅读 · 0 评论 -
leetcode-Convert Sorted Array to Binary Search Tree(2014.4.20)
Convert Sorted Array to Binary Search Tree Total Accepted: 10637 Total Submissions: 33426My SubmissionsGiven an array where elements are sorted in ascending order, convert it to a height balan原创 2014-04-20 21:08:45 · 623 阅读 · 0 评论 -
Linked List Cycle(2014.2.7)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(原创 2014-04-19 23:19:21 · 498 阅读 · 0 评论 -
leetcode-Jump Game(2014.2.23)
需要两个计数,lastJump记载当前能跳最远处,maxJump记录实际能跳最远距离。class Solution {public: bool canJump(int A[], int n) { int lastJump=0; int maxJump=0; int i=0; while(maxJump原创 2014-04-15 20:04:24 · 499 阅读 · 0 评论 -
leetcode-Best Time to Buy and Sell Stock(2014.2.17)
O(n)时间即可以完成class Solution {public: int maxProfit(vector &prices) { if(prices.size() int maxPro=0; int low=prices[0]; for(int i=0;i int m=price原创 2014-04-19 23:07:28 · 425 阅读 · 0 评论 -
leetcode-Add Two Numbers (2014.2.27)
细节很多,用了相当长的时间。代码长度很长,不够简洁。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */cla原创 2014-04-19 23:10:45 · 502 阅读 · 0 评论 -
leetcode OJ -Binary Tree Preorder Traversal(2014.1.20)
递归:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2014-04-19 23:13:45 · 501 阅读 · 0 评论 -
leetcode-gas station(2014.2.21)
暴力的方法需要O(n*n)的时间复杂度,即挨个使每个加油站作为起点,进行尝试。改进的方法:先要对整体进行判断,即加油站油总量大于需要消耗的油的总量,则一定可以。每当累积的油量不足以消耗,则当从该站的下一站作为起点,也就是说,这样的站点,尽量放在后边。遍历一次即可。class Solution {public: int canCompleteCircuit(vector原创 2014-04-13 22:00:34 · 594 阅读 · 0 评论 -
leetcode-Triangle (2014.3.12)
递归的方法超时,思想是以当前的,加上以此节点分出的两个节点中的较小的一个,需要改成动态规划来计算。递归的思想:一个出口,一个循环条件,考虑的时候要假设后续的都已经实现。类似于斐波那契的转换。尝试和LCS比较和《编程之美》中的GPS最短路径导航进行比较的话,动态规划是先从底部开始计算,则是从目标地点开始计算。可以将所有的最短路径计算出来,最后只有一个出发点,返回该出发点的最小值就可以原创 2014-04-13 22:03:08 · 418 阅读 · 0 评论 -
leetcode-Search Insert Position (2014.3.10)
class Solution {public: int searchInsert(int A[], int n, int target) { if(n==0) return 0; if(target if(target>A[n-1]) return n; for(int i=0;i if原创 2014-04-13 22:13:45 · 594 阅读 · 0 评论 -
leetcode-two sum(2014.4.3)
该问题和编程之美中的相似问题有所区别,编程之美中是要返回具体的两个值,而该题是要返回两个标志。所以不能先行排序将之处理为有序序列。尝试采用map将数值和标志位进行关联。出现问题:向量中的值有可能是重复的!这也是做哈希要考虑的一个问题,有冲突。参考的网上的方法,用结构体来建立数组值和下标的关系:struct Node{ int num, pos;};bool原创 2014-04-13 21:59:39 · 518 阅读 · 0 评论 -
leetcode-Best Time to Buy and Sell Stock III(2014.2.17)
整个调试过程花了好久,还是在参考别人答案的情况下。好吧,牙疼,昨夜没怎么睡觉,状态不佳。 采用动态规划的方法,时间复杂度为O(n)就搞定了,动态规划的核心似乎是备忘录。不过和诗辉讨论认为这个似乎并不是动态规划。class Solution {public: int maxProfit(vector &prices) { if(prices.原创 2014-04-19 23:05:08 · 578 阅读 · 0 评论 -
Linked List Cycle II(2014.2.7)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *dete原创 2014-04-19 23:18:43 · 518 阅读 · 0 评论 -
leetcode OJ -Binary Tree Postorder Traversal(2014.1.20)
递归方法:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2014-04-19 23:14:24 · 576 阅读 · 0 评论 -
leetcode-Balanced Binary Tree(2014.1.27)
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla原创 2014-04-19 23:08:11 · 480 阅读 · 0 评论 -
leetcode-Generate Parentheses (2014.4.18)
Generate Parentheses Total Accepted: 10265 Total Submissions: 33530 My SubmissionsGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example原创 2014-04-19 23:08:59 · 502 阅读 · 0 评论 -
leetcode-Best Time to Buy and Sell Stock(2014.2.16)
用两层遍历的方法超时:class Solution {public: int maxProfit(vector &prices) { int maxPro=0; for(int i=0;i for(int j=i+1;j int m=prices[j]-prices[i];原创 2014-04-19 23:06:46 · 530 阅读 · 0 评论 -
leetcode-Binary Tree Inorder Traversal(2014.1.23)
递归方法:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2014-04-19 23:04:26 · 485 阅读 · 0 评论 -
leetcode-Longest Consecutive Sequence(2014.3.31)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3原创 2014-04-15 19:57:57 · 423 阅读 · 0 评论 -
leetcode-Minimum Depth of Binary Tree(2014.1.22)
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla原创 2014-04-15 19:55:48 · 427 阅读 · 0 评论 -
leetcode-Length of Last Word(2014.3.31)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defin原创 2014-04-15 19:58:41 · 376 阅读 · 0 评论 -
leetcode-Maximum Depth of Binary Tree(2014.1.21)
递归方法:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2014-04-15 19:57:19 · 404 阅读 · 0 评论 -
leetcode-Rotate List (2014.3.10)
单链表的操作,先连接起来,再进行移动。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public原创 2014-04-13 22:16:03 · 521 阅读 · 0 评论 -
leetcode-Same Tree(2014.1.23)
判断两个树是否相同/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }原创 2014-04-13 22:14:26 · 516 阅读 · 0 评论 -
leetcode-Search a 2D Matrix(2014.2.23)
class Solution {public: bool searchMatrix(vector > &matrix, int target) { int m=matrix.size(); int n=matrix[0].size(); if(targetmatrix[m-1][n-1]) return false;原创 2014-04-13 22:11:35 · 525 阅读 · 0 评论 -
leetcode-Spiral Matrix(2014.3.1)
这道题脱离数组下标的考虑,以x轴,y轴进行考虑,更易进行理解和处理。class Solution {public: vector spiralOrder(vector > &matrix) { vector ans; if(matrix.size()==0||matrix[0].size()==0) return ans;原创 2014-04-13 22:07:27 · 571 阅读 · 0 评论 -
leetcode-Sqrt(x) (2014.4.7)
Implement int sqrt(int x).Compute and return the square root of x.注意二分搜索的写法。class Solution {public: int sqrt(int x) { long long low=0; long long high=x/2+1;原创 2014-04-13 22:06:35 · 549 阅读 · 0 评论 -
leetcode-Symmetric Tree(2014.1.27)
递归做法:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2014-04-13 22:05:07 · 470 阅读 · 0 评论 -
leetcode-Trapping Rain Water (2014.3.10)
每一个节点的存水量为左右最大高度较小值和本节点的差值min(maxLeftHeight[i],maxRightHeight[i])-A[i]。用空间换时间,若每个节点计算会超时,用两个数组记录每个节点的左右最高值。另外,vector不初始化会报错。class Solution {public: int trap(int A[], int n) { // Start原创 2014-04-13 22:04:00 · 435 阅读 · 0 评论 -
leetcode-Unique Binary Search Trees(2014.1.31)
catlan数:递归的方法:class Solution {public: int numTrees(int n) { if(n==1||n==0) return 1; int num=0; for(int i=1;i num+=numTrees(i-1)*numTrees(n-i);原创 2014-04-13 22:02:06 · 473 阅读 · 0 评论 -
leetcode-Sort List(2014.2.8)
采用归并排序:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListN原创 2014-04-13 22:08:21 · 544 阅读 · 0 评论 -
leetcode-Jump Game II (2014.3.3)
参考的网上的答案,似乎和jump game有很大的不同。还没有弄明白,mark。class Solution {public: int jump(int A[], int n) { int lastJump=0; int maxJump=0; int num=0; int i=0; fo原创 2014-04-15 20:03:39 · 624 阅读 · 0 评论