- 博客(8)
- 收藏
- 关注
原创 二分法
二分法基本模版class Solution: # @param nums: The integer array # @param target: Target number to find # @return the first position of target in nums, position start from 0 def binarySearch(self, nums, target): if len(nums) == 0:
2021-05-26 12:25:05
199
原创 BackTracking Problems
Permutation IIGiven a collection of numbers,nums,that might contain duplicates, returnall possible unique permutationsin any order.有重复数字: [1,2,1]sort, 在前一个相同数字被选之前,不能选当前数字,否则会出现 1pi, 1 == 1, 1pi的情况Subset IIGiven an integer arraynumsthat may...
2021-05-20 14:41:36
95
原创 Spiral Matrix I && II
simulation, (i, j) 来记录每一层的起始坐标坐标变换来traverse当前坐标最后一个环只有一行或者一列,可以跳出循环class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: # (i, j) is the start of traveral coordinates # (p, q) is the current coo
2021-05-10 09:56:29
79
原创 Subarray 问题
Subarray 问题常用思路1. 从第i个到第j个 subarray 之和 转换成 sum(0,,j) - sum(0,,i)使用典型题目:sum divide by k, sum equal to k,sum multiple of k2. maximum subarray sum / product单一subarray问题,可以思考当1 pass到第i个的时候和已知包含(i-1)个关系,同时维持一个全局变量来记录所有已知的最优解product稍微复杂一点,需要考虑0 reset .
2021-05-10 06:16:38
515
原创 Best Time to buy && sell stock
买卖一次1个变量记录目前已知最便宜,(当前 - 已知最便宜)= 当前最大收益买卖无数次https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii特殊值的处理,如果全部一样的话,其实可以直接initiate peak && valley = prices[0], 这样对于总体的profit是没有变化的~t1 < t2 < t3:buy t1, sell t2 && buy.
2021-05-09 07:19:21
105
原创 Pascal Triangle I && II
Easy manipulation of the triangleThe main idea is to maintian 2 arrays and keeps using the 1st array to store last row information and use it to update/edit the 2nd row results1. prev_array2. curr_array因为需要得知当前array的信息,其实只需要记录上一排array的信息不能只用一个arr
2021-05-04 00:07:18
89
原创 2 Sum / 3 Sum / 4 Sum
1.Two SumGiven an array of integersnumsand an integertarget, returnindices of the two numbers such that they add up totargetYou may assume that each input would haveexactlyone solution, and you may not use thesameelement twiceYou can return...
2021-05-03 13:30:07
170
1
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人