基础算法(leetcode)刻意练习
基础算法(leetcode)刻意练习
Mu__Cheng
这个作者很懒,什么都没留下…
展开
-
通配符匹配
Pythonclass Solution(object): def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ l_s = len(s) l_p= len(p) dp =...原创 2020-03-30 21:36:06 · 200 阅读 · 0 评论 -
加油站
Pythonclass Solution(object): def canCompleteCircuit(self, gas, cost): """ :type gas: List[int] :type cost: List[int] :rtype: int """ sum_gas=0 ...原创 2020-03-29 15:21:08 · 212 阅读 · 0 评论 -
跳跃游戏
Pythonclass Solution(object): def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ start = 0 end = 0 n = len(nums) w...原创 2020-03-28 15:53:26 · 199 阅读 · 0 评论 -
分发饼干
Pythonclass Solution(object): def findContentChildren(self, g, s): """ :type g: List[int] :type s: List[int] :rtype: int """ g.sort() s.so...原创 2020-03-27 21:48:07 · 134 阅读 · 0 评论 -
判断子序列
1.双指针双指针还是太慢了class Solution(object): def isSubsequence(self, s, t): """ :type s: str :type t: str :rtype: bool """ i=0 j=0 while i...原创 2020-03-26 21:18:10 · 148 阅读 · 0 评论 -
买卖股票的最佳时机 II
Pythonclass Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ ans = 0 for i in range(1, len(prices)): ...原创 2020-03-25 21:17:07 · 113 阅读 · 0 评论 -
恢复二叉搜索树
Python# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: de...原创 2020-03-24 21:35:08 · 115 阅读 · 0 评论 -
不同的二叉搜索树 II
Python# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object...原创 2020-03-23 21:50:56 · 152 阅读 · 0 评论 -
二叉树的中序遍历
Python# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(objec...原创 2020-03-22 20:34:06 · 145 阅读 · 0 评论 -
二叉树的最大深度
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def maxD...原创 2020-03-21 11:20:56 · 99 阅读 · 0 评论 -
对称二叉树
Python# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object)...原创 2020-03-20 19:06:19 · 129 阅读 · 0 评论 -
相同的树
Python# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object)...原创 2020-03-19 18:58:02 · 116 阅读 · 0 评论 -
正则表达式匹配
Pythonclass Solution(object): def isMatch(self, s, p): len_s = len(s) len_p = len(p) res_m = [[False for j in range(len_p + 1)] for i in range(len_s + 1)] for i in...原创 2020-03-18 20:24:30 · 122 阅读 · 0 评论 -
最长回文子串
Python(中心扩展算法)class Solution: def longestPalindrome(self,s): if s is None and len(s) < 1:return 0 start = end = 0 for i in range(len(s)-1): len1 = self.around(s,i,i) len2 = self.aroun...原创 2020-03-17 19:44:41 · 117 阅读 · 0 评论 -
无重复字符的最长子串
Pythonclass Solution: def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ st = {} i, ans = 0, 0 for j in range(len(s)):...原创 2020-03-16 12:25:53 · 100 阅读 · 0 评论 -
有效的括号
Pythonclass Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ stack = [] left = ['(', '{', '['] right = [')', '}',...原创 2020-03-15 18:56:55 · 111 阅读 · 0 评论 -
最长公共前缀
Pythonclass Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ strs = list(set(strs)) if len(strs) &g...原创 2020-03-14 19:48:11 · 110 阅读 · 0 评论 -
罗马数字转整数
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适...原创 2020-03-13 20:13:23 · 169 阅读 · 0 评论 -
合并K个排序链表
Python# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def mergeKLists(self, ...原创 2020-03-12 12:09:42 · 111 阅读 · 0 评论 -
删除链表的倒数第N个节点
Python# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def removeNthFromEnd(s...原创 2020-03-11 15:45:26 · 147 阅读 · 0 评论 -
两数相加
Pythonclass Solution(object): def addTwoNumbers(self, l1, l2): l = ListNode(0) l_copy = l carry = 0 while l1 or l2: l1_val = l1.val if l1 else 0 ...原创 2020-03-10 19:42:33 · 207 阅读 · 0 评论 -
环形链表
Pythonclass Solution(object): def hasCycle(self, head): if(head == None or head.next == None): return False slow = head fast = head.next while(slow !=...原创 2020-03-09 11:59:30 · 110 阅读 · 0 评论 -
删除排序链表中的重复元素
Pythonclass Solution(object): def deleteDuplicates(self, head): if not (head and head.next): return head i,j = head,head while j: if i.val!=j.val: i = i.next i.val = j.val j =...原创 2020-03-08 12:19:41 · 97 阅读 · 0 评论 -
合并两个有序链表
class Solution(object): def mergeTwoLists(self, l1, l2): if l1 is None: return l2 elif l2 is None: return l1 elif l1.val < l2.val: l...原创 2020-03-07 21:32:56 · 87 阅读 · 0 评论 -
买卖股票的最佳时机 III
Pythonclass Solution: def maxProfit(self, prices): if prices==[]:return 0 length=len(prices) b1= [0 for x in prices ] s1 = prices[0] b1[0]= 0 for ...原创 2020-03-06 19:34:11 · 145 阅读 · 0 评论 -
最接近的三数之和
Python(双指针)class Solution(object): def threeSumClosest(self, nums, target): nums.sort() length=len(nums) flag=float("inf") for i in range(length): if i...原创 2020-03-05 19:28:58 · 122 阅读 · 0 评论 -
三数之和
Pythonclass Solution: def threeSum(self, nums): nums.sort() count = [] length = len(nums) for i in range(length): if i > 0 and nums[i] == nums[i-1]:...原创 2020-03-04 19:12:59 · 133 阅读 · 0 评论 -
移除元素
python:比较简单的直接暴力法class Solution(object): def removeElement(self, nums, val): for i in range(len(nums)-1, -1, -1): if nums[i] == val: nums.pop(i) return le...原创 2020-03-03 18:44:33 · 111 阅读 · 0 评论 -
删除排序数组中的重复项
Pythonclass Solution(object): def removeDuplicates(self, nums): length=len(nums) count=0 for i in range(1,length): if nums[count]!=nums[i]: nu...原创 2020-03-02 14:53:37 · 143 阅读 · 0 评论 -
两数之和
class Solution(object): def twoSum(self, nums, target): num_dict = {} for i, num in enumerate(nums): num_dict[i] = num for j in num_dict: if num_dict[j] == (target - num) and j != i: return [j, i]解题思...原创 2020-03-01 15:03:09 · 278 阅读 · 0 评论