
Leetcode刷题
wenkun97
这个作者很懒,什么都没留下…
展开
-
LeetCode 56. 合并区间
给出一个区间的集合,请合并所有重叠的区间。示例 1:输入: [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]]解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].示例 2:输入: [[1,4],[4,5]]输出: [[1,5]]解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。tips :...原创 2020-03-31 20:40:10 · 141 阅读 · 0 评论 -
LeetCode 31. 下一个排列
实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。必须原地修改,只允许使用额外常数空间。以下是一些例子,输入位于左侧列,其相应输出位于右侧列。1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1tips : 先从后往前找到位置i-1,满足位置i元素大于i...原创 2020-03-27 00:21:40 · 145 阅读 · 0 评论 -
LeetCode 24.两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。示例:给定 1->2->3->4, 你应该返回 2->1->4->3.tips : 原地交换,时间复杂度O(n), 空间复杂度O(1)class Solution {public: ListNode* swapPairs(L...原创 2020-03-22 17:15:03 · 107 阅读 · 0 评论 -
LeetCode 41. 缺失的第一个正数
给定一个未排序的整数数组,找出其中没有出现的最小的正整数。示例 1:输入: [1,2,0]输出: 3示例 2:输入: [3,4,-1,1]输出: 2示例 3:输入: [7,8,9,11,12]输出: 1tips : 将大小在1~n之间的元素a,放置到nums[a]处。时间复杂度O(n), 空间复杂度O(1)class Solution {public: int f...原创 2020-03-21 22:13:15 · 104 阅读 · 0 评论 -
LeetCode 611. 有效三角形的个数
给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。示例 1:输入: [2,2,3,4]输出: 3解释:有效的组合是:2,3,4 (使用第一个 2)2,3,4 (使用第二个 2)2,2,3tips : 先排序,从后往前遍历,用双指针法。(只要最小的两条边大于最大的边即可构成三角形)class Solution {public: int tr...原创 2020-03-17 21:37:48 · 295 阅读 · 0 评论 -
LeetCode 15.三数之和
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。示例:给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:[[-1, 0, 1],[-1, -1, 2]]tips: 排序+双指针,时间复杂...原创 2020-03-17 15:14:53 · 381 阅读 · 0 评论 -
LeetCode 146.LRU缓存机制
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据...原创 2020-03-13 19:53:46 · 106 阅读 · 0 评论 -
Leetcode 78. 子集
题目描述给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。说明:解集不能包含重复的子集。示例:输入: nums = [1,2,3]输出:[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]]tips : 每增加一个元素,让之前幂集中的每个集合,追加这个元素,就是新增的子集。class Solution {p...原创 2020-02-07 23:53:28 · 107 阅读 · 0 评论 -
Leetcode 300 Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence.For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], theref原创 2018-01-08 23:27:21 · 224 阅读 · 0 评论 -
Leetcode 452 .Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordina原创 2017-12-27 19:56:14 · 159 阅读 · 0 评论 -
Leetcode 343 Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, return 1原创 2018-01-01 15:04:04 · 179 阅读 · 0 评论 -
算法概论8.15 最大公共子图问题
8.15 Show that the following problem is NP-complete. MAXIMUM COMMON SUBGRAPH Input: Two graphs G1=(V1,E1) and G2=(V2,E2); a budget b. Output: Two set of nodes V’1⊆V1, V’2⊆V2, whose deletion leaves a原创 2017-12-31 10:50:36 · 1464 阅读 · 0 评论 -
leetcode 712 Minimum ASCII Delete Sum for Two Strings
Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal.Example 1: Input: s1 = “sea”, s2 = “eat” Output: 231 Explanation: Deleting “s” from “sea” adds the原创 2017-12-07 13:09:48 · 223 阅读 · 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 tree. T原创 2017-12-20 20:43:27 · 202 阅读 · 0 评论 -
算法分析与设计期中测试——拓扑序[Special judge]
在图论中,拓扑序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列. 且该序列必须满足下面两个条件:每个顶点出现且只出现一次.若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面.对于一个含有n个节点的有向无环图(节点编号0到n-1),输出它的一个拓扑序.图的节点数和边数均不原创 2017-12-02 20:03:49 · 329 阅读 · 0 评论 -
算法分析与设计期中测试——最小和
从数列A[0], A[1], A[2], …, A[N-1]中选若干个数,要求对于每个i(0<=i< N-1),A[i]和A[i+1]至少选一个数,求能选出的最小和.1 <= N <= 100000, 1 <= A[i] <= 1000请为下面的Solution类实现解决上述问题的函数minSum,函数参数A是给出的数列,返回值为所求的最小和.class Solution { public:原创 2017-12-02 11:34:46 · 376 阅读 · 0 评论 -
leedcode 14. Longest Common Prefix
题目描述: Write a function to find the longest common prefix string amongst an array of strings.很简单就一句话,但是一开始还是有些不理解,什么叫“common prefix string”共同的前缀字符串?嗯就是给一个string类型的vector容器,找出所有的字符串从左往右相同的部分。 需要注意的是若没有原创 2017-09-16 17:00:21 · 226 阅读 · 0 评论 -
leedcode 2. Add Two Numbers
题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and ret原创 2017-09-09 12:19:56 · 241 阅读 · 0 评论 -
leetcode 21. Merge Two Sorted Lists
题目描述: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.很简单就是一个归并两个已经排好序的两个链表。也是归并排序中核心的代码。主要用到了链表的知识,考验了链表的构原创 2017-09-20 15:59:31 · 157 阅读 · 0 评论 -
LeetCode684 Redundant Connection
In this problem, a tree is an undirected graph that is connected and has no cycles.The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additional原创 2017-10-27 15:42:16 · 279 阅读 · 0 评论 -
leetcode399 Evaluate Division
题目描述: Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the an原创 2017-10-11 12:54:58 · 324 阅读 · 0 评论 -
leetcode 169 Majority Element
题目描述: Given 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 majority element原创 2017-11-09 19:55:33 · 250 阅读 · 0 评论 -
Leetcode406 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 this p原创 2017-11-17 14:55:57 · 229 阅读 · 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原创 2017-11-03 16:07:57 · 226 阅读 · 0 评论 -
Leetcode 718 Maximum Length of Repeated Subarray
Given two integer arrays A and B, 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,4,7] Output: 3 Explanation: The repeated subarra原创 2017-11-29 23:55:46 · 167 阅读 · 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 consist of原创 2017-12-08 14:44:05 · 184 阅读 · 0 评论 -
leetcode 392 Is Subsequence
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, an原创 2017-11-16 18:41:12 · 245 阅读 · 0 评论 -
leetcode 17. Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string原创 2017-09-25 01:08:14 · 301 阅读 · 0 评论