- 博客(45)
- 收藏
- 关注
原创 LeetCode 常见--二分搜索--问题,Python实现
二分查找0. 模板判断左闭右开区间 = [left, mid, right)def binary_search(left, right): while left < right: mid = left + (right - l) // 2 if isResult(mid): return mid if judge(mid): right = mid # next range = [l, m)
2020-06-26 21:36:00 344
原创 LeetCode 常见数组-矩阵问题(python实现)
本文主要包括了LeetCode中常见的数组、矩阵问题的Python解法。问题列表来自于Github CS-Notes/Leetcode 题解。
2020-06-08 19:29:32 614
原创 LeetCode 常见字符串问题 (python实现)
字符串1. 判断两个字符串是否是:同字母异序词LeetCode 242. Valid Anagram (Easy)使用列表实现的哈希表存储s串字符出现的次数,遍历t串字符,对哈希表中的字符进行减法操作,如果出现负数,那么字符数量不匹配,返回False,如果遍历完成不返回False,那么字符串是匹配的,返回True。class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t
2020-06-05 00:38:28 609
原创 LeetCode 常见哈希表问题 (python实现)
哈希表从哈希表中取出数据时间复杂度为O(1)O(1)O(1). 哈希表的存储空间复杂度为O(n)O(n)O(n).对于不同的编程语言,其内置哈希表结构不同,实现速度也不同。1. 求数组中两个数的和为给定值LeetCode 1. Two Sum (Easy)首先创建一个空字典。遍历整个数组,每次遍历的同时计算当前item值与target的差值是否已经存在在字典中。如果存在,那么当前遍历值与字典中的插值就是所求结果。直接返回即可。如果不存在,那么将当前值和索引存储在字典中,以便下次计算时候进行查
2020-06-05 00:33:58 242
原创 LeetCode 常见栈和队列问题 (python实现)
栈和队列1. 用栈实现队列LeetCode 232. Implement Queue using Stacks (Easy)栈和队列是两种顺序完全相反的数据结构。队列先进先出(First In First Out),栈先进后出(First In Last Out)。可以通过使用两个栈,数据在两个栈之间来回“倒腾”,实现队列的功能。class MyQueue: def __init__(self): """ Initialize your data struc
2020-06-05 00:30:47 388
原创 LeetCode 常见二叉树问题 (python实现)
递归树是一种常用递归操作的数据结构。一棵树要么是空,要么有两个指针,指向另外两棵树。1. 树的高度Leet Code 104. Maximun Depth of Binary Tree (Easy)# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.
2020-06-03 05:27:45 845
原创 LeetCode 常见链表问题 (python实现)
链表链表问题很多可以采用递归进行操作。递归理清思路的时候,主要考虑问题最简单的情况。当只有一个节点或两个节点的时候,问题是如何解决的。理清递归退出的条件,可以更加清晰代码的作用。1. 找出两个链表的交点 LeetCode 160 Easy问题的核心思想是:两个链表被分成三个部分,a、b、c. 如果他们有共同的交点那么一定有公式a+c+b=b+c+aa+c+b = b+c+aa+c+b=b+c+a,因此当a链表循环到末尾时,跳转到b链表上。那么他们一定会同时到达同一个节点上去。如果不存在共同链c,
2020-06-03 00:16:11 528
原创 LeetCode 31 May, Edit Distance,字符串最少修改次数
Edit DistanceGiven two words word1 and word2, find the minimum number of operations required to convert word1 to word2.You have the following 3 operations permitted on a word:Insert a characterDelete a characterReplace a characterExample 1:Input:
2020-05-31 20:19:19 208
原创 LeetCode 30 May, K Closest Points to Origin, 查找距原点最近的K个点
K Closest Points to OriginWe have a list of points on the plane. Find the K closest points to the origin (0, 0).(Here, the distance between two points on a plane is the Euclidean distance.)You may return the answer in any order. The answer is guaranteed
2020-05-31 20:17:00 116
原创 LeetCode 29 May, Course Schedule,判断是否选课冲突
Course ScheduleThere are a total of numCourses courses you have to take, labeled from 0 to numCourses-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]Given the total
2020-05-31 20:15:31 372
原创 LeetCode 28 May, Counting Bits, 统计数二进制形式1的个数
Possible BipartitionGiven a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.Example 1:Input: 2Output: [0,1,1]Example 2:Input: 5Outp
2020-05-28 17:10:36 120
原创 LeetCode 27 May, Possible Bipartition, 图的可能分割
Possible BipartitionGiven a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of any size.Each person may dislike some other people, and they should not go into the same group.Formally, if dislikes[i] = [a, b], it
2020-05-28 05:08:21 187
原创 LeetCode 26 May, Contiguous Array, 连续数组
Contiguous ArrayGiven a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.Example 1:Input: [0,1]Output: 2Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.Example 2:Input:
2020-05-28 05:06:06 94
原创 LeetCode 25 May, Uncrossed Lines,不交叉连线,动态规划
Uncrossed LinesWe write the integers of A and B (in the order they are given) on two separate horizontal lines.Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that:A[i] == B[j];The line we draw does not int
2020-05-28 05:02:54 2172
原创 LeetCode 24 May, Construct BinarySearch Tree from Preorder Traversal, 前序序列构建平衡二叉树
Construct Binary Search Tree from Preorder TraversalReturn the root node of a binary search tree that matches the given preorder traversal.(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value &l
2020-05-28 05:00:05 156
原创 Linux 文件备份 rsync 命令
rsync命令可以将文件追踪备份,下次备份的时候只修改变动的部分,避免文件大量的重复拷贝删除。# 增量追踪文件,每次只备份改动的文件,已经删除的文件备份不会删除$ rsync -av ~/DATA /Volumes/Disk/BACKUP# 每次备份修改状态发生改变的文件,删除的文件也会同时删除$ rsync -av --delete ~/DATA /Volumes/Disk/BACKUP# 备份位置为远程服务器$ rsync -av --delete -e ssh ~/DATA roo.
2020-05-25 00:14:27 119
原创 LeetCode 21 May, Count Square Submatrices with All Ones, 统计全1正方形个数
Count Square Submatrices with All OnesGiven a m * n matrix of ones and zeros, return how many square submatrices have all ones.Note:You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Example 1:Input: matrix =[ [0,1,1,1], [1,1,1,1],
2020-05-23 18:59:48 159
原创 LeetCode 22 May, Sort Characters By Frequency, 按字符出现频率排序字符串
Sort Characters By FrequencyGiven a string, sort it in decreasing order based on the frequency of characters.Example 1:Input:"tree"Output:"eert"Explanation:'e' appears twice while 'r' and 't' both appear once.So 'e' must appear before both 'r' a
2020-05-23 18:57:22 108
原创 LeetCode 23 May, Interval List Intersections, 重叠区间判断
Interval List IntersectionsGiven two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.Return the intersection of these two interval lists.(Formally, a closed interval [a, b] (with a <= b) denotes the set of r
2020-05-23 18:55:28 122
原创 LeetCode 20 May, Kth Smallest Element in a BST, 寻找二叉搜索树的第k小的值
Kth Smallest Element in a BSTGiven a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Example 1:Input: root = [3,1,4,null,2], k = 1 3 / \
2020-05-20 22:06:09 87
原创 LeetCode 19 May, Online Stock Span,股票持续最长天数
Online Stock SpanWrite a class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock’s price for the current day.The span of the stock’s price today is defined as the maximum number of consecutive days (startin
2020-05-20 22:03:45 213
原创 LeetCode 18 May, Permutation in String, 判断是否含有异序字符串
题目连接分析:此题跟之前的一题是一样的,均是采用滑动窗口思想和哈希表的应用。这里不做过多描述,可以参考之前的博文LeetCode 17 May。# 方法一# Runtime: 108ms# Memory Usage: 15MBclass Solution: def checkInclusion(self, s1: str, s2: str) -> bool: if len(s1) == 0 or len(s2) == 0: return False .
2020-05-18 16:18:58 90
原创 LeetCode 17 May, Find All Anagrams in a String, 寻找同字母异序的子串
Find All Anagrams in a StringGiven a string s and a non-empty string p, find all the start indices of p’s anagrams in s.Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.The order of
2020-05-18 00:23:42 135
原创 LeetCode 16 May, Odd Even Linked List, 奇偶位置链表拆分合并
Odd Even Linked ListGiven a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.Note:The relative order inside both the even and odd groups sh
2020-05-18 00:21:41 170
原创 LeetCode 15 May, Maximum Su Circular Subarray, 求循环数组和最大子序列
Maximum Sum Circular SubarrayGiven a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C.Here, a circular array means the end of the array connects to the beginning of the array. (Formally, C[i] = A[i
2020-05-16 03:28:39 142
原创 LeetCode 53, Maximum Subarray, 求数组和最大的子序列
Maximum SubarrayGiven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example 1:Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4],Output: 6Explanation: [4, -1, 2, 1] has the larges
2020-05-16 03:23:29 138
原创 LeetCode 14 May, Implement Trie (Prefix Tree), 构建字典树、前缀树
Implement Trie (Prefix Tree)Implement a trie with insert,search,and startsWith methods.Note:You may assume that all inputs are consist of lowercase letters a to z.All inputs are guaranteed to be non-empty strings.Example 1:Trie trie = new Trie()t
2020-05-14 20:38:46 131
原创 LeetCode 13 May, Remove K Digits, 删除k个字符求最小数
Remove K DigitsGiven a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.Note:The length of num is less than 10002 and will be >= k.The give num does not contain any le
2020-05-14 06:18:33 434
原创 LeetCode 12 May, Single Element in a Sorted Array, 找出独数
Single Element in a Sorted ArrayYou are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. Find this single element that appears only once.Example 1:Input: [1,
2020-05-14 06:16:42 96
原创 LeetCode 11 May, Flood Fill, 像素区域填充
Flood FillAn image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newCo
2020-05-14 06:12:31 621
原创 LeetCode 10 May, Find the Town Judge, 找出法官
Find the Town JudgeIn a town, there are N people labbelled from 1 to N. There is a rumor that one of these people is secretly the town judge.If the town judge exists, then:The town judge trusts nobody.Everybody (except for the town judge) trusts the t
2020-05-14 06:07:50 132
原创 LeetCode 9 May, Valid Perfect Square, 完全平方数判断
Valid Perfect SquareGiven a positive integer num write a function which returns True if num is a perfect square else False.Note: Do not use any built-in library function such as sqrt.Example 1:Input: 16Output: TrueExample 2:Input: 14Output: Fals
2020-05-09 18:06:38 135
原创 LeetCode 8 May, Check If It is Straight Line, 散点共线判断
Check If It is a Straight LineYou are given an array coordinates, coordinates[i] = [x, y] where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.Example 1:Input: coordinates = [[1, 2], [2, 3], [3
2020-05-08 19:15:34 264
原创 LeetCode 7 May, Cousins in Binary Tree, 判断二叉树的堂兄弟节点
Cousins in Binary TreeIn a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.Two nodes of a binary tree are cousins if they have the same depth, but have d...
2020-05-07 18:59:23 218
原创 LeetCode 6 May Majority Element 寻找众数
Majority ElementGiven an array of size n , find the majority element. The majority element is the element that appears more than 「n / 2」 times.You may assume that the array is non-empty and the majo...
2020-05-06 19:11:22 142
原创 LeetCode 5 May First Unique Character in a String 字符串中首次单独出现的字符
First Unique Character in a StringGiven a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.Example 1:s = “leetcode”return 0.s = “lovelee...
2020-05-05 18:37:53 114
原创 LeetCode 4 May Number Complement 正数取反
Number ComplementGiven a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Example 1:**Input: ** 5**Output: ** 2**Explanati...
2020-05-04 23:26:01 160
原创 LeetCode 3 May Ransom Note 赎金条、杂志、查找
Ransom NoteGiven an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the ma...
2020-05-03 21:50:58 140
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人