
LeetCode
码,码不停蹄
JuyongJiang
Follow your own pace. To be the best.
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
81. Search in Rotated Sorted Array II
#加入了重复元素,会导致,无法准确的判定 mid 元素到底位于左、右哪个区域,简单的方法就是不改变顺序的去重,原来的方法即可生效Search in Rotated Sorted Array class Solution: def search(self, nums: List[int], target: int) -> int: nums = list(set(n...原创 2020-03-04 21:13:57 · 122 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
class Solution: def search(self, nums: List[int], target: int) -> int: low = 0 high = len(nums)-1 while low <= high: mid = (low+high) // 2 i...原创 2020-03-04 19:56:22 · 112 阅读 · 0 评论 -
20. Valid Parentheses
#利用字典存放匹配规则 class Solution: def isValid(self, s: str) -> bool: if s == "": return True if len(s) == 1: #只有一个字符时 return False stack = [] ...原创 2020-03-04 18:13:56 · 107 阅读 · 0 评论 -
84. Largest Rectangle in Histogram
#关键就是找到一个元素左、右边第一个比它小的元素,然后计算 宽度 × 自身高度 class Solution: def largestRectangleArea(self, heights: List[int]) -> int: ans = 0 stack = [] heights = [0] + heights + [0] #便于...原创 2020-03-04 17:23:27 · 98 阅读 · 0 评论 -
61. Rotate List
#翻转k次 等效于 从count-k那个结点开始循环遍历一遍,在k % count ==0 时,序列不变 # Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None class Solution...原创 2020-03-03 03:35:10 · 130 阅读 · 0 评论 -
74. Search a 2D Matrix
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: #二分查找在矩阵中的应用 if matrix == [] or matrix == [[]]: return False row = le...原创 2020-03-02 20:32:51 · 123 阅读 · 0 评论 -
58. Length of Last Word
class Solution: def lengthOfLastWord(self, s: str) -> int: temp=s.split(" ") for word in temp[-1::-1]: if word != "": return len(word) retu...原创 2020-03-02 18:44:48 · 113 阅读 · 0 评论 -
905. Sort Array By Parity
#利用快速排序的思想,序列两边同时进行比较,时间复杂度为:O(n) class Solution: def sortArrayByParity(self, A: List[int]) -> List[int]: i = 0 j = len(A)-1 L_num = A[j] #取出最后一个值 while i < ...原创 2020-03-02 13:01:15 · 238 阅读 · 0 评论 -
50. Pow(x, n)
import math class Solution: def myPow(self, x: float, n: int) -> float: return math.pow(x,n) class Solution: def myPow(self, x: float, n: int) -> float: res = 1 ...原创 2020-02-29 05:33:17 · 158 阅读 · 0 评论 -
32. Longest Valid Parentheses
class Solution: def longestValidParentheses(self, s: str) -> int: stack = [0] ret = 0 for char in s: if (char == "("): stack.append(0) ...原创 2020-02-29 04:52:55 · 120 阅读 · 0 评论 -
串的模式匹配 : KMP算法
当存在 [1] 这样的现象,[2] 这样的做法,是否合理以及正确,或者有 [1] 这样的现象,为什么就可以有 [2] 这样的做法 ? 算法思想 [1] 当主串的第i个字符与子串的第j个字符失配时,若子串的前(k-1)个字符和子串的后(k-1)个字符匹配,‘p1……pk-1’=‘pj-k+1……pj-1’,则只需主串 S 的第 i 个字符与子串 P 的第 k 个字符开始向后比较即可,i 不必回溯。 ...原创 2020-02-28 19:40:22 · 466 阅读 · 0 评论 -
457. Circular Array Loop
class Solution: def circularArrayLoop(self, nums: List[int]) -> bool: nums_len = len(nums) for i in range(nums_len): nindex = sindex = i temp =...原创 2020-02-27 21:21:01 · 182 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: longest_sub = 0 temp = [] point = 1 if len(s) <= 1: return len(s) temp...原创 2020-02-27 03:11:48 · 124 阅读 · 0 评论 -
数据结构基础总结笔记
整体框架 线性结构 线性表 非线性结构 应用原创 2020-02-27 00:47:04 · 551 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums = nums1 + nums2 #print(nums) nums.sort() #排序的时间为O(log(m+n))即可 ...原创 2020-02-26 03:46:03 · 137 阅读 · 0 评论 -
2. Add Two Numbers
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None #Use listnode to get a decimal number class GetNum: snode = None ...原创 2020-02-25 08:06:26 · 154 阅读 · 0 评论 -
1. Two Sum
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)-1): comp=target-nums[i] for j in range(i+1,len(nums)): ...原创 2020-02-23 00:48:46 · 129 阅读 · 0 评论