
LeetCode
Raven_csdn
分享,记录
展开
-
LeetCode:Sort a linked list in O(n log n) time using constant space complexity.(链表排序)
题目描述:链表排序,空间复杂度要求O(nlogn)。时间复杂度为O(nlogn)的排序方式为归并排序。先把链表分成小链表,小到只有一个元素,这样就编程有序的了,然后就是有序链表的合并,归并的原理就是这样的,关键是怎么编程实现,这里用到两个关键的方法:1、将链表分成小链表的方法。这个方法要掌握,在链表中和很多地方要用到。一般都是采用两个指针一个快指针一个满指针,快指针每次走两步,...原创 2018-07-29 21:52:52 · 280 阅读 · 0 评论 -
LeetCode:75.颜色分类;41. 缺失的第一个正数;通过交换位置来解决。
75. 颜色分类给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。注意:不能使用代码库中的排序函数来解决这道题。示例:输入: [2,0,2,1,1,0]输出: [0,0,1,1,2,2]进阶:一个直观的解决方案是使用计数排序的两趟扫描算...原创 2019-05-18 13:51:30 · 269 阅读 · 0 评论 -
LeetCode:45.跳跃游戏;55. 跳跃游戏
45.跳跃游戏给定一个非负整数数组,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。你的目标是使用最少的跳跃次数到达数组的最后一个位置。示例:输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。class Solution {pu...原创 2019-05-16 16:48:18 · 232 阅读 · 0 评论 -
LeetCode:56. Merge Intervals(合并区间);57.Insert Interval(插入区间)
给出一个无重叠的 ,按照区间起始端点排序的区间列表。在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。输入: intervals = [[1,3],[6,9]], newInterval = [2,5]输出: [[1,5],[6,9]]输入: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], ne...原创 2019-05-14 22:21:01 · 348 阅读 · 0 评论 -
LeetCode:236. Lowest Common Ancestor of a Binary Tree( 二叉树的最近公共祖先)
class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root==NULL){ return root; } //找到p或者q就返回。 if(root...原创 2019-05-18 22:32:58 · 333 阅读 · 0 评论 -
LeetCode
1、两数之和:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]方法一...原创 2019-04-29 23:55:41 · 239 阅读 · 0 评论 -
EditDistance,求两个字符串最小编辑距离,动态规划
问题描述:题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operatio...原创 2019-01-06 21:39:58 · 498 阅读 · 1 评论 -
LeetCode: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] ha...原创 2019-01-06 21:31:12 · 263 阅读 · 0 评论 -
LeetCode:Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.Example: Input: s = 7, ...原创 2019-01-10 15:32:14 · 169 阅读 · 0 评论 -
二叉树前序遍历非递归
二叉树的非递归前序遍历(根->左->右) void preorderTraversal(TreeNode* root) { if(root==NULL){ return ; } TreeNode * p=root; stack<TreeNode *> stas...原创 2018-12-27 17:12:37 · 379 阅读 · 1 评论 -
LeetCode:Largest Rectangle in Histogram(直方图中最大矩形面积)
84. Largest Rectangle in HistogramGiven 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. Ab...原创 2018-12-01 22:43:19 · 856 阅读 · 0 评论 -
LeetCode:Recover-Binary-Search-Tree(恢复有位置交换错误的深度搜索二叉树)
题目:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note: A solution using O(n ) space is pretty straight forward. Could you dev...原创 2018-09-06 20:08:59 · 374 阅读 · 0 评论 -
LeetCode :Given a binary tree, find its maximum depth. (二叉树的最大深度)
题目描述: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.解题思路:第一种是递归方法:每个二叉树的最大深度=左子树的...原创 2018-07-22 13:45:29 · 806 阅读 · 0 评论 -
LeetCode:Given a singly linked list L: L 0→L 1→…→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2...
题目描述:链表:变换成为:两种思路:1、第一种比较容易想到,也比较简单,但是就是复杂度比较高,即使能在牛客网的LeetCode上能通过。就是把所有的元素都存储起来呗,然后新建一个链表,循环取第一个,最后一个,第一个,最后一个直到结束为止,并将这写元素插入到新的链表中,程序如下:import java.util.LinkedList;public class Solution...原创 2018-07-29 22:40:33 · 785 阅读 · 0 评论 -
LeetCode:10.正则表达式匹配;44. 通配符匹配;
10.正则表达式的匹配注释程序去解答题目,递归class Solution {public: bool isMatch(string s, string p) { return matchCore((char*)s.c_str(),(char*)p.c_str()); } bool matchCore(char *str,char * pat){...原创 2019-05-18 16:12:32 · 443 阅读 · 0 评论