![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
weixin_45405128
stay hungry, stay foolish.
展开
-
Day 30 Leetcode: Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass S...原创 2020-04-30 22:16:31 · 179 阅读 · 0 评论 -
Leetcode 84 largest Rectangle Area
class Solution: def largestRectangleArea(self, heights: List[int]) -> int: stack = [(0, -1)]#dummy variable, (0.-1) o represens dummy initial height, -1 represents dummy intial positio...原创 2020-04-10 15:53:58 · 170 阅读 · 0 评论 -
Leetcode-Dynamically Programming-Edit Distance-hard
https://www.youtube.com/watch?v=MiqoA-yF-0Mclass Solution: def minDistance(self, word1: str, word2: str) -> int: if word1==word2: return 0 dp=[] n=len(w...原创 2020-04-10 13:52:26 · 129 阅读 · 0 评论 -
Leetcode-DP-Coing Change 2
class Solution: def change(self, amount: int, coins: List[int]) -> int: m=len(coins)+1 n=amount+1 dp=[[0]*n for i in range(m)] dp[0][0]=1 for i in range...原创 2020-04-07 18:38:00 · 103 阅读 · 0 评论 -
Leetcode_DP_Unique Jumps
class Solution: def __init__(self): self.memory = {} def uniquePaths(self, m: int, n: int) -> int: if (m,n) in self.memory: return self.memory[(m,n)] ...原创 2020-04-06 23:59:56 · 406 阅读 · 0 评论 -
Leetcode_Dynamic Programming_Jump Game
class Solution: def canJump(self, nums: List[int]) -> bool: n = len(nums) right_most = n - 1 for i in range(n - 2, -1, -1): if i + nums[i] >= right_mos...原创 2020-04-06 22:36:23 · 88 阅读 · 0 评论 -
34. Find First and Last Position of Element in Sorted Array.
class Solution: def searchRange(self, nums: List[int], target: int) -> List[int]: # if target>nums[-1] or target<nums[0]: # return [-1,-1] #ans=[] left...原创 2020-03-06 16:33:08 · 80 阅读 · 0 评论 -
25. Reverse Nodes in k-Group ListNode 链表各个节点顺序倒过来,再复原顺序
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def reverseKGroup(self, head: ListNode,...原创 2020-03-06 14:52:45 · 160 阅读 · 0 评论 -
36. Valid Sudoku
1. 暴力循环import numpy as npclass Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: for i in range(9): if len(list(set(board[i][:])))<9-board[i][:].co...原创 2020-03-06 13:04:04 · 104 阅读 · 0 评论 -
28. Implement strStr()
class Solution: def strStr(self, haystack: str, needle: str) -> int: for i in range(len(haystack)-len(needle)+1): if haystack[i:i+len(needle)]==needle: ...原创 2020-03-06 01:09:08 · 68 阅读 · 0 评论 -
24. Swap Nodes in Pairs
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def swapPairs(self, head: ListNode) -&g...原创 2020-03-06 00:46:42 · 74 阅读 · 0 评论 -
21. Merge Two Sorted Lists
#If both lists are non-empty, make sure a starts smaller, use its head as result, and merge the remainders behind it. Otherwise, i.e., if one or both are empty, return what's there.class Solution...原创 2020-03-04 01:22:16 · 75 阅读 · 0 评论 -
20. Valid Parentheses
方法一:class Solution: def isValid(self, s: str) -> bool: left=['(', '{', '[' ] right=[ ')','}',']'] if len(s)==0: return True if s[0] in right:...原创 2020-03-04 01:07:37 · 90 阅读 · 0 评论 -
19. Remove Nth Node From End of List 两个指针
方法一:先得到listnode的长度,然后再L-n+1处断开,接上下一个node# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: ...原创 2020-03-03 23:54:43 · 85 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number ##backtracking
class Solution: def letterCombinations(self, digits: str) -> List[str]: #先把字典写出来 d={'2':['a','b','c'], '3':['d','e','f'], '4':['g','h','i'], '...原创 2020-03-03 19:55:29 · 101 阅读 · 0 评论 -
16. 3Sum Closest- Medium - 排序之后用夹逼的方法 (求和,找最接近目标的值)
class Solution: def threeSumClosest(self, nums: List[int], target: int) -> int: if len(nums)<=2: return None if len(nums)==3: return sum(nums) ...原创 2020-02-22 20:57:10 · 139 阅读 · 0 评论 -
15. 3Sum
#暴力求解,超时class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: res=[] n=len(nums) for x in range(n-2): for y in range(x+1,n-1): ...原创 2020-02-22 19:37:24 · 69 阅读 · 0 评论 -
14. Longest Common Prefix
class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs)==0: return '' if len(strs)==1: return strs[0] s='' ...原创 2020-02-22 17:56:01 · 188 阅读 · 0 评论 -
11. Container With Most Water - Medium
#暴力求解'''class Solution: def maxArea(self, height: List[int]) -> int: maxvol=0 n=len(height) for i in range(n-1): for j in range(i+1,n): vo...原创 2020-02-22 16:44:17 · 89 阅读 · 0 评论 -
9. Palindrome Number - Easy
方法一:class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False if x<10: return True res=0 num=x ...原创 2020-02-22 02:05:55 · 84 阅读 · 0 评论 -
7. Reverse Integer
方法一:class Solution: def reverse(self, x: int) -> int: minus=False if x<0: x=-x minus=True li=[] while x>0: ele=x%...原创 2020-02-22 01:04:04 · 99 阅读 · 0 评论 -
6. ZigZag Conversion
class Solution: def convert(self, s: str, numRows: int) -> str: if numRows==1: return s if len(s)<=numRows: return s ans='' ...原创 2020-02-21 21:25:14 · 86 阅读 · 0 评论 -
5. 最长回文子串 Longest Palindromic Substring
(1) 暴力求解class Solution: def longestPalindrome(self, s: str) -> str: maxlen=0 result='' for i in range(len(s)): for j in range(i,len(s)+1):##注意首尾坐标 ...原创 2020-02-21 19:49:41 · 186 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
#import itertools as itclass Solution: def lengthOfLongestSubstring(self, s: str) -> int: mlen=0 n=len(s) for i in range(n): zd={} #字典比list运算快许多 ...原创 2020-02-15 15:21:57 · 71 阅读 · 0 评论 -
2. add-two-numbers
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def addTwoNumbers(self, l1: ListNode, l2...原创 2020-02-15 13:36:48 · 86 阅读 · 0 评论 -
Leetcode 4. Median of Two Sorted Arrays (寻找两个有序数组的中位数) Python
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums = nums1 + nums2 &...原创 2019-07-14 20:51:41 · 130 阅读 · 0 评论 -
Leetcode 3. Longest Substring Without Repeating Characters (无重复字符的最长字串)Python
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if not s: &nbs...原创 2019-07-14 20:29:15 · 95 阅读 · 0 评论 -
Leetcode 2. Add Two Numbers (两数相加)Python
class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: if l1 is None: &nb...原创 2019-07-14 17:34:18 · 148 阅读 · 0 评论 -
Leetcode 1. Two SUM(两数之和)Python
方法一:class Solution: def twoSum(self, nums: List[int], target:int) -> List[int]:n=len(nums)for i in range(0,n):for j in range(i+1,n):if target-nums[i]==nums[j]:return i,jbreakelse:continue...原创 2019-07-14 16:23:10 · 180 阅读 · 0 评论