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

【LeetCode/力扣】#1720 - 解码异或后的数组【代码】

1 题目描述 题目链接:https://leetcode-cn.com/problems/decode-xored-array/未知 整数数组 arr 由 n 个非负整数组成。 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encoded[i] = arr[i] XOR arr[i + 1] 。例如,arr = [1,0,2,1] 经编码后得到 encoded = [1,2,3] 。 给你编码后的数组 encoded 和原数组 arr 的第一个元素 first(arr[0])。 请解码返回原数组 arr 。可以证明答案存在并且是唯一的。示例 1: 输入:...

Leetcode: Linked List Cycle【代码】

Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?Analysis: typical Runner Technique. 一次过 1/** 2 * Definition for singly-linked list.3 * class ListNode {4 * int val;5 * ListNode next;6 * ListNode(int x) {7 * val = x;8 * next = null;9 * } 10 * } 11*/12publicclass Solution { 13publicboolean hasCycle(ListNod...

LeetCode | Sqrt(x)

题目 Implement int sqrt(int x). Compute and return the square root of x. 分析 两种方法:1. 牛顿法(解法1)2. 二分查找(解法2)解法1public class Sqrt {public int sqrt(int x) {double ret = 1, temp = 0;while ((int) ret - (int) temp != 0) {temp = ret;ret = ret / 2 + (double) x / (2 * ret);}return (int) ret;} }解法2 public class Sqrt {public int sqrt(int x) {if (x < 2) {return x;}int left = 1;int right...

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 always exist in the array.Credits:Special thanks to @ts for adding this problem and creating all test cases. 题目解析:我用了Hashmap的方法, 感觉不是最好的, 但是是最好想到的. 如果有更好方法的童鞋们求评论我~~...

leetcode-204-Count Primes【代码】

题目描述:Count the number of prime numbers less than a non-negative number, n. 要完成的函数:int countPrimes(int n) 说明:1、题目看上去非常简单和熟悉。给定一个非负数n,要求返回小于n的所有素数的个数。2、处理一下边界条件,n<=2时返回0,n=3时返回1,n=4时返回2。3、传统方法:对于小于n的每个数i,判断i是不是素数。判断方法是对于每个大于等于2且小于等于i/2的数,确定i能否整除这个数。双重循环,暴力解法。然后...

【LeetCode】Longest Palindromic Substring【代码】【图】

Longest Palindromic SubstringGiven a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 同Palindrome Partitioning II 几乎一致,二维数组动态规划问题。需要注意两点:1、使用数组比使用vector of vector节省时间;2、每次发现当前最优回文串就使用substr取出会耗时间,改成记录起始位置。class Sol...

[Leetcode] Decode Ways【代码】

Decode Ways 题解题目来源:https://leetcode.com/problems/decode-ways/description/DescriptionA message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it.ExampleGiven encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).The numbe...

LeetCode783. 二叉搜索树节点最小距离【中序遍历】【代码】【图】

难度:简单 题目描述: 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(...

2015.03.30 LeetCode Merge Intervals 解题记录

今天下午做了一道题。leetcode merge intervals 属于比较难的题目。 首先用collections.sort 给list排序,然后用两个while loop来比较两个interval 的start, end 。 从而生成新的interal,再插入到新的list 返回结果。 下面给出自己的代码:/*50 Merge Intervals https://leetcode.com/problems/merge-intervals/Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18]...

LeetCode53——Maximum Subarray——3 different methods【代码】【图】

这道题目让我们求出一个线性数组中最大的连续子数组的和,题目要求如下: 这道题的题目很简单,解法也有多种,在这里总结3种解法去解决这个问题,在比较中加深对不同思路算法的理解,这三种算法是动态规划、模拟法、滑动窗口。 首先是动态规划法,正如我们之前提过的,递推形式的动态规划基本上就是三部曲: 1.确定DP数组的含义 2.递推奠基 3.基于递推方程来进行反复递推,直到最终答案 就这道题而言,DP[i]的含义是以nums[i]为结...

LeetCode 60. Permutation Sequence【代码】【图】

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 (ie, for n = 3):"123""132""213""231""312""321"Given n and k, return the kth permutation sequence.Note: Given n will be between 1 and 9 inclusive.【问题描述】给定1到n这n个数字,他们的排列组合共有n!个, 把这些数字排列组合的得到的数升序排序,返回其中第...

leetcode刷题27【代码】

今天刷的题是LeetCode第88题,题目要求和示例是:给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。说明:初始化 nums1 和 nums2 的元素数量分别为 m 和 n。你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。示例:输入:nums1 = [1,2,3,0,0,0], m = 3nums2 = [2,5,6], n = 3输出: [1,2,2,3,5,6]这个题很简单,我的代码是:class Solution {publ...

Leetcode 1094. 拼车 (差分数组)【代码】【图】

这种连续区间的加法处理,是典型的差分数组优化。 class Solution { public:bool carPooling(vector<vector<int>>& trips, int capacity) {vector<int> delta(1010);for(auto trip: trips){delta[trip[1]] += trip[0];delta[trip[2]] -= trip[0]; }int cur = 0;for(int i = 0; i <= 1000; i++){cur += delta[i];if(cur > capacity){return false;}}return true;} };

[LeetCode] 3. Longest Substring Without Repeating Characters 最长无重复字符的子串【代码】

1.暴力法: 本题让求给定字符串的最长的无重复字符的子串,首先想到暴力解法,穷举出字符串的所有子串,并判断每个子串是否是不重复子串,具体使用hashset或set判是否有重复字符;暴力法效率很差,时间O(n^3),空间O(n);参考代码如下: 1class Solution {2public:3int lengthOfLongestSubstring(string s){4int res = 0;5constint size = s.size();6if(s.empty()) return0;7if(size<=1) return size;8for(int i = 0;i<size;++i...

LeetCode: Binary Tree Inorder Traversal【代码】

LeetCode: Binary Tree Inorder TraversalGiven a binary tree, return the inorder traversal of its nodes‘ values. For example: Given binary tree {1,#,2,3}, 12/3 return [1,3,2].Note: Recursive solution is trivial, could you do it iteratively?地址:https://oj.leetcode.com/problems/binary-tree-inorder-traversal/算法:要求用非递归来进行中序遍历。初始时,从根节点开始,一直往左走,并把每个节点入栈。在wh...