随笔分类 - Leetcode
Leetcode: Add Two Numbers
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single ...
阅读全文
Leetcode: Word Ladder II
摘要:Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be ch...
阅读全文
Leetcode: Triangle
摘要:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the fol...
阅读全文
Leetcode: Best Time to Buy and Sell Stock II
摘要:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complet...
阅读全文
Leetcode: Best Time to Buy and Sell Stock
摘要:这道题求进行一次交易能得到的最大利润。如果用brute force的解法就是对每组交易都看一下利润,取其中最大的,总用有n*(n-1)/2个可能交易,所以复杂度是O(n^2)。跟Maximum Subarray非常类似,用“局部最优和全局最优解法”。思路是维护两个变量,一个是到目前为止最好的交易,另
阅读全文
Leetcode: Pascal's Triangle II
摘要: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 ...
阅读全文
Leetcode: 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...
阅读全文
Leetcode: Path Sum II
摘要:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and su
阅读全文
Leetcode: Convert Sorted Array to Binary Search Tree
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.基本上一次过,要注意边界条件的问题:如果在recursion里有两个参数int begin, end, 递...
阅读全文
Leetcode: Merge Sorted Array
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal...
阅读全文
Leetcode: Word Search
摘要:Remember to change the character back after loop through all the possibility 非常聪明的方法1:O(1)空间的话可以不用visited数组,直接改board。改了之后,再访问的时候因为board[i][j] != word.
阅读全文
Leetcode: Remove Duplicates from Sorted Array II
摘要:Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function shou
阅读全文
Leetcode: Subsets
摘要:Adopted approach: Notice that in line 11, you should create a copy of current path, and add it to res. Otherwise, it'll be edited later, and be wiped
阅读全文
Leetcode: Combinations
摘要:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4...
阅读全文
Leetcode: Climbing Stairs
摘要:这道题目是求跑楼梯的可行解法数量。每一步可以爬一格或者两个楼梯,可以发现,递推式是f(n)=f(n-1)+f(n-2),也就是等于前一格的可行数量加上前两格的可行数量。熟悉的朋友可能发现了,这个递归式正是斐波那契数列的定义. 这里base case 是f(1)=1, f(2)=2. Fibonacc
阅读全文
Leetcode: Set Matrix Zeroes
摘要:一次过,空间复杂度为O(m+n), 下一次做的时候寻找constant space solution。用boolean array也可以,用bit vector可能会更节省. 其实还可以再优化,我们考虑使用第一行和第一列来记录上面所说的行和列的置0情况,这里问题是那么第一行和第一列自己怎么办?想要记
阅读全文
Leetcode: Search a 2D Matrix
摘要:1 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: 2 3 Integers in each row are ...
阅读全文
Leetcode: Add Binary
摘要:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".我自己的做法:用StringBuffer, a和b都从末尾扫描到开头,而StringBu...
阅读全文
Leetcode: Trapping Rain Water
摘要:这道题做的比较艰辛,一开始自己想的是一个用stack的解法,感觉过于繁琐(出栈,入栈,计算容积),但未尝不是一个好的尝试,这个方法还是有点小问题,过后会好好想清楚。看了网上提示完成了最终的方法,这个方法两次遍历数组,第一次遍历找每个元素右边最大的元素,第二次遍历寻找每个元素左边最大的元素,同时计算该
阅读全文
浙公网安备 33010602011771号