摘要: 暴力求解法,直接遍历求最大值 class Solution { public: int maxProfit(vector<int>& prices) { int maxprofit=0; for(int i=0;i<prices.size();i++) { for(int j=i+1;j<price 阅读全文
posted @ 2021-04-02 12:18 章大佬 阅读(37) 评论(0) 推荐(0)
摘要: 调用反向迭代器函数,rbegin,rend,判断回文串 class Solution { public: bool isPalindrome(string s) { string sgood; for (char ch: s) { if (isalnum(ch)) { sgood += tolowe 阅读全文
posted @ 2021-04-02 12:04 章大佬 阅读(50) 评论(0) 推荐(0)
摘要: 克隆图 class Solution { private HashMap <Node, Node> visited = new HashMap <> (); public Node cloneGraph(Node node) { if (node == null) { return node; } 阅读全文
posted @ 2021-04-01 20:58 章大佬 阅读(28) 评论(0) 推荐(0)
摘要: 利用深度优先搜索,递归+1 class Solution { public: int maxDepth(TreeNode* root) { if(root==NULL) { return 0; } return max(maxDepth(root->left),maxDepth(root->righ 阅读全文
posted @ 2021-04-01 16:23 章大佬 阅读(18) 评论(0) 推荐(0)
摘要: 杨辉三角题,利用动态规划逆推 class Solution { public: vector<int> getRow(int rowIndex) { vector<int> row(rowIndex + 1); row[0] = 1; for (int i = 1; i <= rowIndex; + 阅读全文
posted @ 2021-03-31 20:09 章大佬 阅读(20) 评论(0) 推荐(0)
摘要: 动态规划题,根据动态规划,选择最短的路径 class Solution { public: int minimumTotal(vector<vector<int>>& triangle) { int n = triangle.size(); vector<int> f(n); f[0] = tria 阅读全文
posted @ 2021-03-30 20:45 章大佬 阅读(35) 评论(0) 推荐(0)
摘要: 滑动窗口问题,找出最大的不重复的字符串 class Solution { public: int lengthOfLongestSubstring(string s) { unordered_set<char> occ; int n = s.size(); int rk = -1, ans = 0; 阅读全文
posted @ 2021-03-11 19:37 章大佬 阅读(22) 评论(0) 推荐(0)
摘要: 一个典型的递归算法 class Solution { public: bool hasPathSum(TreeNode *root, int sum) { if (root == nullptr) { return false; } if (root->left == nullptr && root 阅读全文
posted @ 2021-03-09 16:56 章大佬 阅读(17) 评论(0) 推荐(0)
摘要: 运用一个递归进行二叉树的翻转,很基本的题目。、 class Solution { public: TreeNode* invertTree(TreeNode* root) { if (root == nullptr) { return nullptr; } TreeNode*left=invertT 阅读全文
posted @ 2021-03-09 15:57 章大佬 阅读(18) 评论(0) 推荐(0)
摘要: 直接判断,循环遍历,只要是前面是空格并且自己不为空,就是个新单词。 class Solution { public: int countSegments(string s) { int flag=1; int count=0; for(int i=0; i<s.length();i++) { if( 阅读全文
posted @ 2021-03-08 16:47 章大佬 阅读(23) 评论(0) 推荐(0)