链接:https://leetcode.cn/problems/maximum-subarray/ 之前题解的博客:https://tsuish.gitee.io/p/7a78 注:之后把这篇博客整理到hexo 我的代码 int max(int a,int b){ return a>b?a:b; } int maxSubArray(int* nums, int numsSize){ int dp[100001],res = nums[0]; dp[
组合数公式介绍 组合算法常用案例场景 场景一:简单组合 n个不同元素 重新排列有多少种组合 /** * 功能描述 組合算法 * * @author chch213 * @version 1.0 * @Date 2022/7/26 */ public class Main01 { public static void main(String[] args) { combination(
给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。 import java.util.*; import java.util.stream.Collectors; public class LC349 { //运用流的一种解法 public int[] intersecti
LeetCode 实现跳表 主要参考了以下几篇Bolg: https://www.acwing.com/blog/content/15081/ https://www.jianshu.com/p/9d8296562806 https://www.acwing.com/blog/content/4863/ https://www.luogu.com.cn/blog/your-alpha1022/SkipList class Skiplist { public: const stati
1. 深度优先 class Solution { public: int countNodes(TreeNode* root) { if(!root) return 0; return 1+countNodes(root->left)+countNodes(root->right); } }; 2. 广度优先 class Solution { public: int countNodes(TreeNode* root) {
复数 可以用字符串表示,遵循 "实部+虚部i" 的形式,并满足下述条件: 实部 是一个整数,取值范围是 [-100, 100] 虚部 也是一个整数,取值范围是 [-100, 100] i2 == -1 给你两个字符串表示的复数 num1 和 num2 ,请你遵循复数表示形式,返回表示它们乘积的字符串。 示例 1: 输入:num1 = "1+1i"
1. 二叉树的层平均值 class Solution { public: vector<double> averageOfLevels(TreeNode* root) { auto averages = vector<double>(); auto q = queue<TreeNode*>(); q.push(root); while (!q.empty()) { doubl
1. 树中是否存在根节点到叶子节点的路径 class Solution { public: bool hasPathSum(TreeNode *root, int sum) { if (root == nullptr) { return false; } if (root->left == nullptr && root->right == nullptr) { return
1. 合并两个有序链表 class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* preHead = new ListNode(-1); //一定要使用头指针并复制一个副本,不然后面循环操作不统一,未合并完的也不好加上去 ListNode* prev =
最接近的三数之和 你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。 返回这三个数的和。 假定每组输入只存在恰好一个解。 示例 1: 输入:nums = [-1,2,1,-4], target = 1 输出:2 解释:与 target 最接近的和是 2 (-
给定一个含有 n 个正整数的数组和一个正整数 target 。 找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。 示例 1: 输入:target = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组 [4,
思路: 拓扑排序。 实现: 1 class Solution { 2 public: 3 bool sequenceReconstruction(vector<int>& a, vector<vector<int>>& s) { 4 int n=a.size(),m=s.size(); 5 vector<int>in(n+1,0); 6 vector<vector&l
1. 根据身高重建队列 假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序) 每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好有 ki 个身高大于或等于 hi 的人 class Solution { public: vector<vector<int>> reconstructQueue(v
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() removes the elem
import java.util.HashSet; class Solution { public int lengthOfLongestSubstring(String s) { // 滑动窗口 int maxLength = 0; for (int i = 0; i < s.length(); i ++) { int length = 0; HashSet<Character>
题目信息 源地址:回文数 给你一个整数 x,如果 x 是一个回文整数,返回 true;否则,返回 false。 回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 例如,121 是回文,而 123 不是。 提示信息 示例 1 输入:x = 121 输出:true 示例 2 输入:x = -121 输出:false 解释:从左向右读, 为 -121
/** <p>你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,<strong>如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警</strong>。</p> <p>给定一个代表每个房屋存放金额的非负整数数组,计算你<s
原题链接在这里:https://leetcode.com/problems/web-crawler-multithreaded/ 题目: Given a URL startUrl and an interface HtmlParser, implement a Multi-threaded web crawler to crawl all links that are under the same hostname as startUrl. Return all URLs obtai
shell部分的练习在牛客网有34题,在Leetcode有4题,总体来说难度不大,熟练就好。 牛客部分: 1.统计文件行数 写一个 bash脚本以输出一个文本文件 nowcoder.txt中的行数 示例: 假设 nowcoder.txt 内容如下: #include <iostream> using namespace std; int main() { int a =
链接:LeetCode [Leetcode]2335. 装满杯子需要的最短总时长 现有一台饮水机,可以制备冷水、温水和热水。每秒钟,可以装满 2 杯 不同 类型的水或者 1 杯任意类型的水。 给你一个下标从 0 开始、长度为 3 的整数数组 amount ,其中 amount[0]、amount[1] 和 amount[2] 分别表示需要装满冷
给你一个整数数组 nums 和一个正整数 k,请你判断是否可以把这个数组划分成一些由 k 个连续数字组成的集合。 如果可以,请返回 true;否则,返回 false。 示例 1: 输入:nums = [1,2,3,3,4,4,5,6], k = 4 输出:true 解释:数组可以分成 [1,2,3,4] 和 [3,4,5,6]。 示例 2: 输入:nums = [3,2,1,2,3
class Solution { public: static bool mycomp(pair<string, int> a, pair<string, int> b) { return a.first < b.first; } vector<int> smallestTrimmedNumbers(vector<string>& nums, vector<vector<int>>
题目:数组嵌套 索引从0开始长度为N的数组A,包含0到N - 1的所有整数。找到最大的集合S并返回其大小,其中 S[i] = {A[i], A[A[i]], A[A[A[i]]], ... }且遵守以下的规则。 假设选择索引为i的元素A[i]为S的第一个元素,S的下一个元素应该是A[A[i]],之后是A[A[A[i]]]... 以此类推,不断添加直
A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two
给你一个字符串 s,它仅由字母 'a' 和 'b' 组成。每一次删除操作都可以从 s 中删除一个回文 子序列。 返回删除给定字符串中所有字符(字符串为空)的最小删除次数。 「子序列」定义:如果一个字符串可以通过删除原字符串某些字符而不改变原字符顺序得到,那么这个字符串就是原字符串的一个