You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is(target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], ghosts[i][1]). Each turn, you and all ghosts simultan…
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may be rotated. XX <- domino XX <- "L" tromino X Given N, how many ways are there to tile a 2 x N board? Return your answer modulo 10^9 + 7. (…
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occ…
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each othe…
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 Total Submissions: 1844 Difficulty: Easy Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The…
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round. class Solution { public: string predictPartyVictory(string senate) { int len = senate.size(); int r = 0; int d = 0; int i = 0; while(1) { if(senate[…
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them.…
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 “幻方” 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. class Solution { public: int numMagicSquaresInside(vector<vector<int&…
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash加查找 class Solution { public: int findLHS(vector<int>& nums) { if (nums.empty()) ; ; int len = nums.size(); unordered_map<int, int> hash(len…
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ [题意] 让你把一段序列去掉3个元素,然后分成4个部分; 要求这4个部分的和相同; 问你可不可能; [题解] 先枚举要删除的3个元素中的中间那个元素j; 然后把整个序列分成左边和右边两个部分; 然后再左边的序列中枚举i; 然后把0..j-1分成0..i-1和i+1..j-1两个部分; 然后看这两个部…
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个人是不是朋友. 问你最后有多少个连通块; [题解] 裸并查集. [完整代码] #include <bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #d…
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence-ii/ [题意] 字符串变成多个了; (不止两个) 让你求最长不公共子序列 [题解] 因为最大长度为10; 所以把每个长度有哪些字符串记录下来; 然后从字符串长度由大到小枚举len; 假设这个答案序列为len长度的某个字符串; 然后看看len长度的字符串有没有和它一样的字符串(即出现两次及以上)…
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence-i/ [题意] 让你求两个字符串的最长不公共子序列的长度; 即这个序列是两个字符串中的一个的子序列; 同时要求这个序列不是所有其他任意一个字符串的子序列; [题解] 两个字符串相同就无解; 否则输出两个字符串的长度中较大者; [完整代码] #include <bits/stdc++.h> usi…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/escape-the-ghosts/description/ 题目描述 You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (targ…
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My SubmissionsBack to Contest   User Accepted:1054 User Tried:1755 Total Accepted:1080 Total Submissions:7519 Difficulty:Easy Given an array with n integers, y…
已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode. 这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~). 第一题: 965. Univalued Binary Tree A binary tree is univalued if every node in the tree has the same value. Return true if and only if the given tree is univalu…
581. Shortest Unsorted Continuous Subarray Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find the shor…
Every email consists of a local name and a domain name, separated by the @ sign. For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If yo…
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so on until the 100th row.  Each glass holds one cup (250ml) of champagne. Then, some champagne is poured in the first glass at the top.  When the top m…
1. 551. Student Attendance Record I 2. 552. Student Attendance Record II hihocode原题,https://hihocoder.com/contest/offers10/problems 直接粘贴代码. 其实这个第二题的问题挺多的,这里还是写一下分析过程. 我刚开始做的时候,考虑也是计数dp,一维记录a的个数,一维记录末尾l的个数,然后考虑转移,后来好像是因为转移情况复杂,简化为不考虑a的个数,因为a的个数只有1个,所以…
写的有点晚了. 我每次都是先看一下这里http://bookshadow.com/leetcode/的思路,然后再开始写我自己的. 1. 521. Longest Uncommon Subsequence I 说实话,看完就懵逼了,这怎么做,难道是dp,后仔细一想,不对啊,很简单啊. 只要2个字符串不相等,返回长度较长的那个就行了. 刚开始,考虑,如果其中之一为空串,我返回的-1,不知道怎么想的.这显然是错的. class Solution { public: int findLUSlength…
1, 543. Diameter of Binary Tree 维护左右子树的高度,同时更新结果,返回以该节点结束的最大长度.递归调用. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Sol…
1. 496. Next Greater Element I 暴力的话,复杂度也就1000 * 1000 = 1e6, 在1s的时限内完全可以. 当然,有许多优化方法,利用stack维护递减序列的方法, 见这里http://bookshadow.com/weblog/2017/02/05/leetcode-next-greater-element-i/ 2. 506. Relative Ranks 这个是水题,直接排序,处理一下边界问题,输出就可以. class Solution { publi…
退役老人现在连leetcode都不会做了 = = 今天早上做了leetcode第三题题目看错了,加上比赛中间还在调投稿的实验,一心二用直接gg 总结下教训就是 本渣现在做题连题目都看不清就开始做.开始写题之前应当把样例过一遍,然后自己再造1-2个例子,然后再开始做 A题:统计素数的个数(素数筛或者sqrt(n)判断都可以),然后分别计算count! class Solution { public: int numPrimeArrangements(int n) { vector<int> ha…
leetcode现在每周末举办比赛,这是今天上午参加的比赛的题解.题目难度不算大,两个easy,一个medium,一个hard.hard题之前接触过,所以做得比较顺利. 1.  Sum of Left Leaves(Leetcode 404 Easy) Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two left leaves in the binar…
1. 第一题 看完题目后,肯定先对houses和heaters排序,然后考虑贪心可以么,我那时候没有想出来,然后看到可以O(n)的判断一个半径是否满足要求,就对半径[0,1e9]进行二分,然后就a了. 其实,排完序可以进行贪心,对每一个houses,找到最近的heaters,左右2边的,求最小,这个可以通过二分来做,然后不断更新结果,求出最小值的最大值,这应该算是正常的解法. 我认为应该算是medium,easy难度设的有点低. 2. 第二题 字符串题目,其实看到这题,我是不大乐意的,因为这题需…
上周因为感冒没有刷题,两个星期没有刷题,没手感了,思维也没有那么活跃了,只刷了一道,下个星期努力. 984. String Without AAA or BBB Given two integers A and B, return any string S such that: S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters; The substring 'aaa' does not…
要死要死,第一题竟然错误8次,心态崩了呀,自己没有考虑清楚,STL用的也不是很熟,一直犯错. 第二题也是在室友的帮助下完成的,心态崩了. 970. Powerful Integers Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. Return a list of all powerfu…
766. Toeplitz Matrix 第一题不说,贼麻瓜,好久没以比赛的状态写题,这个题浪费了快40分钟,我真是...... 767. Reorganize String 就是给你一个字符串,能不能转换成:任意一个字符S[i],使得S[i-1]和S[i+1]都不等于S[i].如果能输出,不能就输出一个空字符串. 大一的时候好像有印象,队友做出来的,贪心吧,如果遇到相邻两个一样的,就从后面找一个插到中间,具体原理我也不知道为什么,反正是对的,从头到尾扫一遍,再倒着来一遍,为什么要两边我也不知道…
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th package on the conveyor belt has a weight of weights[i].  Each day, we load the ship with packages on the conveyor belt (in the order given by weights)…