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

leetcode Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1"Return "100".这道也是水题,但是注意有前导0,还有全0的时候输出为0。.还有最后结果的处理。#include <iostream> #include <string>using namespace std;class Solution { public:string addBinary(string a, string b) {if (a.empty()){return b;}if (b.empty()){return a;}int i = 0;int j = 0;for (; i < a.size(); ++i){if...

leetcode 题型整理之数字加减乘除乘方开根号【代码】

需要注意overflow,特别是Integer.MIN_VALUE这个数字。需要掌握二分法。不用除法的除法,分而治之的乘方2. Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8基本等于合并数组,记得...

LeetCode085最大子矩形(相关话题:单调栈)【代码】【图】

题目描述:给定一个仅包含?0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。示例:输入: [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"] ] 输出: 6解题思路:1、计算每个行和前面所有行叠加所产生的数组里的最大子矩阵,求出最大的数组里的最大子矩阵。叠加规则为遇到1累加,遇到0重新开始计算2、数组的最大子矩阵实际就是求以j为中心向左右寻找第一个比j柱子小的柱子...

leetcode之链表十二: 1721-交换链表中的节点【代码】【图】

原题: https://leetcode-cn.com/problems/swapping-nodes-in-a-linked-list/ 一、题目要求 给你链表的头节点 head 和一个整数 k 。 交换 链表正数第 k 个节点和倒数第 k 个节点的值后,返回链表的头节点(链表 从 1 开始索引)。 示例 1:输入:head = [1,2,3,4,5], k = 2 输出:[1,4,3,2,5]示例 2:输入:head = [7,9,6,6,7,8,3,0,9,5], k = 5 输出:[7,9,6,6,8,7,3,0,9,5]示例 3:输入:head = [1], k = 1 输出:[1] 示例 4:输入...

Leetcode 945 使数组唯一的最小增量 贪心【代码】

地址 https://leetcode-cn.com/problems/minimum-increment-to-make-array-unique/给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1。返回使 A 中的每个值都是唯一的最少操作次数。示例 1:输入:[1,2,2] 输出:1 解释:经过一次 move 操作,数组将变为 [1, 2, 3]。 示例 2:输入:[3,2,1,2,1,7] 输出:6 解释:经过 6 次 move 操作,数组将变为 [3, 4, 1, 2, 5, 7]。 可以看出 5 次或 5 次以下的 move 操作是不能让数...

977.leetcode_有序数组的平方【代码】

思路一:暴力解法,直接平方,然后添加到vector中class Solution { public:vector<int> sortedSquares(vector<int>& A) {vector<int> res;if(A.size()==0){return res;}for(auto i:A){res.push_back(i*i);}sort(res.begin(),res.end());return res;} }; 思路二:原文:https://www.cnblogs.com/tianjiale/p/11069036.html

leetcode 50 pow(x,y)【代码】

Implement pow(x, n), which calculates x raised to the power n (xn).Example 1:Input: 2.00000, 10 Output: 1024.00000 Example 2:Input: 2.10000, 3 Output: 9.26100 Example 3:Input: 2.00000, -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/4 = 0.25 Note:-100.0 < x < 100.0n is a 32-bit signed integer, within the range [?231, 231 ? 1]可能是我想多了,因为int的负数范围比正数范围多一,转化为正整数要考虑他的一个...

【leetcode】14. longest common prefix【代码】

@requires_authorization @author johnsondu @create_time 2015.7.1310:11 @url [longest-common-prefix](https://leetcode.com/problems/longest-common-prefix/)/*** 求出string中的最小长度* 然后依次每个string从第一个开始进行比较* 时间复杂度:O(n*m), n是string个数,m是string的长度*/class Solution { public:string longestCommonPrefix(vector<string>& strs) {int len = strs.size();if(len == 0) return"";if(len ==...

【LeetCode每日一题】整数反转【代码】

整数反转1、题目描述给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。如果反转后整数超过 32 位的有符号整数的范围 [?231, 231 ? 1] ,就返回 0。 注意:假设环境不允许存储 64 位整数(有符号或无符号)。示例1:输入:x = 123 输出:321 示例2:输入:x = -123 输出:-321 示例3:输入:x = 120 输出:21 示例4:输入:x = 0 输出:0 2、算法描述核心思想:主要就是分情况讨论,题目本身很简单,把数字按照...

LeetCode 28:Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 实现函数strStr。代码如下:int strStr(char* haystack, char* needle) {size_t len = strlen(haystack);if(strlen(needle) == 0)return 0;if(strcmp(haystack, needle) == 0)return 0;for(int i=0; i<len;i++){if(haystack[i] == needle[0]){if(strncmp(&(haystack[i]), needle, strlen(needle)...

leetcode 7 整数翻转【代码】

选了个先转换成字符串,然后再翻转的做法,虽然效率不差,但属实有点拉,贴代码class Solution { public:char* inttoString(int a,char* b){int t = a/10;if(t!=0){b = inttoString(a/10,b) +1;}*b = a%10 + ‘0‘;return b;}int reverse(int x) {if(x == -2147483648)return0;bool fu = false;// long t = x;if(x<0){fu = true;x = -x; }char a[20];memset(a,‘\0‘,sizeof(a));inttoString(x,a);int i = 0;while(a[i]!=‘...

Leetcode刷题路线总结【图】

为了跳槽,我目前为止刷了小四百道leetcode,也算是有一些经验,今天就跟大家分享下学习方法和我总结的干货。 注:文末附资料下载~ 如何找到感觉 讲起算法,我看过不少书,有《算法导论》、《算法第四版》、《算法竞赛入门经典》、《剑指Offer》,但都没有让我产生质变,现在回想原因可能是: 看书的时候着急,不过脑子直接看解析,也不记笔记,过几天就忘了看完书就觉得自己会了,直接 Leetcode 随机选题,还是不会,内心受挫就不...

leetcode Longest Substring with At Most Two Distinct Characters【代码】

找到最多含有两个不同字符的子串的最长长度。例如:eoeabc,最长的是eoe为3,其他都为2.思路:用p1,p2表示两种字符串的最后一个出现的下标位置。初始p1为0. p2为-1.start初始化为0,表示两种字符串的开头。只要遍历一次string就可以得到结果了。首先我们要确定p2的值,那么i要一直到不等于s[p1]的值为止,那么位置就是p2了。然后继续往后如果来一个字符等于之前两种的其中一种,那么就要更新最后一次出现的下标。根据是谁就更新谁。...

LeetCode142:Linked List Cycle II【代码】【图】

题目:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up: Can you solve it without using extra space?解题思路:判断链表有无环,可用快慢指针进行,快指针每次走两步,慢指针每次走一步,如果快指针追上了慢指针,则存在环,否则,快指针走到链表末尾即为NULL是也没追上,则无环。为什么快慢指针可以判断有无环?因为快指针先进入环,在慢指针进入之后,如果把慢...

[LeetCode] Permutations II

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1],and [2,1,1].解题思路:这道题与Permutations(http://blog.csdn.net/kangrydotnet/article/details/45691783)是姊妹题,唯一不同的是这道题的元素可以重复。其实我们在排列之前,先对整个数组进行排序。在递归过程中,若当...