
Leetcode
dtwd886
这个作者很懒,什么都没留下…
展开
-
leetcode30——串联所有单词的字串
滑动窗口的思想,将所有子串分为单词长度个元组,作为遍历的外层循环。内层循环跨度为单词的长度class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer>result=new ArrayList<Integer>(); int wordNum=words.length; if(word原创 2021-05-11 20:47:51 · 169 阅读 · 1 评论 -
智力题_环回到原点问题
一个环上有10个点,编号为0-9,从0点出发,每步可以顺时针到下一个点,也可以逆时针到上一个点,求:经过n步回到0点有多少种不同的走法?举例:如果n=1,则出发只能到1或者9,不可能回到0,共0种走法。如果n=2,则从0出发有4条路径:0->1->0,0->1->2,0->9->8,0->9->0,其中有两条回到了0点,故有两种走法。思路:考虑用动态规划的思想,只可能从左边或者右边相邻点回到原点,即先到旁边的点,看看有多少回来的方法。所原创 2020-10-25 22:21:11 · 2159 阅读 · 0 评论 -
二叉树的前中后序遍历的非递归解法
二叉树的前序遍历非递归解法:思路:需要借助于一个辅助栈。遍历每个节点时需要把其右子树节点放入到栈中,然后将当前结点的值存入结果中,然后递归遍历其左子树,如果左子树为空,则从栈中取出栈顶结点。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), le原创 2020-10-19 11:31:55 · 247 阅读 · 0 评论 -
leetcode145——二叉树的后序遍历序列(非递归解法)
题目链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/submissions/思路:以栈的方式显示模拟/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), lef原创 2020-10-17 20:58:26 · 112 阅读 · 0 评论 -
leetcode54——螺旋矩阵
题目链接:思路:模拟打印,left维护初始打印列,right维护最后一列,top维护第一行,bottom维护最后一行。首先打印第一行全部元素,然后打印最后一个元素对应的所在列剩余元素m-1(m为当前打印的列的元素个数)。然后打印最后一行n-2元素(n为当前打印的第一行的元素个数),最后打印当前列剩余m-1个元素(当且仅当right>left&&top>bottom时才有第三步第四步的打印)。每轮打印完毕left++,right--,top++,bottom--cla原创 2020-10-17 20:15:01 · 170 阅读 · 0 评论 -
leetcode437——前缀和
题目链接:https://leetcode-cn.com/problems/path-sum-iii/思路:以map来记录路径上结点的前缀和,key代表着路径上的前缀和,val代表着当前结点路径上的前缀和的个数针对每个结点记录前缀和,并判断之前在路径上的前缀和cur_sum-target的个数,加上这个数,并将当前结点前缀和cur_sum加入到map中,使val加一。然后递归加上左右结点产生的值为target的路径个数。最后将当前结点前缀和对应val减一。/** * Defini原创 2020-10-12 15:46:28 · 175 阅读 · 0 评论 -
leetcode621——优先队列的思路
题目链接:https://leetcode-cn.com/problems/task-scheduler/思路:数量多的先安排任务,少的后安排,每次仅仅采用每类任务个数减1的方式class Solution {public: struct node { node():ch('.'),count(0){} node(char c,int n):ch(c),count(n) { } bool frien原创 2020-10-04 21:57:32 · 824 阅读 · 0 评论 -
leetcode448——巧用符号标注已出现数字
题目链接:https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/思路:对于出现的数字将其标注为负数,if(nums[abs(nums[i])-1]>0)nums[abs(nums[i])-1]*=-1注意:1.因为可能本身该位数字已经为负数,只有加上abs才能找到正确置为负数的下标。2.本身值为负数代表该位数字已出现过,无需再乘以-1class Solution {public:原创 2020-09-25 21:27:28 · 143 阅读 · 0 评论 -
找出升序排序数组第一个小于目标值的所有下标,第一个大于目标值的所有下标——四次二分搜索
题目:找出升序排序数组第一个小于目标值的所有下标,第一个大于目标值的所有下标——四次二分搜索思路:1. 第一次二分:找目标值的最左下标2. 第二次二分:找小于目标值的第一个数的值的最左边界3. 第三次二分:找第一个大于目标值的下标4. 第四次二分:找第一个大于(第一个大于目标值的值)的下标例如:{-1,2,2,3,3,3,5,5,5,6,7,8,10}; //结果应该为 1,2,6,8#include <iostream>#include <cstdio..原创 2020-09-25 12:12:34 · 1263 阅读 · 0 评论 -
leetcode34——两遍二分法
题目链接:https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/submissions/第一遍找左边第一个目标值,第二遍找大于目标值的第一个值得位置class Solution {public:int findIndex(vector<int>& nums,int target,bool flag){ int len=nums.size.原创 2020-09-22 19:27:58 · 1393 阅读 · 0 评论 -
leetcode68——二叉树的最近公共祖先
题目链接:https://leetcode-cn.com/problems/er-cha-shu-de-zui-jin-gong-gong-zu-xian-lcof/对于每个结点讨论四种情况:1.左子树不存在结点为nullptr,以 left来标记。右子树不存在结点为nullptr,以right来标记。那么返回nullptr。2.left和right均不为空,返回当前结点3.left为空,而right不会空,细分两种情况:一个结点在右子树,则right指向p或q,两个结点在右子树中,原创 2020-09-03 18:23:15 · 102 阅读 · 0 评论 -
leetcode84——柱状图的最大矩形
单调栈的方法,每次弹出大于等于当前需要入栈的元素,且最后的栈是顺序递增的,需要将所有元素依次弹出,这个时候每个弹出的元素所对应的最大面积为index=stk.top(),stk.pop(),result_max=max(result_max,(len-stk.top()-1)*heights[index])class Solution {public: int largestRectangleArea(vector<int>& heights) { int原创 2020-08-28 14:00:54 · 280 阅读 · 0 评论 -
leetcode647——回文字符串的中心扩展法
对于长度为n的字符串,只需遍历0-2*n-2,中心为left=n/2,right=n/2+1然后只用针对每个中心一层循环遍历,时间复杂度为O(n^2),就把奇数偶数情况统一起来了。class Solution {public: int countSubstrings(string s) { int len=s.size(); int result=0; for(int i=0;i<2*len-1;i++) {原创 2020-08-19 17:34:10 · 197 阅读 · 0 评论 -
Leetecode——面试题 16.25. LRU缓存
常考。最初建立头部尾部哑结点,两者相互指向。注意的是push时本身有key是应该对其进行更新,并将其移到头部。不然再根据是否容量足够来判断是否需要删除尾部结点,然后将其放置到头部哑结点后面一位。get时有对应结点需要将当前结点从结点位置移动到头部后一个结点。class LRUCache {private: int _capacity; int count_node=0; struct Node { Node(int _key,int _val)原创 2020-08-05 14:29:10 · 238 阅读 · 0 评论 -
leetcode470——用 Rand7() 实现 Rand10()
题目链接:https://leetcode-cn.com/problems/implement-rand10-using-rand7/思路:首先利用(rand7()-1)*7 随机生成等间隔的数据0,7,14,21,28,35,42然后利用rand7()在生成数据将数据插入到第一步生成的数据中,保证随机性。从而得到1,2,3,...,49对于大于40的数据重新生成,而对于小于的数据对十取模加一,这样保证了1-40之间数据的概率是完全相同的。// The rand7() API.原创 2020-08-02 17:32:05 · 217 阅读 · 1 评论 -
剑指 Offer 40. 最小的k个数
题目链接:https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/思路:快速排序的变形方法一:class Solution {public: vector<int>result; int findNum(vector<int>& arr,int k,int left,int right) { int temp=arr[left]; while原创 2020-07-27 12:57:32 · 98 阅读 · 0 评论 -
leetcode 5474——好叶子节点对的数量
题目链接:https://leetcode-cn.com/problems/number-of-good-leaf-nodes-pairs/思路:做题时,想的是记录每个点的父节点来回溯找到每个节点的第一个公共祖先,这样的时间复杂度是O(n^2),太大了。可以考虑以空间换时间的方式,针对每个叶子结点记录一条路径。找两个结点的最低公共祖先,每两个的时间复杂度为O(n)/** * Definition for a binary tree node. * struct TreeNode {原创 2020-07-26 19:38:21 · 299 阅读 · 0 评论 -
leetcode42——接雨水
题目链接:https://leetcode-cn.com/problems/trapping-rain-water/思路:方法1:暴力(超时了)遍历每个下标,找左边和右边的最大值max_left,max_right,并取两者的最小值Min,将Min-height[j]和0的最大值加到result上即可class Solution {public: int trap(vector<int>& height) { int len=height.si原创 2020-07-24 17:57:44 · 135 阅读 · 0 评论 -
leetcode26——删除排序数组中的重复项
题目链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/方法一:只用erase:其返回值指向删除元素的第一个下标class Solution {public: int removeDuplicates(vector<int>& nums) { int len=nums.size(); if(len==0)return 0;原创 2020-07-22 13:19:37 · 120 阅读 · 0 评论 -
leetcode16——带绝对值的最接近三数之和
题目链接:https://leetcode-cn.com/problems/3sum-closest/solution/zui-jie-jin-de-san-shu-zhi-he-by-leetcode-solution/思路:借助双指针对枚举进行优化。如果pb,pc当前指向元素大于target,则将pc向左移动,因为以pc为第三个数的指针的pa,pb,pc已经指向最接近target的值。如果小于target,则将pb向右移动,因为以pb为第二个数指针的当前三指针已经达到最接近target。即如原创 2020-06-24 23:05:13 · 204 阅读 · 0 评论 -
剑指offer20题——leetcode主站65题
题目链接:https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/思路:1. 首先去除字符串首尾字符2. 根据小写e划分指数和底数3. 分别对指数和底数判断是否合法即可class Solution {public: bool judgeDigit(string s) { int len=s.size(); bool flag_has_digit=fals原创 2020-06-19 21:34:05 · 156 阅读 · 0 评论 -
leetcode1014——最佳观光组合
题目链接:https://leetcode-cn.com/problems/best-sightseeing-pair/思路:不能采用两重循环暴力枚举,会超时,而应该将A[i]+A[j]+i-j拆分为两部分,A[i]+i,A[j]-j两部分(i<j)。这样单重循环枚举j,保留j之前的最大的A[i]+i,为max_i_part,然后每次取max_i_part+A[j]-j当前的最大值。class Solution {public: int maxScoreSightseeingP原创 2020-06-18 11:50:08 · 181 阅读 · 0 评论 -
Leetcode1300—— 转变数组后最接近目标值的数组和
考点:双重二分函数:max_element(vec.begin(),vec.end())返回指向最大元素的迭代器lower_bound(vec.begin(),vec.end(),val)返回指向排好序数组的大于等于val的第一个元素的迭代器lower_bound(vec.begin(),vec.end(),val)返回指向排好序数组的大于val的第一个元素的迭代器class Solution {public: int findBestValue(vector<int&原创 2020-06-14 23:34:28 · 89 阅读 · 0 评论 -
Leetcode15——三数之和为0
伴随着第二个数递增时,第三个数递减,所以采用双指针:class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { int len=nums.size(); sort(nums.begin(),nums.end()); vector<vector<int>>result; for原创 2020-06-13 12:48:59 · 184 阅读 · 0 评论 -
leetcode990——并查集
class Solution {private: int fa[26];public: int DFS(int x) { if(fa[x]!=x) fa[x]=DFS(fa[x]); return fa[x]; } bool equationsPossible(vector<string>& equations) { int len=equations.size();.原创 2020-06-09 17:13:05 · 144 阅读 · 0 评论 -
Leetcode面试题9——根据前序和中序遍历序列来建立二叉树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* constructTree(vector.原创 2020-06-07 13:51:24 · 291 阅读 · 0 评论 -
Leetcode66——对称二叉树
一个树为对称的仅当两棵子树互为镜像,即要满足:1.子树根节点值相等2.每个树的右子树都与另一个树的左子树镜像对称。有递归迭代两种解法:递归:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), righ原创 2020-05-31 17:19:16 · 142 阅读 · 0 评论 -
Leetcode560——和为K的子数组
给定一个整数数组和一个整数k,你需要找到该数组中和为k的连续的子数组的个数。输入:nums = [1,1,1], k = 2输出: 2 , [1,1] 与 [1,1] 为两种不同的情况。说明 : 数组的长度为 [1, 20,000]。 数组中元素的范围是 [-1000, 1000] ,且整数k的范围是[-1e7, 1e7]。 第一种做法:直接两层循环枚举(而不需要三层),因为一个范围内的大小可以简化为该数加上前一范围的和。class Solution {pub...原创 2020-05-15 20:48:40 · 109 阅读 · 0 评论 -
Leetcode84——柱状图中最大的矩形
题目描述:给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。求在该柱状图中,能够勾勒出来的矩形的最大面积。方法一:暴力解法O(N^2)复杂度,超时,仅提供思路(踩坑)直接枚举每一段,找最小。class Solution {public: int largestRectangleArea(vector<int>&...原创 2020-05-01 14:19:46 · 233 阅读 · 0 评论 -
Leetcode——round176——1004
题目链接:https://leetcode-cn.com/problems/construct-target-array-with-multiple-sums/解题思路:设当前数组的最大值为cur_max。 数组的和为sum,那么将最大值替换成cur_max-(sum-cur_max),解法是将最大值不断进行替换直到数值全为1。可以考虑优化效率:当第二小数为1时,判断(cur_max-...原创 2020-02-16 20:45:13 · 101 阅读 · 0 评论 -
LeetcodeRound171——1003并查集模板题
class Solution {public: int x[100010]; int DFS(int pos) { if(x[pos]!=pos) { x[pos]=DFS(x[pos]); return x[pos]; } return pos; ...原创 2020-01-12 12:03:11 · 226 阅读 · 1 评论 -
leetcode1307——深度优先搜索
变量的含义:chars用来保存从低位到高位每一位未确定的字符equations用来记录每一层中每一个字母对应的加减个数标志将result也push_back到words的动态数组中,最后直接减去result中的当前位用以判断余数是否为0num_to_ch 用以保存从数字到单词的映射,ch_to_num用以保存从单词到数字的映射zeroFlag记录不能为0的所有单词开头字母思...原创 2020-01-07 10:03:05 · 255 阅读 · 0 评论 -
1297. 子串的最大出现次数
题目链接:https://leetcode-cn.com/problems/maximum-number-of-occurrences-of-a-substring/comments/class Solution {public: int maxFreq(string s, int maxLetters, int minSize, int maxSize) { ...原创 2019-12-25 10:26:12 · 257 阅读 · 0 评论