![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
不错的基础题
cc 提升ing 变优秀ing
有需要搜广推nlp实习生的部门吗?
展开
-
leetcode:1030. 距离顺序排列矩阵单元格(简单,排序,广搜)
题目: 分析: 看完题目觉得是广搜,但是总感觉应该是有特殊的,没想到直接是如下这个图: 看下图,每个点都对应着四个点,每一次一条边上的和相等。 代码: 一直出错,先放弃了~ 方法二:也可以当做排序来做,python3的排序如下: class Solution: def allCellsDistOrder(self, R: int, C: int, r0: int, c0: int) -> List[List[int]]: ret = [(i, j) for i in ra原创 2021-03-22 10:05:43 · 135 阅读 · 3 评论 -
leetcode:1027. 最长等差数列(dp,中等)
题目: 分析: 我的想法:A【i】【j】是 以第i个元素为结尾时,公差为j,应该设立两个,因为公差还有可能是负的。 然后每次遍历i之前的就可以啦! 代码: class Solution { public: int longestArithSeqLength(vector<int>& A) { if(A.size()==0) return 0; if(A.size()==1) return 1; int maxx=A[0],minn原创 2021-02-18 22:24:52 · 85 阅读 · 0 评论 -
leetcode:1020. 飞地的数量(中等,dfs)
题目: 分析: 虽然是一道水题。但是还是有技巧的,从四个方向进行搜索即可。 代码: class Solution { public: void f(vector<vector<int>>& A,int x,int y,int len1,int len2) { if(x<0||x>len1) return ; if(y<0||y>len2) return ; if(A[x][y]==0)原创 2021-02-18 13:35:23 · 101 阅读 · 0 评论 -
leetcode:1021. 删除最外层的括号(简单)
题目: 分析: 虽然很简单,但是觉得自己的思路以及代码质量都是不错的。 代码: class Solution: def removeOuterParentheses(self, S: str) -> str: stack = [] i = 1 s='' cnt1=0 cnt2=0 while i < len(S): if cnt1==cnt2原创 2021-02-16 08:21:58 · 114 阅读 · 0 评论 -
leetcode:面试题 16.16. 部分排序(排序,双指针,中等)
题目: 分析: 自己想到了先找不按照熟顺序的,然后两边不断移动。 最简单的方法当然是排序啦! 代码1.排序水题: class Solution { public: vector<int> subSort(vector<int>& array) { vector<int> v=array; sort(v.begin(),v.end()); vector<int> ans={-1,-1};原创 2020-12-29 17:46:42 · 233 阅读 · 0 评论 -
leetcode:面试题 16.10. 生存人数(差分,中等)
题目: 分析: 不错,很快就想到了差分。 代码: class Solution { public: int maxAliveYear(vector<int>& birth, vector<int>& death) { map<int,int> m; int min_=2000,max_=1000; for(int i=0;i<birth.size();i++) {原创 2020-12-28 15:32:58 · 167 阅读 · 0 评论 -
leetcode:211周赛:5210. 球会落何处(中等)
题目: 分析: 模拟过程,再加一个记忆化搜索,别被题目吓住了。 代码: class Solution { public: vector<vector<int> > vv; vector<int> ans; int f(int x,int m,vector<vector<int>>& grid) {//处于x位置 m行 if(m==grid.size()) return x;原创 2020-12-27 12:01:43 · 102 阅读 · 0 评论 -
leetcode:剑指 Offer 46. 把数字翻译成字符串(字符串,中等)
题目: 分析: 从数字角度写的,代码写的漂亮啊 代码: class Solution { public: int ans; void f(int num) { if(num==0) { ans++;return ; } f(num/10); if(num>=10&&num%100<26&&num%100>=10) f(num/100);原创 2020-12-21 17:11:02 · 72 阅读 · 0 评论 -
leetcode:剑指 Offer 38. 字符串的排列(回溯)
题目: 代码: class Solution { public: vector<string> ans; void f(string s,string a,vector<int> v) {//a 当前 if(s.length()==a.length()) { ans.push_back(a);return; } int last=' '; for(int i=0;i<原创 2020-12-21 16:23:01 · 66 阅读 · 0 评论 -
leetcode:剑指 Offer 34. 二叉树中和为某一值的路径(树,中等)
题目: 分析: 如果做题不认真,不思考,还不如不做。 本题和主站上有一道题是一样的,测试后发现自己犯了和上次一样的一个错误。 如果到NULL才去判断,那么会导致: 1.可能有一个子树的结点会被放进去。 2.满足条件的被放两次。 特错大错的代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *原创 2020-12-17 15:08:08 · 80 阅读 · 0 评论 -
leetcode:剑指 Offer 26. 树的子结构(树,中等)
题目: 分析: 就是不认真,小错误太多了吧!!! 刚开始忽略了B终止,而A还可以继续的情况。 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class So原创 2020-12-16 11:46:51 · 98 阅读 · 0 评论 -
leetcode:剑指 Offer 07. 重建二叉树(树,)
题目: 分析: 注意看清題,人家要的是重构一颗二叉树。 代碼: /** * 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:原创 2020-12-14 09:38:14 · 73 阅读 · 0 评论 -
leetcode:面试题 10.09. 排序矩阵查找(二分,中等)
题目: 分析: 让我痛过的题目 代码: class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { //大于等于tar的第一个元素 if(matrix.size()==0||matrix[0].size()==0) return 0; if(matrix[matrix.size()-1][matrix[0].原创 2020-12-11 10:59:11 · 108 阅读 · 0 评论 -
leetcode:剑指 Offer 53 - II. 0~n-1中缺失的数字(简单,二分)
题目: 分析: 基础二分,但是不用库函数的话,如果面试,自己又凉凉了。 代码: class Solution { public: int missingNumber(vector<int>& nums) { int a=0,b=nums.size()-1; int ans=-1; while(a<=b) { int c=a+(b-a)/2; //cout<原创 2020-12-11 10:27:20 · 74 阅读 · 0 评论 -
leetcode:491. 递增子序列(中等,剪枝)
题目: 分析: 因为要具体的内容,所以必须要搜索,但是因为不能排序,所以搜索时应该引入一个set,来保证不重复的现象。 写的还是不错的,能对原来的模板进行适时修改: 代码: class Solution { public: vector<vector<int>> vv; void f(vector<int>& nums,vector<int> v,int x) { if(x==nums.size())原创 2020-12-10 10:28:45 · 114 阅读 · 1 评论 -
leetcode:495. 提莫攻击(中等,数组)
题目: 分析: 天呐,看到lol激动到直接秒杀了该题(虽然本题本来就很简单) 每次射击的中毒时间取决于下次时间与本次设计时间差 和 中毒最长时间的最小值。 代码: class Solution { public: int findPoisonedDuration(vector<int>& timeSeries, int duration) { int ans = 0; if(timeSeries.size()==0) return 0;原创 2020-12-09 16:42:56 · 89 阅读 · 0 评论 -
leetcode:475. 供暖器(中等,数组)
题目: 分析: 先想出来,再看标签。 二分有点复杂。 想sort之后,取暖器也一遍遍历作出,结果把自己给绕哭。 状态真的很重要,一道简单题,依然没有写出来。 class Solution { public: int findRadius(vector<int>& houses, vector<int>& heaters) { sort(houses.begin(),houses.end()); sort(heaters.be原创 2020-12-08 17:38:08 · 167 阅读 · 1 评论 -
leetcode:437. 路径总和 III(树,前缀和,中等)
題目: 分析: 前缀和问题、 代码: /** * 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),原创 2020-12-08 16:28:25 · 105 阅读 · 0 评论 -
leetcode:1541. 平衡括号字符串的最少插入次数(中等,括号问题)
题目: 分析: 属于一般的括号问题,正着的的是统计(的个数,倒着的话,是统计)的个数, 代码: class Solution { public: int minInsertions(string s) { int c=0;//( int ans=0; int ok=1; for(int i=0;i<s.size()-1;i++) { if(s[i]=='(') c++;原创 2020-12-07 18:59:10 · 179 阅读 · 0 评论 -
leetcode:463. 岛屿的周长(数组,简单)
題目: 分析: 虽然是简单题,自己想的也有点过于复杂,看了题解。 对于单独的格子而言,周长是4,但是当两个格子相邻时,会各自损失1的周长。 所以问题转化为 找出 岛屿格子 之间的 相邻边。 代碼: class Solution { public: int islandPerimeter(vector<vector<int>>& grid) { int all=0;//1的个数 int n=0;//临边 if(grid.原创 2020-12-07 10:08:18 · 94 阅读 · 0 评论 -
leetcode:周赛:K 和数对的最大数目(双指针,)
题目: 分析: 看完题想了一会,就差不多想到了双指针,一道不错的双指针题目。 刚开始想的时候感觉 固定前面的指针,后面的指针向前移动 或者 固定后面的指针,前面的指针向前移动 但是提交后根据错误提示发现。 两个要结合,当前后相加大于目标值,应该缩小,则后面的往前移动。 当小于目标值的时候,应该增大,则前面的应该往后移动。 虽然能想出来,但是希望下次思路更加全面。 代码: class Solution { public: int maxOperations(vector<int>&原创 2020-12-06 13:09:58 · 147 阅读 · 0 评论 -
leetcode:450. 删除二叉搜索树中的节点(树,中等)
题目: 分析: 本属于数据结构的基本问题(虽然已经忘完了),值的回顾一下。 分为三种情况: 1.根节点直接删除。 2.只有左子树或者右子树的直接删掉拼接。 3.既有左子树又有右子树的,则把左子树的最大值拿过来,或者是右子树的最小值拿过来! 虽然简单,但是写起来技巧性挺多的。 代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeN原创 2020-12-04 16:35:39 · 70 阅读 · 0 评论