
LeetCode@Python版
文章平均质量分 65
LeetCode真题Python版解决方案
dby_freedom
Everythin has a good ending, if it's not, then it's not the end.
展开
-
LeetCode 621. Task Scheduler (任务调度)
原题Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task co...原创 2018-12-19 21:24:33 · 415 阅读 · 0 评论 -
LeetCode 647. Palindromic Substrings (回文子字符串)
原题Given a string, your task is to count how many palindromic substrings in this string.The substrings with different start indexes or end indexes are counted as different substrings even they consis...原创 2018-12-19 20:01:22 · 517 阅读 · 2 评论 -
LeetCode 581. Shortest Unsorted Continuous Subarray (最短的未排序连续子阵列)
原题Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.You need to fin...原创 2018-12-19 16:57:01 · 245 阅读 · 0 评论 -
LeetCode 617. Merge Two Binary Trees (合并两棵二叉树)
原题Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tr...原创 2018-12-19 16:12:10 · 381 阅读 · 0 评论 -
LeetCode 560. Subarray Sum Equals K (和为K的子数组)
原题Given 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 length of ...原创 2018-12-18 23:35:27 · 306 阅读 · 0 评论 -
LeetCode 406. Queue Reconstruction by Height (依据高度进行队列重构)
原题Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of th...原创 2018-12-18 21:50:37 · 329 阅读 · 0 评论 -
LeetCode437. Path Sum III (路径和III)
原题You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it mu...原创 2018-12-18 13:47:00 · 315 阅读 · 0 评论 -
LeetCode572. Subtree of Another Tree (另一个棵树的子树)
原题Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this n...原创 2018-12-18 11:00:30 · 382 阅读 · 0 评论 -
LeetCode 538. Convert BST to Greater Tree (将 BST 转换为Greater树)
原题Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.Exam...原创 2018-12-12 23:50:55 · 226 阅读 · 0 评论 -
LeetCode 494. Target Sum (目标和)
原题You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.Find out how m...原创 2018-12-12 15:37:48 · 507 阅读 · 0 评论 -
LeeCode 448. Find All Numbers Disappeared in an Array (查找数组中缺失的数字)
原题Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Cou...原创 2018-12-11 21:08:52 · 256 阅读 · 0 评论 -
LeetCode 438. Find All Anagrams in a String (查找字符串所有的变位词)
原题Given 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 lar...原创 2018-12-11 19:10:11 · 412 阅读 · 0 评论 -
LeetCode 416. Partition Equal Subset Sum (分割相等子集)
原题Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note:Each of the array ele...原创 2018-12-11 16:16:00 · 366 阅读 · 0 评论 -
LeetCode 338. Counting Bits (统计位数)
原题Given 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: 2Out...原创 2018-12-09 23:01:13 · 217 阅读 · 0 评论 -
LeetCode 394. Decode String (解码字符串)
原题Given an encoded string, return it’s decoded string.The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is g...原创 2018-12-09 22:51:42 · 674 阅读 · 0 评论 -
LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output: 6Explanation: [2,3] has ...原创 2018-12-08 13:38:35 · 333 阅读 · 0 评论 -
LeetCode 148. Sort List (排序列表)
Sort a linked list in O(n log n) time using constant space complexity.Example 1:Input: 4->2->1->3Output: 1->2->3->4Example 2:Input: -1->5->3->4->0Output: -1->0-...原创 2018-12-08 13:26:13 · 415 阅读 · 0 评论 -
LeetCode 142. Linked List Cycle II (链表环起点)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-in...原创 2018-12-08 11:35:01 · 364 阅读 · 0 评论 -
LeetCode 141. Linked List Cycle (链表环判断)
原题Given a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail ...原创 2018-12-08 11:07:12 · 325 阅读 · 0 评论 -
LeetCode 322. Coin Change (硬币找零)
原题You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of mon...原创 2018-12-06 20:21:20 · 1056 阅读 · 0 评论 -
LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (在具有冻结时间条件下买入和卖出股票的最佳时间)
原题Say 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 as many transactions as you like (ie, buy on...原创 2018-12-06 14:08:18 · 431 阅读 · 0 评论 -
LeetCode 139. Word Break (断词)
原题Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if scan be segmented into a space-separated sequence of one or more dictionary words.Note:The...原创 2018-12-06 11:08:29 · 470 阅读 · 0 评论 -
LeetCode 136. Single Number (独数)
原题Given 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 it without u...原创 2018-12-06 10:48:52 · 251 阅读 · 0 评论 -
LeetCode 121. Best Time to Buy and Sell Stock (买入和卖出股票的最佳时机)
原题Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock...原创 2018-12-05 23:38:16 · 394 阅读 · 0 评论 -
LeetCode 287. Find the Duplicate Number (寻找重复数)
原题Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number,...原创 2018-12-05 22:01:31 · 272 阅读 · 0 评论 -
LeetCode 279. Perfect Squares (完美平方数)
原题Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.Example 1:Input: n = 12Output: 3 Explanation: 12 = 4 + 4 + 4.Example...原创 2018-12-05 20:49:24 · 477 阅读 · 0 评论 -
LeetCode 240. Search a 2D Matrix II (二维矩阵搜索)
原题Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right.Integers in ea...原创 2018-12-05 08:57:26 · 517 阅读 · 0 评论 -
LeetCode 236. Lowest Common Ancestor of a Binary Tree (二叉树的最小公共祖先)
原题Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes ...原创 2018-12-04 20:01:35 · 386 阅读 · 0 评论 -
LeetCode 234. Palindrome Linked List (回文链表)
原题Given a singly linked list, determine if it is a palindrome.Example 1:Input: 1->2Output: falseExample 2:Input: 1->2->2->1Output: trueFollow up:Could you do it in O(n) time and...原创 2018-12-04 19:53:24 · 264 阅读 · 0 评论 -
LeetCode 221. Maximal Square (最大正方形)
原题Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.Example:Input: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Output: 4Reference An...原创 2018-12-03 22:57:50 · 954 阅读 · 0 评论 -
LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)
原题Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Note: A leaf is a node with no childr...原创 2018-12-03 11:33:44 · 375 阅读 · 0 评论 -
LeetCode 102. Binary Tree Level Order Traversalet (二叉树层遍历)
原题Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 ...原创 2018-12-03 11:09:45 · 210 阅读 · 0 评论 -
LeetCode 101. Symmetric Tree (对称树)
原题Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3B...原创 2018-12-03 11:02:07 · 251 阅读 · 0 评论 -
LeetCode 98. Validate Binary Search Tree (有效二叉搜索树)
原题Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node’s key.The...原创 2018-12-03 10:51:32 · 266 阅读 · 2 评论 -
LeetCode 96. Unique Binary Search Trees (独立二叉搜索树)
原题Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n?Example:Input: 3Output: 5Explanation:Given n = 3, there are a total of 5 unique BST's: 1 3...原创 2018-12-03 10:40:15 · 243 阅读 · 0 评论 -
LeetCode 208. Implement Trie (Prefix Tree) (实现Trie树)
原题Implement a trie with insert, search, and startsWith methods.Example:Trie trie = new Trie();trie.insert("apple");trie.search("apple"); // returns truetrie.search("app"); // returns fals...原创 2018-12-03 09:44:52 · 474 阅读 · 0 评论 -
LeetCode 207. Course Schedule (课程调度)
原题There are a total of n courses you have to take, labeled from 0 to n-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pa...原创 2018-12-02 22:06:49 · 387 阅读 · 0 评论 -
LeetCode 206. Reverse Linked List (反转链表)
原题Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or recursive...原创 2018-12-02 20:30:34 · 492 阅读 · 0 评论 -
LeetCode 200. Number of Islands (岛屿数量)
原题Given 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. You may ass...原创 2018-12-02 19:30:09 · 443 阅读 · 0 评论 -
LeetCode 198. House Robber (房屋抢劫)
原题You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent ho...原创 2018-12-02 16:32:59 · 336 阅读 · 0 评论