Leetcode
杨三金
这个作者很懒,什么都没留下…
展开
-
leetcode7.12
单纯的记录一下自己是个怎样的智障 哈哈我的错误版本:class MovingAverage {public: /** Initialize your data structure here. */ MovingAverage(int size) { this->size = size; nowsize = 0; moves = vector<int>(size); } vector<int>.原创 2020-07-12 18:32:58 · 101 阅读 · 0 评论 -
Leetcode7.12地下城游戏 动态规划
**class Solution {public: int calculateMinimumHP(vector<vector<int>>& dungeon) { int M = dungeon.size(); int N = dungeon[0].size(); int dp[M][N]; dp[M-1][N-1] = dungeon[M-1][N-1] > 0 ? 1: -dungeon[M.原创 2020-07-12 11:45:17 · 114 阅读 · 0 评论 -
LeetCode7.10 股票问题汇总 贪心,动态规划,排序
上次学到的,比较和赋值转为一步,没用上for循环中,应该这么写propit = max(propit,*it - min);min = min(min,*it);关键字和变量名尽量别起一样的for(price:prices) 啥时候C++支持这么写了!class Solution {public: int maxProfit(vector<int>& prices) { if(prices.empty()) return 0; int.原创 2020-07-10 22:20:00 · 158 阅读 · 0 评论 -
LeetCode7.10恢复空格 (字典树)
最开始的思路是所有字符串匹配,但有逻辑错误,因为可能字典里的单词有相同的前缀和后缀。错误代码,能通过部分样例class Solution {public: int respace(vector<string>& dictionary, string sentence) { int falg = false; int ci = 0; vector <string> dict[26]; for(int.原创 2020-07-10 10:42:08 · 112 阅读 · 0 评论 -
Leetcode7.9 前五科的均分
class Solution {public: vector<vector<int>> highFive(vector<vector<int>>& items) { vector<int> student[1001]; for(int i = 0; i < items.size(); i++){ student[items[i][0]].push_back(items.原创 2020-07-09 23:50:05 · 174 阅读 · 0 评论 -
LeetCode7.8 阿姆斯特lang数
class Solution {public: int powcal(int d, int m){ if(m == 0) return 1; int result = 1; while(m--){ result*= d; } return result; } int dight(int N){ if(N/10 == 0) return 1; el.原创 2020-07-08 23:57:57 · 157 阅读 · 0 评论 -
leetcode7.8 三个有序数组的交集 最小元素各数位之和
有序表 建议用二分查找,图省事用了一般的查找class Solution {public: vector<int> arraysIntersection(vector<int>& arr1, vector<int>& arr2, vector<int>& arr3) { int i = 0, j = 0, k = 0; vector<int> result; wh.原创 2020-07-08 23:29:50 · 363 阅读 · 0 评论 -
Leetcode7.8跳水板
class Solution {public: vector<int> divingBoard(int shorter, int longer, int k) { vector<int> maylong; if(k == 0) return maylong; if(shorter!=longer){ for(int i = 0 ;i <= k; i++){ .原创 2020-07-08 22:34:13 · 89 阅读 · 0 评论 -
Leetcode7.7嵌套列表权重和
注意区分NestedInteger 和vector<NestedInteger>两种数据类型/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Constructo.原创 2020-07-07 23:49:14 · 144 阅读 · 0 评论 -
leetcode7.7单行键盘
class Solution {public: int calculateTime(string keyboard, string word) { int now,last=0,sum = 0; for(int i = 0; i < word.size(); i++){ for(int j = 0; j < keyboard.size();j++){ if(word[i] == keyboard[.原创 2020-07-07 23:20:20 · 125 阅读 · 0 评论 -
leetcode7.7找出变位映射
注意可能有相同元素class Solution {public: vector<int> anagramMappings(vector<int>& A, vector<int>& B) { vector<int> P; int visit[A.size()]; memset(visit,false,sizeof(visit)); for(int i = 0; i &l.原创 2020-07-07 23:10:52 · 125 阅读 · 0 评论 -
Leetcode7.7删去字符串中的元音
太久没打C++代码了,字符串如果只声明的话,他的长度是0,不会因为你后来L[0] = 'c’之类的操作改变,但是cin可以改变字符串的长度,所以string声明的时候最好初始化,最后用substr截取自己需要的长度class Solution {public: char judge[5] = {'a','e','i','o','u'}; string removeVowels(string S) { string L =S; int j =0; .原创 2020-07-07 22:50:07 · 135 阅读 · 0 评论 -
LeetCode7.7 路径总合
注意判断空树!递归:/** * 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 { bool bfs(TreeNode *root, i.原创 2020-07-07 18:42:30 · 92 阅读 · 0 评论 -
Leetcode7.6不同路径II
状态转移:代码实现:class Solution {public: int dp[101][101]; int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { if(obstacleGrid[0][0] == 0) dp[0][0] = 1; int m = obstacleGrid.size(); int n = obst.原创 2020-07-06 16:15:30 · 95 阅读 · 0 评论