LeetCode
LeetCode题解
焦下鹿
中国科学技术大学地球物理毕业,现已转码于加拿大求学中
展开
-
LeetCode(27), 5. 最长回文子串
题目link解法1暴力循环,对每一个子字符串,判断是否是回文。记录回文子字符串的最大长度时间复杂度 O(N^3)空间复杂度 O(N)"""1. 暴力解法 对每一个子字符串,判断是否是回文。 记录回文子字符串的最大长度 时间复杂度 O(N^3) 空间复杂度 O(N)"""class Solution1: def longestPalindrome(self, s: str) -> str: size = len(s)原创 2020-07-14 03:20:19 · 113 阅读 · 0 评论 -
LeetCode(26), LRU Cache,Least Recently Used Cache
LRU CacheDescriptionDesign and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwis原创 2020-05-11 11:45:58 · 178 阅读 · 0 评论 -
LeetCode(25), Jewels and Stones
DescriptionYou’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 man...原创 2020-05-04 07:33:57 · 93 阅读 · 0 评论 -
LeetCode(24), Ransom Note
DescriptionGiven 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-04 07:26:52 · 97 阅读 · 0 评论 -
LeetCode(23), Bitwise AND of Numbers Range
DescriptionGiven a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.Example 1:Input: [5,7]Output: 4Example 2:Input: [0,1]Ou...原创 2020-05-02 08:01:38 · 87 阅读 · 0 评论 -
LeetCode(22), Subarray Sum Equals K
DescriptionGiven an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.Example 1:Input:nums = [1,1,1], k = 2Output: 2Note:The l...原创 2020-05-02 07:43:11 · 80 阅读 · 0 评论 -
LeetCode(21), Leftmost Column with at Least a One
Description(This problem is an interactive problem.)A binary matrix means that all elements are 0 or 1. For each individual row of the matrix, this row is sorted in non-decreasing order.Given a row...原创 2020-05-02 04:46:31 · 114 阅读 · 0 评论 -
LeetCode(20), Construct Binary Search Tree From Preorder Traversal
DescriptionReturn the root node of a binary search tree that matches the given preordertraversal.(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left ...原创 2020-05-02 04:08:18 · 126 阅读 · 0 评论 -
LeetCode(19), Search in Rotated Sorted Array
DescriptionSuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).You are given a target value to search....原创 2020-05-02 00:04:00 · 74 阅读 · 0 评论 -
LeetCode(18), Minimum Path Sum
DescriptionGiven a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or...原创 2020-05-01 10:08:22 · 72 阅读 · 0 评论 -
LeetCode(17), Number of Islands
DescriptionGiven a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Yo...原创 2020-05-01 07:08:23 · 79 阅读 · 0 评论 -
LeetCode(16), Valid Parenthesis String
DescriptionGiven a string containing only three types of characters: ‘(’, ‘)’ and ‘*’, write a function to check whether this string is valid. We define the validity of a string by these rules:Any ...原创 2020-04-27 23:22:56 · 95 阅读 · 0 评论 -
LeetCode(15), Product of Array Except Self
DescriptionGiven an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Example:Input: [1,2,3,4]...原创 2020-04-27 11:57:38 · 71 阅读 · 0 评论 -
LeetCode(14), Perform String Shifts
DescriptionYou are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [direction, amount]:direction can be 0 (for left shift) or 1 (for right shift).amount ...原创 2020-04-27 10:34:47 · 121 阅读 · 0 评论 -
LeetCode(13), Contiguous Array
DescriptionGiven 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 subarra...原创 2020-04-27 06:00:17 · 89 阅读 · 0 评论 -
LeetCode(12), Last Stone Weight
DescriptionWe have a collection of stones, each stone has a positive integer weight.Each turn, we choose the two heaviest stones and smash them together. Suppose the stones have weights x and y wit...原创 2020-04-27 05:20:48 · 98 阅读 · 0 评论 -
LeetCode(11), Diameter of Binary Tree
DescriptionGiven a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path ...原创 2020-04-27 05:18:45 · 97 阅读 · 0 评论 -
LeetCode(10), Min Stack
DescriptionDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top...原创 2020-04-27 05:16:09 · 77 阅读 · 0 评论 -
LeetCode(9), Longest Substring with At Least K Repeating Characters
DescriptionFind the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.Example 1:Input:s = "aaabb"...原创 2020-04-27 05:13:26 · 73 阅读 · 0 评论 -
LeetCode(8), Middle of the Linked List
DescriptionGiven a non-empty, singly linked list with head node head, return a middle node of linked list.If there are two middle nodes, return the second middle node.Example 1:Input: [1,2,3,4,5]...原创 2020-04-26 23:58:49 · 424 阅读 · 0 评论 -
LeetCode(7), Group Anagrams
Group AnagramsDescriptionGiven an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat","tan"], ["bat"]]...原创 2020-04-26 23:51:36 · 102 阅读 · 0 评论 -
LeetCode(6), Backspace String Compared
DescriptionGiven two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.Note that after backspacing an empty text, the text will con...原创 2020-04-26 23:43:34 · 107 阅读 · 0 评论 -
LeetCode(5), Move Zeroes, 移动数组中的0
Move ZeroesDescriptionGiven an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12]Output: [1,3,...原创 2020-04-06 10:11:37 · 77 阅读 · 0 评论 -
LeetCode(4), Best Time to Buy and Sell Stock II, 买卖股票的最佳时机II
Best Time to Buy and Sell Stock IIDescriptionSay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete ...原创 2020-04-06 10:10:24 · 129 阅读 · 0 评论 -
LeetCode(1), Maximum Subarray, 最大子序数组
文章目录Maximum SubarrayDescriptionSolution1. Kadane's algorithms takes O(n) time.2. Divide and ConquerMaximum SubarrayDescriptionGiven an integer array nums, find the contiguous subarray (containing a...原创 2020-04-04 09:44:30 · 95 阅读 · 0 评论 -
LeetCode(2), Single Number, 只出现一次的数字
文章目录Single NumberDescriptionSolution1. HashMap2. XORSingle NumberDescriptionGiven a non-empty array of integers, every element appears twice except for one. Find that single one.Note:Your algorit...原创 2020-04-04 09:50:06 · 82 阅读 · 0 评论 -
LeetCode(3), Happy Number, 快乐数
文章目录Happy NumberDescriptionSolutionHappy NumberDescriptionWrite an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any posi...原创 2020-04-04 09:59:18 · 74 阅读 · 0 评论