![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python-leetcode
文章平均质量分 50
a1310368974
这个作者很懒,什么都没留下…
展开
-
leetcode 51 -N皇后
最近科研没头绪,写写leetcode缓缓,主要学python了,之前python只是看别人的代码,还没自己写过,果然不同N皇后也算经典题目了,写的时候倒也没那么顺利,没搞懂列表和普通变量复制的区别,也算加深印象了皇后1class Solution(object): def solveNQueens(self, n): Nqunens=[] def fd(m原创 2017-09-26 22:13:39 · 297 阅读 · 0 评论 -
76. Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC".Note:If the...原创 2018-02-17 16:21:36 · 134 阅读 · 0 评论 -
leetcode200---并查集实现
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume...原创 2018-02-26 17:13:36 · 614 阅读 · 1 评论 -
42. Trapping Rain Water
在北京写一题,好爽Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1原创 2018-01-25 22:49:23 · 137 阅读 · 0 评论 -
牛顿法
用牛顿法求平方根,早有耳闻class Solution(object):def mySqrt(self, x):def diedai(x0,a):return (x0*x0+a)/2/x0x0=a=float(x)if x==0:return 0while int(x0)!=int(diedai(x0,a)):x0=diedai(x0,a)r原创 2018-01-05 16:45:04 · 131 阅读 · 0 评论 -
24点游戏--python
leetcode 679 题虽然写的超时了,但是答案都是准确的,超市是因为用了递归以及把所有情况都遍历了,也不想改了思路就是构造后续表达式,先找出4个数的全排列,再构造表达式的全组合,然后构造后续表达式,这里应用了递归可以做的优化:很多情况都是重复的,以及三个数的结果是0或者负数也可以直接排除,但本身的复杂度也不是很高class Solution(o原创 2017-12-29 22:50:15 · 799 阅读 · 1 评论 -
leetcode 121 309
比较简单的俩个题,第一题O(n)的复杂度,第二题也想用O(n)的复杂度解决,刚开始出错了,直接用DP,毕竟最爱,虽然是O(n2)的复杂度,运行时间排名yefeichanSay you have an array for which the ith element is the price of a given stock on day i.If you were only pe原创 2017-12-18 12:10:23 · 150 阅读 · 0 评论 -
leetcode 322. Coin Change
写的相当曲折,思路很明确,用动态规划,从最低值到目标加上去,用一个amount的数组保存每个值的最小步数,但是声明数组不能太大,不能超过247*****,就那个数,这也是散列存储的一个问题,调试提交了很多次才AC,运行时间也不错,真的不容易322. Coin Changeclass Solution(object):def coinChange(self, coin原创 2017-12-20 12:26:27 · 127 阅读 · 0 评论 -
leetcode72. Edit Distance
和最长公共子序列非常像的一道题,解题方法完全一样,只是要的结果不同罢了分类分到动态规划,所以很好想了,如果事先不知道用动态规划,可能不太好想到这也是第一次用到二维列表的声明class Solution(object): def minDistance(self, word1, word2): l1, l2 = len(word1)+1, len(word2)+1原创 2017-09-26 22:27:33 · 156 阅读 · 0 评论 -
leetcode62. Unique Paths
这个题用组合数学的方法非常简单,我还见过更难一点的,但既然说了要用动态规划,就用动态规划吧path1class Solution(object): def uniquePaths(self, m, n): path=[] for i in range(0,m): l=[] l.append(1) path.append(l)原创 2017-09-26 22:25:51 · 169 阅读 · 0 评论 -
leetcode53. Maximum Subarray
记得在考研408的辅导书上见过这个算法设计题,非常巧妙,还好没忘,考完研都一年半了,还是蛮怀念西工大的同学们class Solution(object): def maxSubArray(self, nums): maxnum=nums[0] sum=0 for i in range(len(nums)): sum=sum+nums[i]原创 2017-09-26 22:22:02 · 126 阅读 · 0 评论 -
leetcode 39. Combination Sum40
https://leetcode.com/problems/combination-sum/description/简单的回溯应用,就是第二个算法写的时候没考虑重复情况,如果排好序之后按每个值的个数依次遍历也是可以的Combination Sum1class Solution(object): def combinationSum(self, candidates, ta原创 2017-09-26 22:17:29 · 138 阅读 · 0 评论 -
leetcode 123. Best Time to Buy and Sell Stock III
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 complete at most two transactions.Note:You may not engage...原创 2018-03-19 22:43:04 · 132 阅读 · 0 评论