LeetCode
文章平均质量分 55
Mi4o
这个作者很懒,什么都没留下…
展开
-
LeetCode 121. Best Time to Buy and Sell Stock
买卖股票的最佳时间只能进行一次买入卖出操作,所以要找到整个区间内的最大差值暴力搜索时间复杂度是O(n^2),使用一个标记值来判定是否满足条件,时间复杂度可以优化到O(n)package DP.No121;class Solution { public int maxProfit(int[] prices) { int maxPro = 0; int...原创 2019-02-09 17:54:26 · 227 阅读 · 0 评论 -
LeetCode 122. Best Time to Buy and Sell Stock II
可以进行多次买卖,要保证每一次都获得了小范围内的最大利润。找到所有的连续上升区间,在区间内进行最低价购入,最高价抛售的transaction即可。package DP.No122;class Solution { public static int maxProfit(int[] prices) { int maxPro = 0; if(price...原创 2019-02-09 17:35:38 · 179 阅读 · 0 评论 -
LeetCode 78. Subsets
如何实现ArrayList的拷贝:使用ArrayList.addAll()方法将一个list的内容全部复制过来使用不断扩充的方式从[]空数组生成新的数组(1)首先会生成[1],此时加上原来的有[]和[1](2)然后会生成[2],[1,2],此时加上原来的有[]和[1],[2],[1,2](3)然后会生成[3],[1,3],[2,3],[1,2,3],此时加上原来的有[]和[1],[2],...原创 2019-01-13 22:03:41 · 140 阅读 · 0 评论 -
LeetCode 344. Reverse String
class Solution { public String reverseString(String s) { StringBuffer sb = new StringBuffer(); for(int i=s.length()-1; i >= 0; i--) sb.append(s.charAt(i)); ...原创 2019-01-13 21:40:46 · 114 阅读 · 0 评论 -
LeetCode 611. Valid Triangle Number
如果任意两边之和大于第三边,那么是个合法的三角形所以联想到判断合法IP地址那道题,既然是固定选择3个数,那么可以Brute Force排序之后将所有的情况判定出来package array.No611;import java.util.Arrays;class Solution { public static int triangleNumber(int[] nums) {...原创 2019-01-13 21:01:34 · 155 阅读 · 0 评论 -
LeetCode 88. Merge Sorted Array
这道题的解法比较巧妙最开始的想法是从前往后开始合并数组,不过发现这样需要不断往后移动数组,时间开销比较大标准题解是从两个数组的最后一个元素开始比较,将大的放在第一个数组后面空闲的位置[为什么?因为合并之后的数组的大小是确定的,并且两个数组已经是排序好的]。边界条件:在第一个数组中所有元素都放到正确位置之后,第二个数组还有元素没有放置到第一个数组从末尾开始合并的思路很巧妙,这个和Longe...原创 2019-01-13 17:32:30 · 176 阅读 · 0 评论 -
LeetCode 236. Lowest Common Ancestor of a Binary Tree
这道题和上一道求解最低公共祖先的不同在于是一般的二叉树,只能遍历节点来获取信息通过返回节点是否为空判定子树中是否有pq节点三种情况p和q分别在两颗子树中:那么当前节点就是最低公共祖先p和q都在左子树:将问题转换为在当前节点的左子树找p和q的最低公共祖先p和q都在右子树:将问题转换为在当前节点的右子树找p和q的最低公共祖先package tree.No236;class ...原创 2019-01-12 19:38:04 · 193 阅读 · 0 评论 -
LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
因为是二叉搜索树,所以可以根据值的大小判定是属于根节点的左子树还是右子树。如果都是在左子树部分,那么最低公共祖先一定属于当前节点的左子树;如果都是在右子树部分,那么最低公共祖先一定属于当前节点的右子树;否则当前节点一定是最低公共祖先(为什么?)因为这种情况下无论假设最低公共祖先是属于左子树还是右子树,最低公共祖先一定不是属于另外一棵树的节点的祖先,假设证伪。package tree.N...原创 2019-01-12 19:13:37 · 136 阅读 · 0 评论 -
LeetCode 905. Sort Array By Parity
class Solution { public int[] sortArrayByParity(int[] A) { int[] res = new int[A.length]; int index = 0; for(int i=0; i < A.length; i++){ if(A[i]%2 == 0) ...原创 2019-01-12 18:47:43 · 141 阅读 · 0 评论 -
LeetCode 160. Intersection of Two Linked Lists
最开始想到的O(n^2)只管解法:只需要固定一条链表,移动另一个指针,判定指针是否属于固定链表的后继节点即可package linkedlist.No160;class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; ...原创 2019-01-12 18:21:14 · 149 阅读 · 0 评论 -
LeetCode 199. Binary Tree Right Side View
求解二叉树的右视图其实就是每一层最右边的节点,那么如果能够得到每一层所有节点的有序集合,就很容易得到结果那么首先联想到了做过的二叉树按层次遍历,得到层次遍历集合之后,取每一层的最后一个元素package tree.No199;import java.util.ArrayList;import java.util.List;class TreeNode { i...原创 2019-01-12 11:01:18 · 157 阅读 · 0 评论 -
【LeetCode题解】 70. Climbing Stairs(Java)
文章目录(一)70. Climbing Stairs(一)70. Climbing StairsYou are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you cl...原创 2018-10-11 01:23:57 · 195 阅读 · 0 评论 -
【LeetCode题解】89. Gray Code (Java)
文章目录(一)89. Gray Code(一)89. Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits ...原创 2018-10-12 23:23:09 · 337 阅读 · 0 评论 -
【LeetCode题解】72. Edit Distance (Java)
文章目录(一)72. Edit Distance(一)72. Edit DistanceGiven two words word1 and word2, find the minimum number of operations required to convert word1 to word2.You have the following 3 operations permitted o...原创 2018-10-12 22:20:42 · 227 阅读 · 0 评论 -
【LeetCode题解】64. Minimum Path Sum(Java)
文章目录(一) Minimum Path Sum(一) Minimum Path SumGiven a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note...原创 2018-10-12 21:27:55 · 251 阅读 · 0 评论 -
LeetCode 494. Target Sum
#pragma once#include &lt;vector&gt;#include &lt;string&gt;using namespace std;class Solution {private: int res = 0;public: int findTargetSumWays(vector&lt;int&gt;&amp; nums, int S) { recurs原创 2019-03-07 23:17:24 · 184 阅读 · 0 评论