- 博客(125)
- 问答 (2)
- 收藏
- 关注
原创 Leetcode——563. 二叉树的坡度
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla...
2020-04-24 10:18:32 167
原创 Leetcode——653. 两数之和 IV - 输入 BST
题目传送门思路:遍历一下树(我这里用的层序遍历),然后将目标值k-当前节点的值所得到的差值存入unordered_set这个hashset,然后遍历的时候验证当前节点是否在hashset里面,如果存在的话说明这个节点就是之前期望的差值。/** * Definition for a binary tree node. * struct TreeNode { * int val;...
2020-04-23 09:10:40 177
原创 Leetcode——606. 根据二叉树创建字符串
题目传送门/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *...
2020-04-22 15:18:04 152
原创 Leetcode——538. 把二叉搜索树转换为累加树
思路:类似于中序遍历但是是右子树->根节点->左子树这样来。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), le...
2020-04-21 16:00:51 189
原创 Leetcode——501. 二叉搜索树中的众数
思路:中序遍历后对遍历得到的数组进行计数/排序。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), rig...
2020-04-20 10:14:51 237
原创 Leetcode——108. 将有序数组转换为二叉搜索树
思路:中序遍历、递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} *...
2020-04-19 10:54:29 97
原创 Leetcode——530. 二叉搜索树的最小绝对差
对于二叉搜索树,中序遍历得到的结果就是一个递增的数列/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r...
2020-04-18 10:30:33 110
原创 Leetcode——107. 二叉树的层次遍历 II
思路:1.递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }...
2020-04-17 10:54:20 147
原创 Leetcode——572. 另一个树的子树
题目传送门思路:递归c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {...
2020-04-16 09:13:39 107
原创 Leetcode——445. 两数相加 II
思路:利用栈来解决链表反转简洁版class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { stack<int> s1, s2; while (l1) { s1.push(l1 -> val); ...
2020-04-14 10:10:16 124
原创 Leetcode——437. 路径总和 III
思路:递归写法,用一个数组(python用链表)存储之前节点的值,然后对每个节点进行向前回溯。python# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# ...
2020-04-13 10:34:26 129
原创 Leetcode——404. 左叶子之和
c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */...
2020-04-12 11:43:26 85
原创 Leetcode——100. 相同的树
题目传送门思路就是递归比较,这里用python、c++两种语言写的,不得不说,python就是慢很多,我python 32ms超过80%,c++ 4ms超过60%。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNod...
2020-04-11 10:38:39 105
原创 Leetcode——151. 翻转字符串里的单词
题目传送门最近开始转战python了,因为以后工作可能会用python比较多。还是提取熟悉一下python吧。之前一直用c++刷是认为用c++可以更加专注于算法本身,因为python的api实在是太强了!!!class Solution: def reverseWords(self, s: str) -> str: return " ".join(reversed...
2020-04-10 10:43:25 111
原创 Leetcode——24. 两两交换链表中的节点
题目传送门/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListN...
2020-04-09 15:16:58 74
原创 Leetcode——72. 编辑距离
题目传送门推荐看这个题解很精彩下面给出c++写法自顶向下class Solution {public: struct HashPair { size_t operator() (const pair<int, int> &a) const { return a.first * 1000007 + a.second; ...
2020-04-07 11:05:49 112
原创 Leetcode——面试题 01.07. 旋转矩阵
题目传送门思路:先按对角线交换。后每一行以中间的点为对称轴交换。class Solution {public: void rotate(vector<vector<int>>& matrix) { int n=matrix.size(); for(int i=0;i<n;i++) {...
2020-04-07 11:02:53 103
原创 Leetcode——8. 字符串转换整数 (atoi)
题目传送门思路一:就简单的面向过程但是要处理好边界问题比较费劲class Solution{public: int myAtoi(string str) { int strLength=str.length(); int i; for(i=0;i<strLength;i++) { ...
2020-04-03 16:13:57 75
原创 Leetcode——289. 生命游戏
传送门复制原数组模拟class Solution {public: int index1[8]={1,1,1,0,0,-1,-1,-1}; int index2[8]={0,1,-1,1,-1,-1,0,1}; bool isLegal(int x,int y,int boundx,int boundy) { if(x<0||y<...
2020-04-02 11:26:29 169
原创 Leetcode——912. 排序数组
题目传送门介绍两种方法,第一种就是直接调库,第二种是希尔排序。class Solution{public: vector<int> sortArray(vector<int>& nums) { sort(nums.begin(),nums.end()); return nums; }};class...
2020-03-31 09:09:05 87
原创 Leetcode——1162. 地图分析
题目传送门本题的思路为多源bfs,所谓多源,可以想象成有一个创世节点,然后多源的节点均为其子节点,所以多源bfs本质上还是普通的单源bfs,只不过是从第二轮开始的。然后从每个陆地出发,一圈一圈搜,最后搜到的海洋即为最远的海洋。class Solution{public: int maxDistance(vector<vector<int>>& gri...
2020-03-29 15:19:21 99
原创 Leetcode——820. 单词的压缩编码
题目传送门思路一:暴力,将字符串按字符个数从大到小排列,然后判断后续字符串是否是匹配字符串的子串。class Solution{public: int minimumLengthEncoding(vector<string>& words) { string str=""; int lengths=words.size()...
2020-03-28 15:06:26 69
原创 Leetcode——914. 卡牌分组
题目传送门本题思路就是将出现的数字存在unordered_set里面,因为无序集合是hash实现比较快。然后遍历集合,求公因数(在algorithm里面有函数__gcd(a,b))。如果公因数小于2即为1说明不存在解,即时返回错误,当然只有一种数字的情况要单独考虑。class Solution{public: bool hasGroupsSizeX(vector<int>...
2020-03-27 11:05:11 103
原创 Leetcode——999. 车的可用捕获量
题目传送门这个题不难就是读懂题意需要耐心一点。class Solution {public: int north(int i,int j,vector<vector<char>>& board) { for(int a=i;a>=0;a--) { if(board[a][j]=='...
2020-03-26 11:23:20 120
原创 Leetcode——892. 三维形体的表面积
主要说一下怎么理解减去重合的面积:两个长方体相邻,其实要减去的重合面积就是二者高度的最小值*2.可以想象一下。代码有优化空间,比如说两个循环可以合并,但是我感觉这样可读性差一些,所以没有优化。class Solution{public: int surfaceArea(vector<vector<int> >& grid) { ...
2020-03-25 11:39:56 110
原创 Leetcode——面试题 17.16. 按摩师
思路:动态规划。class Solution {public: int massage(vector<int>& nums) { int length=nums.size(); vector<int> dp(length,0); if(length==0) { ...
2020-03-24 11:18:24 125
原创 priority_queue 大/小根堆互换
c++的priority_queue相当于一个大(小)根堆。priority_queue默认是大根堆,即top元素对队列中最大的元素。那么怎么将其变成小根堆呢?下面以int类型为例子。priority_queue<int,vector<int>,greater<int> > p; //小根堆,//默认大根堆为less<int>struct...
2020-03-23 14:05:28 561
原创 Leetcode——面试题40. 最小的k个数
用优先级队列来模拟一个k个元素的大根堆。class Solution{public: vector<int> getLeastNumbers(vector<int>& arr, int k) { if(k==0) { vector<int> re; r...
2020-03-23 13:57:53 92
原创 Leetcode——876. 链表的中间结点(快慢指针法)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* ...
2020-03-23 11:00:37 88
原创 Leetcode——945. 使数组唯一的最小增量
三种方法:提供c++实现,具体思路可看leetcode题解class Solution{public: int minIncrementForUnique(vector<int>& A) { int length=A.size(); int re=0; sort(A.begin(),A.end()); ...
2020-03-22 15:10:09 97
原创 python易误点——类的私有变量
先举一个python类的例子class Student(object): def __init__(self, name, score): self.name = name self.score = score def print_score(self): print('%s: %s' % (self.name, self.sc...
2020-03-21 16:29:46 138
原创 推荐系统——LR模型
LR模型是广义线性模型。LR模型(对数几率回归模型),虽然叫回归,但是其本质为分类。对数几率函数是一种sigmoid函数。线性模型有可解释性强、易于并行的优点。但是其难以表示非线性关系,所以模型的准确性可能不好。为了增强原始特征与拟合目标之间的非线性关系,通常需要对原始特征做一些非线性转换。常用的转换方法包括:连续特征离散化、特征之间的交叉等。离散化相当于把连续函数变成分段函数来增加非线性...
2020-03-20 17:07:55 1242
原创 Leetcode——409. 最长回文串
class Solution {public: int longestPalindrome(string s) { int index[128]={0}; int slength=s.length(); for(int i=0;i<slength;i++) { int temp=i...
2020-03-19 15:02:12 79
转载 python类 __contains(self,x)__方法
在Class里添加__contains__(self,x)函数可以在类的实例化对象上进行 in 操作.class Graph(): def __init__(self): self.items = {'a':1,'b':2,'c':3} def __str__(self): return '打印我干嘛' def __contai...
2020-03-18 21:40:51 3088
原创 Leetcode——836. 矩形重叠
首先介绍一种比较笨的方法:排除法:排除了不重叠的不就是重叠了嘛。不重叠就那么四种情况在左边/右边/上边/下边(左下右上等其实是可以归于左边/上边的某种情况的)class Solution {public: bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) ...
2020-03-18 10:22:11 103
原创 Leetcode——1160. 拼写单词
class Solution{public: int countCharacters(vector<string>& words, string chars) { int re=0; int sarray[128]={0}; int charray[128]={0}; int temp; ...
2020-03-17 11:05:18 89
原创 torch.cuda.is_available() 为false
最近学习pytorch时出现torch.cuda.is_available()为false可能有三种可能:检查一下pytorch是否是gpu版本一般来是都是gpu版检查一下有没有配置好cuda和cudnn(我的问题)检查一下pytorch版本和cudn版本是否兼容torch.version.cuda可看自己pytorch支持什么版本的cuda...
2020-03-16 11:25:43 3622
原创 Leetcode——面试题 01.06. 字符串压缩
class Solution{public: string compressString(string S) { int lengthpre=S.length(); int temp=0; string restr=""; // S=S+S[lengthpre-1]; string temps;...
2020-03-16 11:12:00 129
原创 Leetcode——695. 岛屿的最大面积
思路1:第一种思路是利用方向数组和set集合来简化代码,不用判断坐标是否超过数组边界。但是也正是因为使用了太多类似于find的函数所以在算法时间复杂度分析相同的情况下,比第二种思路耗时要长一些。时间复杂度为O(line*row)line和row分别为行长和列长。class Solution {public: int maxAreaOfIsland(vector<vect...
2020-03-15 11:37:19 134
原创 Leetcode——300. 最长上升子序列
class Solution {public: int lengthOfLIS(vector<int>& nums) { int length=nums.size(); if(length==0) return 0; int dp[length]; for(int i...
2020-03-14 14:24:08 135
空空如也
tensorflow axis1越界
2018-09-27
qt5.9开启时自动开启vs17
2017-09-25
TA创建的收藏夹 TA关注的收藏夹
TA关注的人