力扣
从小白入门类的题型到中高阶难度的题,目标:不少于200道经典算法题
九久呀
一名计算机系学生的博客
展开
-
力扣【动态规划】-中等题-买卖股票含手续费
题目链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/只需要在卖出的时候扣除手续费即可。#include<bits/stdc++.h>using namespace std;class Solution {public: int maxProfit(vector<int>& prices, int fee) { in原创 2022-01-27 16:00:38 · 688 阅读 · 0 评论 -
力扣【动态规划】买卖股票含有冷冻期
题目链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/注意是卖掉之后有一天的冷冻期。换句话说就是,如果想买股票,那么得提前两天把股票卖出去。#include<bits/stdc++.h>using namespace std;class Solution {public: int maxProfit(vector<int>& prices) {原创 2022-01-27 15:51:07 · 611 阅读 · 0 评论 -
力扣【动态规划】-中等题-122买卖股票时期
题目链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/不要收到题目交易次数的影响题目分析:代码:#include<bits/stdc++.h>using namespace std;class Solution {public: int maxProfit(vector<int>& prices) { int len = prices.size(原创 2022-01-27 11:26:56 · 697 阅读 · 0 评论 -
力扣【动态规划】121题. 买卖股票时期
题目连接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/这道题有个关键问题需要明白:就是交易次数由于本题中K要么是0,要么是1。0表示没有进行过交易,1表示进行过交易。那么当持有股票的时候,要么是上一次持有股票,要么是第一次持有股票。代码:#include<bits/stdc++.h>using namespace std;class Solution {public: int maxPro原创 2022-01-26 23:15:51 · 356 阅读 · 0 评论 -
力扣【动态规划】-中等题-乘积最大子数组
题目链接:https://leetcode-cn.com/problems/maximum-product-subarray/为了解决负号带来的影响,在寻找最大乘积的时候需要用到另外一个数组存放遇到的最小值(当前最小值不一定负数,但是如果当前最小值为负数的话一定可以放进最小数组中,那么如果下一个值也为负数的话最小数组存放的就是结果)。所以本题最大乘积有三个来源:1、前一个最大乘积2、前一个最大当前数值3、前一个最小当前数值代码:#include<bits/stdc++.h>usi原创 2022-01-23 15:29:49 · 241 阅读 · 0 评论 -
力扣【动态规划】-中等题-跳跃游戏
题目链接:https://leetcode-cn.com/problems/jump-game/这道题有两种解题思路,一种是官方给出的解答方案 通过整个数组能够跳跃的最大距离进行判断,另一种是我个人的解答方案 根据下一步能够跳跃的最大距离。方案一:按照官方解题思路,只需要在最后一个数之前找到大于等于整个数组长度的数即可。代码:#include <bits/stdc++.h>using namespace std;class Solution {public: bool原创 2022-01-19 12:47:06 · 2161 阅读 · 0 评论 -
力扣【动态规划】-中等题-删除并获得点数
题目连接:https://leetcode-cn.com/problems/delete-and-earn/数组类型的子问题考虑dp[i]和dp[i-1],dp[i-2]的关系代码:#include<bits/stdc++.h>#include <iostream>using namespace std;class Solution {public: int deleteAndEarn(vector<int>& nums) {原创 2022-01-17 20:01:11 · 418 阅读 · 0 评论 -
力扣【动态规划】-简单-1137. N-th Tribonacci Number
The Tribonacci sequence Tn is defined as follows:T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.Given n, return the value of Tn.Example 1:Input: n = 4Output: 4Explanation:T_3 = 0 + 1 + 1 = 2T_4 = 1 + 1 + 2 = 4Example 2:Input: n原创 2021-11-15 15:24:20 · 245 阅读 · 0 评论 -
力扣【动态规划】-简单-509. Fibonacci Number
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,F(0) = 0, F(1) = 1F(n) = F(n - 1) + F(n - 2), for n > 1.Given n, c原创 2021-11-15 11:25:57 · 603 阅读 · 0 评论 -
力扣------环形子数组最大和
给定一个由整数数组 A 表示的环形数组 C,求 C 的非空子数组的最大可能和。在此处,环形数组意味着数组的末端将会与开头相连呈环状。(形式上,当0 <= i < A.length 时 C[i] = A[i],且当 i >= 0 时 C[i+A.length] = C[i])此外,子数组最多只能包含固定缓冲区 A 中的每个元素一次。(形式上,对于子数组 C[i], C[i+1], …, C[j],不存在 i <= k1, k2 <= j 其中 k1 % A.length =原创 2021-10-14 14:58:50 · 236 阅读 · 1 评论 -
力扣---罗马数字转整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D 500M 1000For example, 2 is written as II i原创 2021-10-01 16:35:05 · 159 阅读 · 0 评论 -
力扣 --- 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatica原创 2021-09-30 18:06:08 · 188 阅读 · 0 评论 -
力扣 旋转链表
题目链接https://leetcode-cn.com/leetbook/read/linked-list/f00a2/思路比较简单 要么在原有链表上进行操作,要么重新建立一个链表。注意K的大小和链表长度之间的关系,小心超时/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* rota原创 2021-08-03 21:16:16 · 90 阅读 · 0 评论 -
力扣 两数相加
题目链接:https://leetcode-cn.com/leetbook/read/linked-list/fv6w7/这道题就是基本的大整数相加,只不过换成了链表的形式代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* addTwoNumbers(struct List原创 2021-07-29 13:51:01 · 126 阅读 · 0 评论 -
力扣 回文链表
题目链接:https://leetcode-cn.com/leetbook/read/linked-list/fov6t/时间复杂度:o(n),空间复杂度o(1)思想:1、遍历链表,获取链表总长度2、将链表前半段进行反转,这里记住一点:链表长度为偶数时,前半段和后半段长度相等;链表长度为奇数时,前半段要比后半段少一(这里选择了少一,其实前半段比后半段多一可以解答)3、如果链表长度是奇数,那么将多一的那个链表取next,保持两个链表长度相等。因为如果是奇数,那么中间的数不影响奇偶性代码:/**原创 2021-07-27 22:06:41 · 143 阅读 · 0 评论 -
力扣 奇偶链表
题目链接:https://leetcode-cn.com/leetbook/read/linked-list/fe0kj/这道题意思说是第i个链表结点,如果i是奇数,那么i位于前面的链表,如果i是偶数则位于后面的链表,代码如下:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* od原创 2021-07-25 21:02:55 · 104 阅读 · 0 评论 -
力扣 移除元素
题目链接:https://leetcode-cn.com/leetbook/read/linked-list/f9izv/思路并不难:重新建立新的链表代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* removeElements(struct ListNode* head,原创 2021-07-25 20:34:57 · 87 阅读 · 0 评论 -
力扣 反转链表 人生苦短,我用python
这是一道很简单的链表题目,却让我开始反省自己对于c++的掌握程度。题目链接:https://leetcode-cn.com/leetbook/read/linked-list/f58sg/下面是c艹代码,一直报错却找不到报错的原因。以及关于改算法是否正确,特意去参考了408上是怎么写的,发现无误后依旧在力扣上面报错,开始怀疑人生。#include <bits/stdc++.h>/** * Definition for singly-linked list. * struct Lis原创 2021-07-25 16:23:29 · 128 阅读 · 0 评论 -
力扣 删除链表的倒数第N个结点
Given the head of a linked list, remove the nth node from the end of the list and return its head.Example 1:Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5]Example 2:Input: head = [1], n = 1Output: []Example 3:Input: head = [1,2], n = 1Output: [原创 2021-07-24 18:05:18 · 132 阅读 · 0 评论 -
力扣 相交链表
题目链接:https://leetcode-cn.com/leetbook/read/linked-list/jjbj2/这道题不难思考到的一点是:只要两个链表相交那么之后的节点都是相同的。所以A和B链表在公共区域之前的长度,就是个问题。这道题的突破点在于,A+B的长度和B+A的长度是一致的。有句话说的很好,我走过你走的路,那么我们会不会相交。/** * Definition for singly-linked list. * struct ListNode { * int val;原创 2021-07-24 17:46:12 · 115 阅读 · 0 评论 -
力扣--乘积最大子数组
Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.It is guaranteed that the answer will fit in a 32-bit integer.A subarray is a contiguous subsequence of the array.Exa原创 2021-07-08 22:27:13 · 141 阅读 · 0 评论 -
力扣 俄罗斯套娃信封问题
You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope.One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope’原创 2021-07-07 21:59:17 · 147 阅读 · 2 评论 -
力扣--最长递增子序列的个数
Given an integer array nums, return the number of longest increasing subsequences.Notice that the sequence has to be strictly increasing.Example 1:Input: nums = [1,3,5,4,7]Output: 2Explanation: The two longest increasing subsequences are [1, 3, 4, 7]原创 2021-07-04 21:28:30 · 116 阅读 · 0 评论 -
力扣--最长上升子序列
题目:Given an integer array nums, return the length of the longest strictly increasing subsequence.A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For examp原创 2021-06-22 21:39:54 · 126 阅读 · 0 评论 -
力扣53.最大子序和
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.代码:#include <bits/stdc++.h>class Solution {public: int maxSubArray(vector<int>& nums) {原创 2021-06-22 21:23:05 · 60 阅读 · 0 评论 -
力扣--将有序数组转化为平衡二叉搜索树
Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than原创 2021-06-06 13:04:40 · 113 阅读 · 0 评论 -
二叉树的层序遍历
Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).Example 1:Input: root = [3,9,20,null,null,15,7]Output: [[3],[9,20],[15,7]]Example 2:Input: root = [1]Output: [[1]]Exam原创 2021-06-06 12:02:59 · 83 阅读 · 0 评论 -
对称二叉树
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).Example 1:Input: root = [1,2,2,3,4,4,3]Output: trueExample 2:Input: root = [1,2,2,null,3,null,3]Output: falseConstraints:The number of nodes原创 2021-06-06 12:00:46 · 91 阅读 · 0 评论 -
力扣--有效的数独
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition.Each column must contain the digits 1-9 without repetition.Each of the nine 3原创 2021-06-06 11:57:54 · 184 阅读 · 0 评论 -
力扣--合并两个有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has a size equal to m + n such that it has enough space t原创 2021-05-26 22:43:16 · 191 阅读 · 0 评论 -
力扣--存在重复元素II
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.Example 1:Input: nums = [1,2,3,1], k = 3Output: trueExample 2:Input: nums = [1,0,1,原创 2021-04-07 15:58:01 · 131 阅读 · 0 评论 -
力扣--哈希表--两个列表的最小索引总和
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.You need to help them find out their common interest with the least list index sum. If there is a choice tie between.原创 2021-04-02 18:10:34 · 158 阅读 · 0 评论 -
力扣--两数之和
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer原创 2021-04-01 21:15:35 · 183 阅读 · 0 评论 -
力扣---快乐数
快乐数挺快乐Write an algorithm to determine if a number n is happy.A happy number is a number defined by the following process:Starting with any positive integer, replace the number by the sum of the squares of its digits.Repeat the process until the number原创 2021-04-01 20:39:46 · 196 阅读 · 0 评论 -
力扣---两个数组的交集
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.题目:给定两个数组,寻找两个数组中相同的元素,如果该元素多次出现只需要记录一次即可,输出结果的顺序可以与样例中给的顺序不同。Example 1:Input: nums1 =原创 2021-04-01 18:51:41 · 197 阅读 · 0 评论 -
力扣 设计哈希集合
自己动手写一写发现收获其实还蛮大的。#define MAXLENGTH 10000class MyHashSet {private: vector<int> set[MAXLENGTH]; /*返回键的下标位置*/ int getIndex(int key){ return key%MAXLENGTH; } /*查找key对应的value*/ int getPos(int key,int index){ fo原创 2021-04-01 10:33:38 · 128 阅读 · 0 评论 -
力扣链表合并两个有序链表
Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.应该考虑到链表是否为空的情况,代码如下:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNod原创 2021-03-29 17:35:12 · 81 阅读 · 0 评论 -
力扣链表:两两交换链表中的节点
写了一些关于力扣的链表的题,说明一下需要注意的地方:1、力扣链表无头结点,所以想要正常操作时需要自己添加一个头结点 2、最后一个节点的next指针指向NULL代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) :.原创 2021-03-27 14:34:41 · 187 阅读 · 0 评论 -
链表:寻找中间结点
代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* middleNode(ListNode* head) { ListNode *chaste.原创 2021-03-27 10:00:55 · 121 阅读 · 0 评论 -
力扣初级算法--数组 加一
题目:给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。示例 1:输入:digits = [1,2,3]输出:[1,2,4]解释:输入数组表示数字 123。示例 2:输入:digits = [4,3,2,1]输出:[4,3,2,2]解释:输入数组表示数字 4321。示例 3:输入:digits = [0]输出:[1]提示:1 <= di原创 2021-03-25 18:07:27 · 174 阅读 · 0 评论