leetcode
文章平均质量分 53
weixin_41838371
这个作者很懒,什么都没留下…
展开
-
592. Fraction Addition and Subtraction
题目592.Fraction Addition and SubtractionGiven a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final res...原创 2019-06-05 20:19:50 · 86 阅读 · 0 评论 -
88. 合并两个有序数组
题目描述给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。说明:初始化 nums1 和 nums2 的元素数量分别为 m 和 n。你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。示例:输入:nums1 = [1,2,3,0,0,0], m = 3nums2 ...原创 2019-05-10 11:50:46 · 107 阅读 · 0 评论 -
34. Search for a Range
34.Search for a Range题目描述在给定的升序排好的数组中,寻找只含target的子区间。方法一:提前分割的分治法创建子方法search,参数是nums的搜索区间的两端索引值:1. 返回值是一个索引区间[lo, hi],该区间内所有的值都是target;2. 如果target在区间之外,返回[-1,-1];3. target在区间之内,但并不是区间中所有值...原创 2018-04-20 14:55:16 · 77 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
33.Search in Rotated Sorted Array题目描述在旋转的已经排好序的数组中寻找目标值target分析方法一:特殊情况,数组为空返回-1while循环寻找target,二分法:二分搜索法的关键在于获得了中间数后,判断下面要搜索左半段还是右半段,我们可以观察出规律, 如果中间的数小于最右边的数,则右半段是有序的, 若中间数大于最右边数,则左半...原创 2018-04-20 11:17:27 · 61 阅读 · 0 评论 -
1. Two Sum
题目描述Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the sam...原创 2018-04-14 10:43:57 · 64 阅读 · 0 评论 -
437. Path Sum III
437.Path Sum 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 th...原创 2019-06-06 11:20:53 · 78 阅读 · 0 评论 -
22. Generate Parentheses
22.Generate Parentheses题目描述生成给定数n对圆括号的所有可能组成的字符串,并保存在一个列表中返回。方法一:递归-使用两个整型变量记录当前生成的结果中还能继续添加的左右括号数,left和right,初始化为 n; 初始时只能添加左括号‘(’;只有在left<right的情况下,才能添加右括号; 因为之前已经有了限制条件left<right,所...原创 2019-06-06 11:45:20 · 69 阅读 · 0 评论 -
21. Merge Two Sorted Lists
21. Merge Two Sorted Lists题目描述合并两个已经排序的链表,并将它们按照从小到大的顺序合并。首先,对特殊情况直接处理: 1. 两个链表有其中一个为空时,返回非空的链表头; if not l1 or not l2: #选择一个非空链表,返回 return l1 or l2 2.两个链表都为空时,返回空值得注意的是,循环体中链表指针的...原创 2018-04-19 09:07:10 · 89 阅读 · 0 评论 -
62. Unique Paths
本题为动态规划问题,共有三种解决方法:二元数组一元数组数学公式return math.factorial(m+n-2)/math.factorial(m-1)/math.factorial(n-1)采用二维数组保存到达每一个位置所有可能的路径个数;在数组中可以寻找到规律:初始化上边和左边为全1;其他位置的元素=上面+左面;最后返回右下角的元素作为最终结果。时...原创 2018-04-23 13:33:21 · 79 阅读 · 0 评论 -
15. 三数之和
题目描述给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:[[-1, 0, 1],[-1, -1, 2]]思路梳理此题需要找出三个...转载 2019-06-15 15:00:16 · 61 阅读 · 0 评论 -
LeetCode 股票交易类题目
121class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ min_price = 2**31 profit = 0 res = 0 ...原创 2019-06-05 22:12:11 · 353 阅读 · 0 评论 -
152. 乘积最大子序列
因为有负数和0的存在,使问题变得复杂了不少。用DP来做,而且要用两个dp数组,其中(1)f[i]表示子数组[0, i]范围内并且一定包含nums[i]数字的最大子数组乘积,(2)g[i]表示子数组[0, i]范围内并且一定包含nums[i]数字的最小子数组乘积,(3)初始化时f[0]和g[0]都初始化为nums[0],其余都初始化为0。(4)那么从数组的第二个数字开始遍历,那么此时的最大...转载 2019-06-24 19:28:39 · 105 阅读 · 0 评论 -
贪心算法
贪心算法1 基本思想2 贪心算法产生优化解的条件贪心算法和动态规划的区别贪心算法的基本思路LeetCode 55 跳跃游戏1 基本思想从问题的某一个初始解出发,通过一系列的贪心选择-当前状态下的局部最优选择,逐步逼近给定的目标;从而达到全局最优 的选择。在每个阶段,都作出一个按照某个评价函数最优的决策,这个评价函数最优称为贪心准则(类似于动态规划的状态转移方程)2 贪心算法产生优化解的条件...转载 2019-06-27 16:45:49 · 566 阅读 · 0 评论 -
LeetCode 74
题目描述:编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:每行中的整数从左到右按升序排列。每行的第一个整数大于前一行的最后一个整数。思路梳理如果将矩阵串联成一行元素,则结果是一个有序数组,然后使用二分查找,寻找元素位置matrix没有shape属性end索引是长度减一class Solution(object): def sear...原创 2019-06-05 08:32:30 · 253 阅读 · 0 评论 -
24. 两两交换链表中的节点
24. 两两交换链表中的节点题目描述思路梳理题目描述给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。给定 1->2->3->4, 你应该返回 2->1->4->3.思路梳理...原创 2019-06-11 14:45:46 · 62 阅读 · 0 评论 -
23. 合并K个排序链表
23. 合并K个排序链表题目描述思路梳理1--分治法思路梳理2--最小堆题目描述合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。思路梳理1–分治法采用分治法的思想,将 k 个排序链表先合并为 k/2 个链表。依次循环,直至合并为1个链表为止。注意:从 k 到 k/2 时,如何定义索引,将每两个链表合并,而且必须符合奇数、偶数个链表的情况。解决办法:k = (n+...原创 2019-06-11 11:25:46 · 80 阅读 · 0 评论 -
Leetcode4 求两个排序数组的中位数 TODO
思路梳理定义一个函数来在两个有序数组中找到第K个元素,下面重点来看如何实现找到第K个元素。首先,为了避免拷贝产生新的数组从而增加时间复杂度,我们使用两个变量 i 和 j分别来标记数组 nums1 和 nums2 的起始位置。然后,来处理一些 corner cases,比如当某一个数组的起始位置大于等于其数组长度时,说明其所有数字均已经被淘汰了,相当于一个空数组了,那么实际上就变成了在另...转载 2019-06-13 17:52:49 · 84 阅读 · 0 评论 -
257/113. Binary Tree Paths/Path Sum II
257.Binary Tree Paths257.题目描述Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1-...原创 2019-06-06 09:48:22 · 61 阅读 · 0 评论 -
19. Remove Nth Node From End of List
19. Remove Nth Node From End of List题目描述移除链表中从末端开始数第N个元素使用两个指针fast和slow:将其间的距离设为相差N个位置,随后将其同时向后移动,直至fast到达最后一个字母,此时slow对应位置即欲删除的元素。特殊情况:删除的元素是链表的第一个元素时,将fast向后移动N个位置,判断它是否为空,若为空,则代表移除的是第一个元素,直接返回head....原创 2018-04-18 09:44:42 · 61 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number
17. Letter Combinations of a Phone Number题目描述类似于9键拼音输入法,将数字对应的所有可能的字母组合都列出来。1. 建立数字-字母映射字典2. 初始化ans,包含一个空字符串3.判断特殊情况:数字串为空4.遍历数字串中的每个数字: 使用列表t保存历史计算结果,本次循环中需要在历史计算结果之后+字符; 清空答案列表ans,本次循环...原创 2018-04-18 09:25:47 · 74 阅读 · 0 评论 -
56. Merge Intervals
56.Merge Intervals题目描述Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].将有重合部分的区间合并。分析先将给定的区间按照区间...原创 2019-06-10 14:50:59 · 176 阅读 · 0 评论 -
495. Teemo Attacking
495.Teemo Attacking题目描述给定一个升序的时间数组timeSeries,为Teemo发起攻击的时间;一个整数为一次攻击能够产生影响的时间长度duration;最后给出总共的影响时间。分析(笨方法)(1)若下次攻击时间到达时,上一次攻击的影响已结束,则可以直接加上duration;(2)若下次攻击时间到达时,上一次攻击的影响未结束,则只加上被影响的时间长度,继...原创 2019-06-10 14:50:50 · 75 阅读 · 0 评论 -
231/326/342. Power of Two/Three/Four
题目描述Given an integer, write a function to determine if it is a power of two/three/four.Follow up: Could you solve it without loops/recursion?分析判断输入的整数是否2/3/4的次幂。解决方法:2的整数次幂意味着其二进制比特串上只有一个非...原创 2019-06-10 14:50:34 · 157 阅读 · 0 评论 -
543. Diameter of Binary Tree
题目描述543. Diameter of Binary TreeGiven 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 thelongestpath between any two nod...原创 2019-06-05 19:40:09 · 65 阅读 · 0 评论 -
714. Best Time to Buy and Sell Stock with Transaction Fee
714.Best Time to Buy and Sell Stock with Transaction Fee题目Your are given an array of integersprices, for which thei-th element is the price of a given stock on dayi; and a non-negativ...原创 2019-06-05 22:40:37 · 61 阅读 · 0 评论 -
718. Maximum Length of Repeated Subarray
718.Maximum Length of Repeated Subarray题目描述Given two integer arraysAandB, return the maximum length of an subarray that appears in both arrays.Example 1:Input:A: [1,2,3,2,1]B: [3,2,1...原创 2019-06-10 14:57:22 · 89 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
102. Binary Tree Level Order Traversal题目描述Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example: Given binary tree[...原创 2019-06-05 20:24:28 · 55 阅读 · 0 评论 -
5. Longest Palindromic Substring
5.Longest Palindromic Substring题目描述Given a strings, find the longest palindromic substring ins. You may assume that the maximum length ofsis 1000.Example:Input: "babad"Output: "bab"...转载 2019-06-15 15:51:13 · 68 阅读 · 0 评论 -
594. Longest Harmonious Subsequence
594.Longest Harmonious Subsequence题目描述We define a harmonious array is an array where the difference between its maximum value and its minimum value isexactly1.Now, given an integer array, ...原创 2019-06-10 14:51:11 · 83 阅读 · 0 评论 -
209. Minimum Size Subarray Sum
209.Minimum Size Subarray Sum题目描述Given an array ofnpositive integers and a positive integers, find the minimal length of acontiguoussubarray of which the sum ≥s. If there isn't one, retu...原创 2019-06-06 09:10:28 · 57 阅读 · 0 评论 -
14. Longest Common Prefix
14. Longest Common Prefix题目描述找出字符串列表中各个字符串的最长公共前缀字符分析使用reduce()函数,依次对列表中的字符串调用lcp()内方法,对字符串迭代使用lcp方法来判断。reduce(function,iterable):依次取出iterable的元素,作为function的参数,并且上一次调用function的返回值,也作为iterable的一个元素lcp(...原创 2018-04-17 09:44:08 · 59 阅读 · 0 评论 -
8. String to Integer (atoi)
8.String to Integer (atoi)题目描述找出字符串中第一个数字字符串(之前只能有空格存在),若是字母开头,则返回零。分析首先使用strip()函数,去掉首尾的空格字符;然后读取第一个字符是否为‘-’号,并保存;之后移除负号;循环遍历负号之后的连续数字字符串,并保存于一个整型变量;最后判断整数的上下边界:正数上界:INT_MAX (231− 1...原创 2018-04-17 09:00:30 · 67 阅读 · 0 评论 -
394. Decode String
https://leetcode.com/problems/decode-string/description/题目描述Given an encoded string, return it's decoded string.The encoding rule is:k[encoded_string], where theencoded_stringinside the sq...原创 2018-04-10 22:58:12 · 158 阅读 · 0 评论 -
7. Reverse Integer
7.Reverse Integer题目描述Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21...原创 2018-04-16 10:19:35 · 72 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
3.Longest Substring Without Repeating Characters题目描述Given a string, find the length of thelongest substringwithout repeating characters.Examples:Given"abcabcbb", the answer is"abc", wh...原创 2018-04-15 11:03:23 · 77 阅读 · 0 评论 -
2. Add Two Numbers
Add Two Numbers题目描述You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand each of their nodes contain a single digit. Add th...原创 2019-06-06 11:32:55 · 80 阅读 · 0 评论 -
395. Longest Substring with At Least K Repeating Characters
395.Longest Substring with At Least K Repeating Characters题目描述Find the length of the longest substringTof a given string (consists of lowercase letters only) such that every character inTa...原创 2019-06-05 22:47:36 · 75 阅读 · 0 评论