![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 61
一颗随风而倒的墙头草
这个作者很懒,什么都没留下…
展开
-
【LeetCode】24.两两交换链表中的节点(python 版)
题目描述:给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。示例:给定 1->2->3->4, 你应该返回 2->1->4->3.思路:方法一:迭代方法:基础的三指针法,分别指向需要交换的两个节点和它们的前一个节点,注意节点交换时各个链的连接顺序。因为头结点会与下一个节点...原创 2020-05-13 20:53:56 · 437 阅读 · 0 评论 -
【LeetCode】23.合并 K 个排序链表(python版)
题目描述:合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。示例:输入:[ 1->4->5, 1->3->4, 2->6]输出: 1->1->2->3->4->4->5->6思路:这个题是简单难度的《合并两个排序链表》的加强版。方法一:一个很容易想的思路是:先合并前两个链...原创 2020-02-09 16:14:46 · 248 阅读 · 0 评论 -
【LeetCode】14.最长公共前缀(python版)
题目描述:编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 “”。示例 1:输入: ["flower","flow","flight"]输出: "fl"示例 2:输入: ["dog","racecar","car"]输出: ""解释: 输入不存在公共前缀。说明:所有输入只包含小写字母 a-z 。思路:本题虽然只是“容易”等级的题目,但是...原创 2020-01-21 14:43:19 · 577 阅读 · 1 评论 -
【LeetCode】22.括号生成(Python 版)
题目描述:给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。例如,给出 n = 3,生成结果为:[ "((()))", "(()())", "(())()", "()(())", "()()()"]思路:方法一:暴力法:枚举出所有的括号组合,再判断其是否有效class Solution: def generateP...原创 2020-01-20 15:16:46 · 1559 阅读 · 0 评论 -
【LeetCode】19. 删除链表的倒数第N个节点(python 版)
题目描述:给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。示例:给定一个链表: 1->2->3->4->5, 和 n = 2.当删除了倒数第二个节点后,链表变为 1->2->3->5.说明:给定的 n 保证是有效的。进阶:你能尝试使用一趟扫描实现吗?思路:# Definition for singly-linked...原创 2020-01-20 09:45:47 · 401 阅读 · 0 评论 -
【LeetCode】17.电话号码的字母组合(python版)
题目描述:给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。示例:输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].说明:尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。思路:回溯法(穷举法)...原创 2020-01-19 15:33:44 · 1228 阅读 · 0 评论 -
【LeetCode】6.Z字形变换(python版)
题目描述:将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。比如输入字符串为 “LEETCODEISHIRING” 行数为 3 时,排列如下:L C I RE T O E S I I GE D H N之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:“LCIRETOESIIGEDHN”。请你实现这个将字符串进行指定行数变换...原创 2020-01-19 10:42:14 · 362 阅读 · 0 评论 -
【LeetCode】11.盛最多水的容器(python版)
题目描述:给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。说明:你不能倾斜容器,且 n 的值至少为 2。图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水...原创 2020-01-17 10:41:56 · 478 阅读 · 0 评论 -
【LeetCode】788.旋转数字(python版)
题目描述:我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数。要求每位数字都要被旋转。如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个数是有效的。0, 1, 和 8 被旋转后仍然是它们自己;2 和 5 可以互相旋转成对方;6 和 9 同理,除了这些以外其他的数字旋转以后都不再是有效的数字。现在我们有一个正整数 N, 计...原创 2020-01-16 11:34:20 · 309 阅读 · 0 评论 -
【LeetCode】5.最长回文子串(python版)
题目描述:给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。示例 1:输入: "babad"输出: "bab"注意: "aba" 也是一个有效答案。示例 2:输入: "cbbd"输出: "bb"思路:暴力算法:遍历字符串,以每个字符作为中心词,往两边延伸判断是否左右两边字符是否相等。每次只需要维护延伸得到的最长回文子串。暴力算法的时...原创 2020-01-16 11:19:06 · 547 阅读 · 0 评论 -
【LeetCode】442.数组中重复的数据(python版)
题目描述:给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。找到所有出现两次的元素。你可以不用到任何额外空间并在O(n)O(n)O(n)时间复杂度内解决这个问题吗?示例:输入:[4,3,2,7,8,2,3,1]输出:[2,3]...原创 2020-01-08 11:21:19 · 949 阅读 · 1 评论 -
【LeetCode】无重复字符的最长子串(python版)
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。示例 2:输入: "bbbbb"输出: 1解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。示例 3:输入: "pwwkew"输出: 3解释: 因为无重复字符的最长子串是 "wke...原创 2019-12-25 16:41:44 · 249 阅读 · 0 评论 -
【每日一练】最长斐波那契子序列
题目描述:给定一个数组,找出其中最长的子序列,满足该子序列是斐波那契子序列注: 如果序列 X_1, X_2, …, X_n 满足下列条件,就说它是 斐波那契式 的:n >= 3对于所有 i + 2 <= n,都有 X_i + X_{i+1} = X_{i+2}思路:本题一开始想岔了,一直以为Fibonacci数列必须是严格按照{1,1,2,3,5,8……}的...原创 2019-10-25 16:04:50 · 614 阅读 · 0 评论 -
【LeetCode】【137. Single Number II】(python版)
Description:Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:Your algorithm should have a linear runti...原创 2019-08-21 16:18:32 · 180 阅读 · 0 评论 -
【LeetCode】【18.4sum】(python版)
Describe:Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the...原创 2019-06-14 21:18:03 · 171 阅读 · 0 评论 -
【LeetCode】【60. Permutation Sequence】【python版】
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, we get the following sequence for n = 3:“123” “132” “213” “231” “312” ...原创 2018-07-13 18:23:48 · 556 阅读 · 3 评论 -
【LeetCode】【1.two sum】(python版)
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 same ...原创 2018-07-13 21:34:57 · 277 阅读 · 0 评论 -
【LeetCode】【5. Longest Palindromic Substring】(python版)
Description: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: "babad"Output: "bab"Note: &a原创 2018-07-28 15:38:25 · 353 阅读 · 0 评论 -
【LeetCode】【837. New 21 Game】(python版)
Description: Alice plays the following game, loosely based on the card game “21”.Alice starts with 0 points, and draws numbers while she has less than K points. During each draw, she gains an inte...原创 2018-07-28 18:16:21 · 1475 阅读 · 0 评论 -
【LeetCode】【322. Coin Change】(python版)
Description: 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 am...原创 2018-07-28 19:08:11 · 2025 阅读 · 0 评论 -
【LeetCode】【16.3sum closest】(python版)
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would ...原创 2018-07-15 21:50:58 · 258 阅读 · 0 评论 -
【LeetCode】【152. Maximum Product Subarray】(python版)
def maxProduct(self, nums): maxvalue = [0 for i in range(len(nums))] minvalue = [0 for i in range(len(nums))] maxvalue[0] = minvalue[0] = nums[0] globalmax = float(...原创 2018-07-16 10:04:10 · 988 阅读 · 0 评论 -
【LeetCode】【84. Largest Rectangle in Histogram】(python版)
Description:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where ...原创 2018-08-22 11:40:35 · 284 阅读 · 0 评论 -
【LeetCode】【3. Longest Substring Without Repeating Characters】(python版)
Description: Given a string, find the length of the longest substring without repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", which the length is 3....原创 2018-08-21 19:09:55 · 604 阅读 · 0 评论 -
【LeetCode】【395. Longest Substring with At Least K Repeating Characters】(python版)
Description: Find 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 = "aa...原创 2018-08-21 20:43:06 · 634 阅读 · 0 评论 -
【LeetCode】【220. Contains Duplicate III】(python版)
Description: Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolu...原创 2018-09-01 20:41:17 · 1944 阅读 · 0 评论 -
【LeetCode】【136. Single Number】(python版)
Description: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...原创 2018-09-02 14:56:30 · 243 阅读 · 0 评论 -
【LeetCode】【31. Next Permutation】(python版)
Description: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the low...原创 2018-07-12 22:19:39 · 784 阅读 · 0 评论