Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路:最简单的方法就是依照[Leetcode]Pascal's Triangle 的方式自顶向下依次求解,但会造成空间的浪费.若仅仅用一个vect…
http://oj.leetcode.com/problems/pascals-triangle-ii/ 杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algorithm to use only O(k) extra space?其实计算当前行,也只用到前一行了.再前面的没有用. class Solution { public: vector<int> getRow(int rowIndex) { // IMPORTANT: Please reset a…
Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. public List<List<Integer>…
Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detecting the loop using faster/slower pointers. Finding the begining of the loop. After two pointers meet in step 1, keep one pointer and set anther to th…
http://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 一题要求得出所有树的种类数,二题要求得出所有树. 在一题的基础上修改代码,还是要提前想清楚再写. #include <iostream> #include <vector> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int…
http://oj.leetcode.com/problems/pascals-triangle/ 杨辉三角 先分析数据,找出规律 ans[row][col] = ans[row-1][col-1]+ans[row-1][col] #include <iostream> #include <vector> #include <string> using namespace std; class Solution { public: vector<vector<…
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 思路:杨辉三角,直接按规律生成即可 vector<vector<int> > generate(int numRows) { vector<vector<int>>…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? 这个题还是蛮考验数学推理的,不过在前一个题的基础上还是能推出结果的.这是英文一段解释,非常有帮助. First Step: A…
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. 相对于单纯的链表转置,这个问题需要把链表的一部分做反转.并要求就地反转而且只遍历一次.我的想法是吧链表看成3个部分:list1-&…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? 和前面那题类似,只不过这里要找到的是环开始的节点,看下面这张图(图是盗的): 当fast以及slow指针在z处相遇的时候,可以…