leetcode
hlllllllll
这个作者很懒,什么都没留下…
展开
-
leetcode:59. Spiral Matrix II
ProblemGiven a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.Example:Input: 3Output:[[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ]]Solution解法类似于旋转取值,只...原创 2019-12-05 10:29:27 · 109 阅读 · 0 评论 -
leetcode:57. Insert Interval
ProblemGiven a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times....原创 2019-12-03 09:48:40 · 107 阅读 · 0 评论 -
leetcode:56. Merge Intervals
ProblemGiven a collection of intervals, merge all overlapping intervals.Example 1:Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] ove...原创 2019-12-01 21:34:08 · 135 阅读 · 0 评论 -
leetcode:55. Jump Game
ProblemGiven an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine...原创 2019-11-29 17:40:06 · 117 阅读 · 0 评论 -
leetcode-54. Spiral Matrix
problemGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]...原创 2019-11-28 15:19:32 · 140 阅读 · 0 评论 -
leetcode - 49.Group Anagrams
遍历法遍历列表中的字符串,并将字符串排序,排序结果一致的字符串储存在一个字典中class Solution: def groupAnagrams(self, strs): res = dict() for s in sorted(strs): key = tuple(sorted(s)) res[key]...原创 2019-07-11 10:46:39 · 82 阅读 · 0 评论 -
leetcode - 48.Rotate Image
python式解法利用zip函数和*,首先将matrix反向,再利用zip压缩即可,*表示选择列表中的子列表,*listclass Solution: def rotate(self, matrix): """ Do not return anything, modify matrix in-place instead. """ ...原创 2019-07-11 10:42:32 · 112 阅读 · 0 评论 -
leetcode - 47.Permutations II
无重全排列递归解法class Solution: def permute(self, nums): res= [] self.recur([],nums,res) return res def recur(self,head,tail,res): if len(tail)<=1: re...原创 2019-07-11 10:40:53 · 85 阅读 · 0 评论 -
leetcode - 45.Jump Game II
贪婪算法定义step为步数,定义cur_pos和max_pos为当前能达到的最大位置和下一步能达到的最大位置,定义i表示两次最大位置中间隔的位置从第0步开始,计算每步能达到的最大位置,一直到达到终点位置,如果下一步的最大位置和本次最大位置一样,说明无法再前进class Solution: def jump(self, nums): max_pos,cur_pos =...原创 2019-07-11 10:38:06 · 90 阅读 · 0 评论 -
leetcode - 44.Wildcard Matching
DP算法定义状态dp(lengh=len(s)+1))表示s中的每个字符是否被p中的前几个子字符所匹配dp[0]用来表示p是否是以开头状态转移:若p中下一个字符为‘’,则newdp[i]=newdp[i-1] or dp[i] 0<i<n若p中下一个字符为’?’,则newdp = dp 0<i<n若p中下一个字符为其他字符,则newdp[i] = dp[i...原创 2019-07-11 10:36:33 · 142 阅读 · 0 评论 -
leetcode - 43.Multiply Strings
模拟乘法手算运算过程class Solution: def multiply(num1, num2): product = [0] * (len(num1) + len(num2)) pos = len(product)-1 for n1 in reversed(num1): tempPos = pos ...原创 2019-07-11 10:31:45 · 93 阅读 · 0 评论 -
leetcode - 42.Trapping Rain Water
简单解法从列表左边开始搜索,定义left=0,如果右边的数小于左边,则固定左边向右搜索,直到遇到右边某个数大于左边的数,或大于左边后的一个数,否则left+1class Solution: def trap(self, height): output = 0 left = 0 while left<len(height)-1...原创 2019-07-11 10:29:22 · 158 阅读 · 0 评论 -
leetcode - 41. First Missing Positive
简单解法class Solution: def firstMissingPositive(self, nums): n = len(nums) if n == 0: return 1 for i in range(1,n+2): if len([j for j in nums if j!=i...原创 2019-07-11 10:26:49 · 81 阅读 · 0 评论 -
leetcode - 40.Combination Sum II
DFS递归算法同上一题class Solution: def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: candidates.sort() res = [] self.dfs(candidates,target,0,[...原创 2019-07-11 10:25:26 · 100 阅读 · 0 评论 -
leetcode - 39.Combination Sum
递归解法一class Solution: def combinationSum(self, candidates, target): candidates.sort() ans = [] ##定义一个数字来存放结果 def helper(sums,path):##sums:前面选择的数字的和 ...原创 2019-07-11 10:18:59 · 99 阅读 · 0 评论 -
leetcode - 38. Count and Say
简单解法列表遍历,分别记录字符和字符的个数,最后叠加在一起class Solution: def countAndSay(self ,n): count = 1 say = '1' while count<n: count+=1 newsay = '' num...原创 2019-07-11 10:12:25 · 90 阅读 · 0 评论 -
leetcode - 33. search in rotated sorted array
简单算法直接利用python的特性class Solution: def search(self, nums, target): srch = [ind for ind, val in enumerate(nums) if val == target] if len(srch) == 0: return -1 ...原创 2019-07-11 09:53:50 · 93 阅读 · 0 评论 -
leetcode - 32.Longest valid parentheses
动态规划法新建一个与字符串相同长度的全0数组dp,数组表示字符串当前为匹配到的最大valid字符串长度class Solution1: def longestValidParentheses(self, s: str) -> int: n = 0 dp = [0 for _ in range(len(s))] sr = [stri...原创 2019-07-11 09:42:58 · 110 阅读 · 0 评论