leetcode
KRYON!
这个作者很懒,什么都没留下…
展开
-
[leetcode]5366. 检查网格中是否存在有效路径
解题思路:BFSAC的代码:struct Point{ int x,y; int d; Point(int _x, int _y, int _d) { x = _x, y = _y, d = _d; }};class Solution { bool isOk(int cur, int next, int dir)...原创 2020-03-22 22:07:55 · 201 阅读 · 0 评论 -
[leetcode]5357. 设计一个支持增量操作的栈
class CustomStack { vector<int>stk; int maxSize; int top;public: CustomStack(int m) { stk = vector<int>(m); maxSize = m; top = -1; } ...原创 2020-03-15 15:48:16 · 191 阅读 · 0 评论 -
[leetcode]5356. 矩阵中的幸运数
class Solution {public: vector<int> luckyNumbers (vector<vector<int>>& matrix) { int m = matrix.size(), n = matrix[0].size(); vector<int>res; ...原创 2020-03-15 15:45:50 · 160 阅读 · 0 评论 -
[leetcode]5355. T 秒后青蛙的位置
无向树没环https://www.slideshare.net/mahmoudhikmet/treedirect-and-undirect-treeclass Solution {public: double frogPosition(int n, vector<vector<int>>& edges, int t, int target) ...原创 2020-03-08 22:49:22 · 158 阅读 · 0 评论 -
[leetcode]5354. 通知所有员工所需的时间
找到底层员工往上算class Solution {public: int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) { int res = 0; for(int i = 0; i <...原创 2020-03-08 18:44:54 · 111 阅读 · 0 评论 -
[leetcode]1375. 灯泡切换器III
解题思路其实很简单,一开始没理解题意题目要求所有灯都亮的次数代码class Solution {public: int numTimesAllBlue(vector<int>& light) { int res = 0; vector<bool>blue(light.size()+1, false); ...原创 2020-03-08 12:24:28 · 375 阅读 · 0 评论 -
[leetcode]5337. 每个元音包含偶数次的最长子字符串
前缀和,下次记住前缀和,从1开始存,cnt[0] 存0class Solution {public: int findTheLongestSubstring(string s) { int len = s.size(); vector<int>cntA(len+1, 0); vector<int>cntE(le...原创 2020-03-08 10:21:18 · 145 阅读 · 0 评论 -
[leetcode]5336. 上升下降字符串
很简单的一题,写完后有个bug改了半天,最后发现是越界了。。。额额,有点想java了class Solution {public: string sortString(string s) { int len = s.size(); string res = ""; sort(s.begin(), s.end()); ...原创 2020-03-08 10:15:26 · 147 阅读 · 0 评论 -
[leetcode]1365. 有多少小于当前数字的数字
class Solution {public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { vector<pair<int,int>>tmp(nums.size()); for(int i = 0; i < nums....原创 2020-03-02 22:22:29 · 437 阅读 · 0 评论 -
[leetcode]1366. 通过投票对团队排名
struct Group{ char name; vector<int>rank; Group(char n) { name = n; rank = vector<int>(26,0); } Group(){} bool operator < (Group &an...原创 2020-03-02 22:20:53 · 531 阅读 · 0 评论 -
[leetcode]5172. 形成三的最大倍数
定理:各位数之和能被3整除的数就是3的倍数https://zhidao.baidu.com/question/987173910529894859.htmlclass Solution { int cnt[10] = {0}; int sum = 0; int del(int m) { for(int i = m; i <= 9; i...转载 2020-02-23 20:56:07 · 220 阅读 · 0 评论 -
[leetcode]5171. 最接近的因数
class Solution {public: vector<int> closestDivisors(int num) { vector<int>res; int a = num+1; int b = num+2; int a1 = sqrt(a); int l...原创 2020-02-23 18:35:47 · 182 阅读 · 0 评论 -
[leetcode]5170. 验证二叉树
层次遍历+setclass Solution {public: bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) { vector<bool>visited1(n,false); ...原创 2020-02-23 18:05:57 · 144 阅读 · 0 评论 -
[leetcode]5169. 日期之间隔几天
不调库class Solution {public: int daysBetweenDates(string date1, string date2) { int year1 = stoi(date1.substr(0,4)); int month1 = stoi(date1.substr(5,2)); int day1 = stoi(...原创 2020-02-23 17:56:52 · 148 阅读 · 0 评论 -
[leetcode]5325. 包含所有三种字符的子字符串数目
我写的:超时class Solution {public: int numberOfSubstrings(string s) { int res = 0; for(int i = 0; i < s.size();i++) { bool flag[3] = {0}; for(int ...原创 2020-02-23 10:25:58 · 192 阅读 · 0 评论 -
[leetcode]1357. 每隔 n 个顾客打折
class Cashier { int n, discount; unordered_map<int,int>hash; int cnt;public: Cashier(int n, int discount, vector<int>& products, vector<int>& prices) { ...原创 2020-02-23 10:20:15 · 137 阅读 · 0 评论 -
[leetcode]5323. 根据数字二进制下 1 的数目排序
int cnt_1[10010] = {0};class Solution {public: vector<int> sortByBits(vector<int>& arr) { for(int i = 1; i < 10010; i++) { cnt_1[i] = cnt_1[i>...原创 2020-02-23 10:18:16 · 144 阅读 · 0 评论 -
[leetcode]746. 使用最小花费爬楼梯
动态规划:dp[i]表示爬上第i级楼梯的最小花费class Solution {public: int minCostClimbingStairs(vector<int>& cost) { cost.push_back(0); //后面加个0,将阶梯顶视为第n层(没加0前的第n层,加0后的第n-1层) int n = cost....原创 2020-02-20 18:17:20 · 148 阅读 · 0 评论 -
[leetcode]70. 爬楼梯
动态规划:dp[i]:表示到第i阶有多少种爬法dp[0] = 0dp[1] = 1:1;dp[2] = 2:1+1;2;dp[3] = 3:1 + 1 + 1‾\underline{1}1; 2 + 1‾\underline{1}1; 1 + 1‾\underline{1}1;爬上 i−1 阶楼梯的方法数量。因为再爬1阶就能到第i阶爬上 i−2 阶楼...原创 2020-02-20 17:53:38 · 99 阅读 · 0 评论 -
[leetcode]16. 最接近的三数之和
class Solution {public: int threeSumClosest(vector<int>& nums, int target) { int res = 0; int minDiff = INT_MAX/10; sort(nums.begin(), nums.end()); for(...原创 2020-02-20 17:22:44 · 106 阅读 · 0 评论 -
[leetcode]15. 三数之和
双指针class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>>res; if(nums.size() == 0) { ...原创 2020-02-20 12:27:09 · 91 阅读 · 0 评论 -
[leetcode]476. 数字的补数
1、class Solution {public: int findComplement(int num) { int res = 0; int t = 0; string s = ""; while(num > 0) { t = num % 2; num /= 2...原创 2020-02-19 23:29:59 · 110 阅读 · 0 评论 -
[leetcode] 472. 连接词
字典树struct TrieNode{ bool isEnd; TrieNode *next[26]; TrieNode() { isEnd = false; memset(next, 0, sizeof(next)); }};class Trie{ TrieNode* root;public: ...原创 2020-02-19 22:55:49 · 154 阅读 · 0 评论 -
[leetcode] 140. 单词拆分 II
class Solution { int n; vector<string>res; unordered_map<int,set<int>>hash; string s; void dfs(int start, string temp) { if(start == n) { ...原创 2020-02-19 22:41:08 · 95 阅读 · 0 评论 -
[leetcode]190. 颠倒二进制位
class Solution {public: uint32_t reverseBits(uint32_t n) { bitset<32>temp(n); //左边是低位,temp[0]是二进制的最低位 uint32_t res = 0; for(int i = 0; i < 32; i++) { ...原创 2020-02-18 22:18:12 · 83 阅读 · 0 评论 -
[leetcode] 7. 整数反转
2^31-1=2147483647,-2^31=-2147483648https://leetcode-cn.com/problems/reverse-integer/solution/zheng-shu-fan-zhuan-by-leetcode/class Solution {public: int reverse(int x) { int rev = ...转载 2020-02-18 22:08:52 · 74 阅读 · 0 评论 -
[leetcode]174. 地下城游戏
写了半天从[0][0]开始的,代码有90多行,就过了一个样例。。看了题解,原来只能倒着来。。额额1、题解里写得最简洁的:https://leetcode-cn.com/problems/dungeon-game/solution/c-dp-10xing-by-pjpj/dp[i][j]表示在[i][j]位置需要的最小血量dp[i][j] = max(1, (min(dp[i+1][j...原创 2020-02-17 22:44:48 · 162 阅读 · 0 评论 -
[leetcode]5343. 多次求和构造目标数组
作者:https://leetcode-cn.com/circle/discuss/0gO5RS/class Solution {public: bool isPossible(vector<int>& target) { if(target.size() == 1) { if(target[0] ...转载 2020-02-16 23:34:21 · 98 阅读 · 0 评论 -
[leetcode] 5342. 最多可以参加的会议数目
参考:https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended/solution/sao-miao-suan-fa-tan-xin-by-lucifer1004/class Solution {public: int maxEvents(vector<vector<i...原创 2020-02-16 23:27:42 · 320 阅读 · 0 评论 -
[leetcode]5341. 最后 K 个数的乘积
随便写下暴力就过了,美版也是class ProductOfNumbers { vector<int>nums;public: ProductOfNumbers() { } void add(int num) { nums.push_back(num); } int getPro...原创 2020-02-16 23:20:46 · 90 阅读 · 0 评论 -
[leetcode]5340. 统计有序矩阵中的负数
class Solution {public: int countNegatives(vector<vector<int>>& grid) { int res = 0; for(int i=0; i <grid.size(); i++) { for(int j= 0; j &...原创 2020-02-16 22:10:15 · 103 阅读 · 0 评论 -
[leetcode]229. 求众数 II
解题思路:题目要求空间复杂度为O(1)大于1/3的数不会超过2个,利用Boyer-Moore 投票算法找到数量最多的两个数,再检查下这俩个数的数量有没有超过1/3class Solution {public: vector<int> majorityElement(vector<int>& nums) { vector<int&...原创 2020-02-15 20:32:12 · 299 阅读 · 0 评论 -
[leetcode]169. 多数元素
1、用个hash表计数肯定没问题class Solution {public: int majorityElement(vector<int>& nums) { unordered_map<int,int>hash; int res = 0, maxCnt = INT_MIN; for(int i = 0...原创 2020-02-15 17:24:29 · 200 阅读 · 0 评论 -
[leetcode]90. 子集 II
bitmasking:歪打正着,想着跟子集1(没有重复的元素)差不多,去下重就好了,然后没排序,然后就出现了重复的子集,然后排下序观察输出,然后就好了???,然后发现没排序重复是因为有的子集包含的元素相同,但元素内顺序不一样可以参考子集1https://blog.csdn.net/qq_40691051/article/details/104326461class Solution ...原创 2020-02-15 15:35:38 · 123 阅读 · 0 评论 -
[leetcode]78. 子集
DFS:class Solution {public: vector<vector<int>>ans; vector<vector<int>> subsets(vector<int>& nums) { int n = nums.size(); vector<int>...原创 2020-02-15 14:38:21 · 148 阅读 · 0 评论 -
[leetcode]980. 不同路径 III
DFSclass Solution { int r,c; int startX,startY; int endX, endY; int res = 0; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; vector<vector<int>> grid;...原创 2020-02-14 23:03:56 · 167 阅读 · 0 评论 -
[leetcode]63. 不同路径 II
跟不同路径1差不多(类似杨辉三角)class Solution { typedef long long ll;public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(), n = ob...原创 2020-02-14 18:39:23 · 171 阅读 · 0 评论 -
[leetcode]62. 不同路径
DFS:肯定是超时了啊class Solution { int dx[2] = {1, 0}; int dy[2] = {0, 1}; int res = 0; int m; int n; void dfs(int x, int y) { if(x == m - 1 && y == n - 1) ...原创 2020-02-14 18:25:50 · 88 阅读 · 0 评论 -
[leetcode]20. 有效的括号
class Solution {public: bool isValid(string s) { stack<char>stk; map<char,char>hash; hash[')'] = '('; hash[']'] = '['; hash['}'] = '{'; ...原创 2020-02-14 13:36:08 · 71 阅读 · 0 评论 -
[leetcode]01.04. 回文排列
额额,例子是错的。。。我为什么要点开。。。。。。。class Solution {public: bool canPermutePalindrome(string s) { unordered_map<char,int>hash; for(int i = 0; i < s.size(); i++) { ...原创 2020-02-13 23:50:00 · 85 阅读 · 0 评论