摘要:problem 141. Linked List Cycle code 这道题是快慢指针的经典应用。只需要设两个指针,一个每次走一步的慢指针和一个每次走两步的快指针,如果链表里有环的话,两个指针最终肯定会相遇。实在是太巧妙了,要是我肯定想不出来。代码如下: 1.Leetcode_Linked Lis
阅读全文
摘要:136. Single Number https://leetcode.com/problems/single-number/description/
阅读全文
摘要:problem 125. Valid Palindrome 参考 1. Leetcode_Valid Palindrome; 完
阅读全文
摘要:problem 122. Best Time to Buy and Sell Stock II 这道题由于可以无限次买入和卖出。我们都知道炒股想挣钱当然是低价买入高价抛出,那么这里我们只需要从第二天开始,如果当前价格比之前价格高,则把差值加入利润中,因为我们可以昨天买入,今日卖出,若明日价更高的话,
阅读全文
摘要:problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<int>& prices) { int res = 0; for(int i=0; i<prices.siz
阅读全文
摘要:problem 119. Pascal's Triangle II code class Solution { public: vector<int> getRow(int rowIndex) { vector<int> res(rowIndex+1, 1); if(rowIndex<2) retu
阅读全文
摘要:problem 118. Pascal's Triangle code 注意数组的下标,以及数组的size。根据题意,其实可以确定数组的大小。 参考 1. leetcode_Pascal's Triangle; 完
阅读全文
摘要:problem 112. Path Sum code 回归方法 参考 1.leetcode_Path Sum; 完
阅读全文
摘要:problem 111. Minimum Depth of Binary Tree code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *r
阅读全文
摘要:problem 110. Balanced Binary Tree code 回归方法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *righ
阅读全文
摘要:problem 108. Convert Sorted Array to Binary Search Tree code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left;
阅读全文
摘要:problem Binary Tree Level Order Traversal II 根据题意,需要将每层的节点分别放入数组中,因此需要每层节点的数量。 广度优先搜索算法(Breadth First Search),又叫宽度优先搜索,或横向优先搜索。从根节点开始,沿着树的宽度遍历树的节点。如果所
阅读全文
摘要:problem Maximum Depth of Binary Tree code 递归方法 参考 1.Maximum Depth of Binary Tree; 完
阅读全文
摘要:problem Symmetric Tree code 回归方法 参考 1.Symmetric Tree; 2.leetcode_discuss; 3. leetcode_solution; 完
阅读全文
摘要:problemSame Treecode 回归的方法 or just one line code 这道题还有非递归的解法,因为二叉树的四种遍历(层序,先序,中序,后序)均有各自的迭代和递归的写法,这里我们先来看先序的迭代写法,相当于同时遍历两个数,然后每个节点都进行比较. 参考1. Same Tre
阅读全文
摘要:problem Merge Sorted Array code 注意, 1.既然给出了结果数据的大小就要能够利用起来有效的数据。 2. 另一个思路是从后面开始比较。 参考 1. Merge Sorted Array; 完
阅读全文
摘要:problem Remove Duplicates from Sorted List 考察的是链表的基本用法。 code 参考 1. Remove Duplicates from Sorted List; 完
阅读全文
摘要:problem ClimbingStairs 题意: 爬台阶问题,每次可以爬1个或者两个台阶,问如果有n个台阶,可以有多少种方法爬到顶部? 解析: 对于n=1,有一种方法;n=2,有两种方法; 对于n>2,对于每一个台阶i到达顶端,最后一步都有两种方式,从i-1再爬一步,或者从i-2再爬2步。 即也
阅读全文
摘要:problem Sqrt(x) code 重点在于理解怎么计算平方根。 参考 1.leetcode_sqrt(x); 完
阅读全文
摘要:problem AddBinary code 注意 1.每个字符串中的每个字符对应的数字进行数字运算,且最后的结果是字符串; 2.int型和字符类型之间的转换; 3.string类型可以直接使用符号'+'进行字符串的连接; 4.进位和余数的运算; 参考 1.leetcode_AddBinary; 完
阅读全文