自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(24)
  • 收藏
  • 关注

原创 力扣29——加油站

class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: n=len(gas) for i in range(n): if gas[i]<cost[i]: continue ...

2020-03-29 11:32:38 119

原创 力扣18——跳跃游戏

class Solution: def canJump(self, nums: List[int]) -> bool: n=len(nums)-2 m=len(nums)-1 while n>=0: if nums[n]+n>=m: m=n n-...

2020-03-28 12:38:33 149

原创 力扣27——分发饼干

先对量列表排序,然后设置双指针,状态转移为sj>=gi,则i,j加1并人数加1,否则i+1,不加人数。边界加0: class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: i=j=0 g.sort() s.sort() ...

2020-03-27 12:44:30 118

原创 力扣26——判断子序列

子问题是s每往后移动一个单位,是否还是t的子序列: class Solution: def isSubsequence(self, s: str, t: str) -> bool: def d(i:int,s:str,t:str): if i==len(s): return True if...

2020-03-26 17:44:31 123

原创 力扣第21天——二叉树最大深度

小小用一下dp思想,定义dp函数含义为目标节点处的最大深度,base case为叶子节点返回1,其他节点返回左右子节点最大函数值: class Solution: def maxDepth(self, root: TreeNode) -> int: if root==None: return 0 t=root i...

2020-03-21 08:24:15 78

原创 力扣第20天——对称二叉树

递归,递归函数定义是判断两节点是否构成镜像: class Solution: def isSymmetric(self, root: TreeNode) -> bool: p=root q=root def Symmetric(t1,t2): if t1==None and t2==None: ...

2020-03-20 08:56:21 66

原创 力扣第18天——相同的树

递归,四种情况: 1.两节点都为空,True 2.只有一个为空,False 3.val不等,False 4.val相等,判断左右子节点 class Solution: def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: if p==None and q==None: return Tr...

2020-03-19 20:04:55 78

原创 力扣第18天——正则表达式

看了大佬思路的暴力递归,还有待优化: class Solution: def isMatch(self, s: str, p: str) -> bool: def ism(s,p): if p=='': if s=='': return True ...

2020-03-18 12:37:08 95

原创 力扣第17天——最长回文子串

参考大佬的思路,对暴力遍历做优化: class Solution: def longestPalindrome(self, s: str) -> str: l=len(s) if l==1 or l==0: return s max_len=1 start=0 dp=[[False...

2020-03-17 12:05:49 60

原创 力扣第16天——无重复字符的最长子串

来个菜鸟的做法: class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if s=='': return 0 dp=[1]*len(s) for i in range(1,len(s)): j=i-1 ...

2020-03-16 12:02:14 56

原创 力扣第15天——有效的括号

class Solution: def isValid(self, s: str) -> bool: a=[0] for i in s: if i=='[' or i=='(' or i=='{': a.append(i) if i==')': ...

2020-03-15 09:58:50 68

原创 力扣第14天——最长公共前缀

选取第一个元素一次和后续元素比较,如find方法返回值部位0,则去除尾项,直至返回0: class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if strs==[]: return "" a=strs[0] for i in ...

2020-03-14 12:30:14 93

原创 力扣第13天——罗马数字转整数

考虑三种特殊情况即可: class Solution: def romanToInt(self, s: str) -> int: tap=dict([('I',1),('V',5),('X',10),('L',50),('C',100),('D',500),('M',1000)]) sum=0 for i in range(len(s)...

2020-03-13 12:28:43 152

原创 力扣第12天——合并k个排序链表

暴力法 class Solution: def mergeKLists(self, lists: List[ListNode]) -> ListNode: m=[] head=point=ListNode(0) for i in lists: while i: m.append(i...

2020-03-12 14:59:07 94

原创 力扣第11天——删除链表第N个节点

两遍遍历: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeNthFromEnd(self, head: ListNo...

2020-03-11 09:10:02 126

原创 力扣第10天——两数相加

class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: def get_nums(l:ListNode): if not l: return 0 return l.val+get_nu...

2020-03-10 18:33:45 90

原创 力扣第九天——环形链表

# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def hasCycle(self, head: ListNode) -> bool...

2020-03-09 09:56:49 92

原创 力扣第8天——删除链表中重复元素

此题设置双指针遍历链表,当前指针指向None叫停即可: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def deleteDuplica...

2020-03-08 11:36:05 148

原创 力扣第七天——合并两个有序链表

两个输入链表都是有序的,因此先创建一个val维-1的链表头部,然后根据l1和l2的大小关系上下横跳即可: class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: prehead=ListNode(-1) pr=prehead whi...

2020-03-07 14:17:15 164

原创 力扣第五天——最接近三数之和

class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ nums = sorted(nums) ...

2020-03-05 21:14:04 84

原创 力扣第四天——three sum

本题如果直接遍历,无法满足题目不重复的要求。因此采用排序加双指针的方法。 排序的目的在于方便确定指针移动方向,若遇到前后相同的数则跳过,避免重复: class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: res=[] n=len(nums) nums...

2020-03-04 14:31:57 100

原创 力扣第三天——移除元素

原地修改类的题目一般是采用双指针,本题慢指针用来定位被替换项,快指针用来查找替换项。 class Solution: def removeElement(self, nums: List[int], val: int) -> int: if len(nums)==0: return 0 i=j=0 if nums...

2020-03-03 10:47:52 76

原创 力扣第二天——删除排序数组中的重复项

此题采用双指针i,j,如果前值等于后值,则让后指针向后找到不同的值。如果后指针溢出,则退出循环 class Solution: def removeDuplicates(self, nums: List[int]) -> int: i=0 j=1 if len(nums)==0: return 0 ...

2020-03-01 19:01:10 68

原创 力扣第一天——两数之和

本人是初学小白,two sum也没three sum复杂,就不引入哈希,直接遍历: class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: n=len(nums) for i in range(0,n-1): for x in ra...

2020-03-01 11:20:55 152

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除