![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
大象漫步
这个作者很懒,什么都没留下…
展开
-
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 · 308 阅读 · 0 评论 -
LeetCode常见--贪心算法--问题 Python实现
本文包括了LeetCode中常见的贪心算法的题目,Python实现。原创 2020-06-26 21:34:51 · 593 阅读 · 0 评论 -
LeetCode 常见--双指针--问题,Python和C++实现
本文包括了LeetCode中常见的利用双指针解决的问题的Python实现。原创 2020-06-26 21:31:18 · 213 阅读 · 0 评论 -
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 · 185 阅读 · 0 评论 -
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 · 103 阅读 · 0 评论 -
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 · 342 阅读 · 0 评论 -
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 · 102 阅读 · 0 评论 -
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 · 149 阅读 · 0 评论 -
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 · 78 阅读 · 0 评论 -
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 · 2099 阅读 · 0 评论 -
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 · 136 阅读 · 0 评论 -
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 · 138 阅读 · 0 评论 -
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 · 93 阅读 · 0 评论 -
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 · 104 阅读 · 0 评论 -
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 · 74 阅读 · 0 评论 -
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 · 178 阅读 · 0 评论 -
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 · 76 阅读 · 0 评论 -
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 · 115 阅读 · 0 评论 -
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 · 152 阅读 · 0 评论 -
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 · 121 阅读 · 0 评论 -
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 · 121 阅读 · 0 评论 -
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 · 115 阅读 · 0 评论 -
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 · 390 阅读 · 0 评论 -
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 · 75 阅读 · 0 评论 -
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 · 524 阅读 · 0 评论 -
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 · 117 阅读 · 0 评论 -
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 · 122 阅读 · 0 评论 -
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 · 239 阅读 · 0 评论 -
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 · 182 阅读 · 0 评论 -
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 · 114 阅读 · 0 评论 -
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 · 97 阅读 · 0 评论 -
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 · 142 阅读 · 0 评论 -
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 · 120 阅读 · 0 评论 -
LeetCode 2 May Jewels and Stones 宝石和石头判断
Jewels and StonesYou’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how...原创 2020-05-03 01:04:01 · 109 阅读 · 0 评论 -
LeetCode 1 May First Bad Version 二分查找、折半查找
First Bad VersionYou are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is dev...原创 2020-05-02 06:22:19 · 132 阅读 · 0 评论 -
LeetCode 1-2 Happy Number 判断快乐数
Happy NumberWrite an algorithm to determine if a number n is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum o...原创 2020-05-01 00:21:11 · 337 阅读 · 0 评论 -
LeetCode 1-1 Single Number 查找单独数
Single NumberGiven a non-empty array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement...原创 2020-04-30 00:43:35 · 297 阅读 · 1 评论