【Leetcode 076. 最小覆盖子串 双指针】教程文章相关的互联网学习教程文章

leetCode 34.Search for a Range (搜索范围) 解题思路和方法

Search for a Range Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm‘s runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given [5, 7, 7, 8, 8, 10] and target value 8,return [3, 4].思路:此题在思考的时候走了些弯路,一心想着一个循环解决问题,但是写代码的时候总是不...

leetcode Best Time to Buy and Sell Stock III【代码】

是之前两道题leetcode Best Time to Buy and Sell Stock和leetcode Best Time to Buy and Sell Stock II的加强版。这里要求只能买两次股票。做了一个多小时,试了好多次,终于AC了。思路:找到有可能为断点的地方,也就是出现递减的地方,递减了就说明有损失所以有可能卖出。对每个可能的分割点,计算左右两边的最大收益,求最大收益和leetcode Best Time to Buy and Sell Stock这个差不多。然后把所有可能的解记录,返回最大的解就...

【leetcode】two sum --medium【代码】

Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.You may assume that each input would have exactly one solution.Input: numbers={2, 7, ...

Subsets -- leetcode【代码】

Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets. For example, If S = [1,2,3],a solution is:[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[] ]基本思路:1, 先插入一个空集作为起始。2, 然后对结果集中的每个元素,复制并添加上新增字符。此代码,在leetcode上实际执行时间为16ms。class Sol...

[LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点【代码】

Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example: Given binary tree 1/ 2 3/ \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Remove the leaves [4, 5, 3] from the tree1/ 2 2. Remove the leaf [2] from the tree 1 3. Remove the leaf [1] from the tree [...

【leetcode】Next Permutation(middle)【代码】

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 lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the ri...

Leetcode | Rotate Image【代码】【图】

You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?实现题。从最外圈顺时针交换,最多交换n/2圈就行。 1class Solution {2public:3void rotate(vector<vector<int> > &matrix) {4int n = matrix.size();5int tmp;6// 这个row其实就是圈数,最后中间可能会剩下一个点,这个点不需要旋转,无碍 7for (int row = 0; row < n / 2; ++row) {8 ...

Leetcode 076. 最小覆盖子串 双指针【代码】

地址 https://leetcode-cn.com/problems/minimum-window-substring/给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字符的最小子串。示例:输入: S = "ADOBECODEBANC", T = "ABC" 输出: "BANC" 说明:如果 S 中不存这样的子串,则返回空字符串 ""。 如果 S 中存在这样的子串,我们保证它是唯一的答案。一个窗口内的连续数组的性质变化, 考虑用双指针来解决这个问题。我们首先需要解决如何能快速的知道添加或...

leetcode 53. 最大子序和【代码】

题解 简单的动态规划吧算是 还是比较简单的题目 首先计算sum的值,如果和的值大于0那么这一段的和就对后面那个数有用,否则,就重新从下一个数开始,然后取最大值。 代码 class Solution { public:int maxSubArray(vector<int>& nums) {int n = nums.size();int ans = nums[0],sum = 0;for(int i = 0; i < n; i++){if(sum > 0){sum = sum + nums[i];}else{sum = nums[i];}ans = max(ans , sum);}return ans;} };

Leetcode: Rotate Image【代码】

Rotate ImageTotal Accepted: 37958 Total Submissions: 118891 You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up: Could you do this in-place? 先按145°对角线将图像对称翻转, 再将各行逆序翻转即可 class Solution { public:void rotate(vector<vector<int> > &matrix) {int n = matrix.size();for (int y = 1; y < n; y++)for (int x = 0; x < y; x++)swap(mat...

LeetCode 371 Sum of Two Integers【图】

Problem:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Summary:不使用+和-符号,计算两个整型数之和。Analysis:XOR相当于二进制数的无进位加法。进位位由两数&运算后左移一位确定。Example:用此方法计算5 + 3:1、无进位结果:101 ^ 011 = 110  进位:101 & 011 = 001  左移:001 << 1 = 0102、将进位与原结果相加:110 ^ 010 = 100  进位:110 & 010 = 010  左移:0...

LeetCode—House Robber 寻找数组不相邻组合最大值DP

https://leetcode.com/problems/house-robber/题目设计了一个抢劫犯的情景,其实就是求数组中不相邻数据进行组合得到的最大值举一个例子假设数据: 8 3 6 15 4 9 7 10那么首先可能选取 8 , 3每一个数字的选取都是根据他的前两个数字,前三个数字得到的最大值进行选择,等到6的时候考虑前面只能和8组合 8,3,14到数字15,那么就可以考虑在8,3中进行组合 8,3,14,23,4,9,7,10接下来的步骤:8,3,14,23,18,9,7,108,3,14,23,18,32,7,1...

[LeetCode] Paint House I & II【代码】

Paint HouseThere are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.The cost of painting each house with a certain color is represented by a n x 3 cost matrix. For example, costs[0][0] is the cost of...

转-LeetCode-二叉搜索树的后续遍历序列【代码】

二叉搜索树的后续遍历序列输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。单挑递增栈辅助,逆向遍历数组// 单调递增栈辅助,逆向遍历数组。 var verifyPostorder = function (postorder) { // 单调栈使用, 单调递增的单调栈 let stack = []; // 表示上一个根节点的元素,这里可以把postorder的最后一个元素root看成无穷大节点...

LeetCode(4)最大回文数 (中等)

问题描述: 给你一个字符串 s,找到 s 中最长的回文子串。 代码: public class Solution {public String longestPalindrome(String s) { int len = s.length(); if (len < 2) { return s; }int maxLen = 1; int begin = 0; // dp[i][j] 表示 s[i..j] 是否是回文串 boolean[][] dp = new boolean[len][len]; // 初始化:所有长度为 1 的子串都是回文串 for (...