![](https://img-blog.csdnimg.cn/20190901194630596.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
leetcode
MaloFleur
这个作者很懒,什么都没留下…
展开
-
leetcode进阶之路——Longest Palindromic Substring
这道题题意很简单,就是求给定字符串的最长回文子串 我的思路就是先找中心点,然后往两边扩展,直到出边界或不相等,然后记录本次回文串的长度,若比之前最长的长度要长,就修改原值,并记录当前的子串。 参考代码如下:class Solution {public: string longestPalindrome(string s) { string res = "";原创 2017-03-10 23:06:45 · 228 阅读 · 0 评论 -
Leetcode进阶之路——Biweekly Contest 1
本周开始leetcode增加了双周的contest,不过就目前来看似乎都锁了,那就不贴链接了1064 Fixed PointGiven an array A of distinct integers sorted in ascending order, return the smallest index i that satisfies A[i] == i. Return -1 if n...原创 2019-06-02 22:03:57 · 416 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 140
5083. Occurrences After BigramGiven words first and second, consider occurrences in some text of the form “first second third”, where second comes immediately after first, and third comes immediatel...原创 2019-06-09 23:00:10 · 281 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 141
1089. Duplicate Zeros给定一个整型数组,若遇到0,则将其重复一遍,剩下数字后移一位,在原数组上进行操作直接新开一个数组,遍历一遍,同时判断是否为0,最后替换原数组即可class Solution {public: void duplicateZeros(vector<int>& arr) { vector<int> res;...原创 2019-06-16 15:06:06 · 344 阅读 · 0 评论 -
Leetcode进阶之路——Biweekly Contest 2
Sum of Digits in the Minimum NumberGiven an array A of positive integers, let S be the sum of the digits of the minimal element of A.Return 0 if S is odd, otherwise return 1.Example 1:Input: [3...原创 2019-06-16 16:04:10 · 252 阅读 · 0 评论 -
Leetcode进阶之路——Target Sum / Maximum Sum
494. Target Sum给定一个非负数组,只通过加法和减法,有多少个算式能够得到target sum这里用到了一个tricks(Java (15 ms) C++ (3 ms) O(ns) iterative DP solution using subset sum with explanation):把原数组分为两部分,一部分求和另一部分求差,则可以得到下式: ...原创 2019-06-21 20:59:04 · 328 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 142
https://leetcode.com/problems/statistics-from-a-large-sample/给定一个长为255的整型数组count, count[k]表示下标为k的数字出现了count[k]次求所有数的最小值、最大值、均值、中位数以及众数遍历的同时记录即可,对这几个概念理解基本就OKclass Solution {public: vector<...原创 2019-06-23 21:31:06 · 362 阅读 · 0 评论 -
Leetcode进阶之路——Biweekly Contest 4
这大概是打过最简单的一次leetcode…都不用赛后总结直接写博了1118 Number of Days in a Month给定年份Y和月份M,返回这一年这一月共有多少天"难点"就是闰年二月的判断了,直接给代码:class Solution {public: int numberOfDays(int Y, int M) { if(M == 2) ...原创 2019-07-13 23:01:19 · 379 阅读 · 0 评论 -
Leetcode进阶之路——Biweekly Contest 3
Two Sum Less Than K给定一个整型数组和K,选取数组中的两个数使其和最接近K先对原数组排序,头尾指针遍历,找到第一个小于K的数即可,实时更新最接近K的值#define fo(i, a, b) for(int i = a; i < b; ++i)class Solution {public: int twoSumLessThanK(vector<...原创 2019-06-30 14:25:04 · 298 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 143
1103. Distribute Candies to People给定两个整数,分别表示糖果数和人数,按顺序给第i个人分配i+1个糖果,若第二个循环,则给第i个人i + 1 + N个糖果返回最后每个人拥有的糖果数直接初始化整个数组,然后遍历一遍即可class Solution {public: vector<int> distributeCandies(int c...原创 2019-06-30 15:46:31 · 235 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 144
1108. Defanging an IP Address很简单的一道题,遍历字符串,给所有的'.'加方括号class Solution {public: string defangIPaddr(string address) { string s = ""; for(int i = 0; i < address.length(); ++i)...原创 2019-07-07 12:42:59 · 379 阅读 · 0 评论 -
leetcode中一些经典动态规划题(不定期更新)
115. Distinct Subsequences状态转移方程:dp[i][j]={dp[i][j−1],s[j]!=t[i]dp[i][j−1]+dp[i−1][j−1],s[j]==t[i]dp[i][j]=\begin{cases}dp[i][j - 1],\qquad\qquad\qquad\qquad\quad s[j] !=t[i] \\\\dp[i][j - 1] + ...原创 2019-07-19 22:13:41 · 2381 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 146
1128. Number of Equivalent Domino Pairs给定一个二维数组,判断其中相同元素的个数相同元素的定义:若对于下标i、j,有dominoes[i] = [a, b], dominoes[j]=[c, d]原创 2019-07-21 12:44:29 · 194 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 148
1144. Decrease Elements To Make Array Zigzag给定一个数组,只能采用减一操作,判断最多需要减多少次,使得数组内数字满足zigzag array,即中间元素比两边大或中间元素比两边小既然最后的结果只有两种,即中间大或中间小,那么分别对两种情况进行计算,取小值即可class Solution {public: int calcuRe...原创 2019-08-04 18:18:11 · 260 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 149
1154. Day of the Year给定一个字符串日期,返回该天是该年的第几天首先把字符串按年月日拎出来,判断是否为闰年,最后求和即可class Solution {public: int strtoInt(string s) { int begin = 0; for (int i = 0; i < s.length(); ++i) if (s[i] ...原创 2019-08-11 14:39:39 · 279 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 151
1169. Invalid Transactions给定一串交易记录,每条记录由[name, time, amount, city]组成,若t[i].amount>1000或(t[i].name = t[j].name && t[i].city != t[j].amount && abs(t[i].time - t[j].time) <= 60),则...原创 2019-08-25 22:04:34 · 151 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 150
1160. Find Words That Can Be Formed by Characters给定一个单词数组words和一个字符串chars,若数组中的单词能由chars中的字母构成,则为good word返回所有good word的长度之和先用一个哈希表存储chars中所有字母出现的个数,之后遍历words,判断每个word是否都在chars中出现过,且出现次数小于等于chars...原创 2019-08-18 13:45:24 · 219 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 152
1175. Prime Arrangements给定一个整数,将1~n的所有数放在a[1] ~ a[n]上,使得素数的下标也是素数,求共有多少种放置方案求出1~n中有多少素数,假设为k个,则只要求Akk∗An−kn−kA_k^k*A_{n-k}^{n-k}Akk∗An−kn−k即可,即分别对素数全排列, 对非素数全排列class Solution {public: int prim...原创 2019-09-01 19:38:49 · 154 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 138
1051. Height CheckerStudents are asked to stand in non-decreasing order of heights for an annual photo.Return the minimum number of students not standing in the right positions. (This is the numbe...原创 2019-05-27 14:22:30 · 185 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 137
感觉这周的题在时间空间没有太卡,每道题用暴力求解都可以过…1046. Last Stone WeightWe have a collection of rocks, each rock has a positive integer weight.Each turn, we choose the two heaviest rocks and smash them together. Sup...原创 2019-05-19 12:50:04 · 121 阅读 · 0 评论 -
Leetcode进阶之路——Rotate Image
这道题题面简单,类似于矩阵转置,按照钟表的顺时针方向选择90度,自己先画个简单的图: 矩阵的阶数为4,于是可以得到以下规律:a00 = a30a01 = a20a21 = a22 · · ·即:a[i][j] = a[n-1-i][j](n为阶数) 于是就得到如下代码:class Solution {public: void rotate(vector<原创 2017-04-24 10:19:32 · 463 阅读 · 0 评论 -
Leetcode进阶之路——Palindrome
本文介绍四道跟Palindrome相关的题目。 首先是125. Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this pro...原创 2018-07-19 16:45:46 · 398 阅读 · 0 评论 -
Leetcode进阶之路——Sudoku
本文介绍两道与数独相关的题 第一题 36. Valid Sudoku Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: 1. Each row must contain the digit...原创 2018-07-25 16:28:19 · 233 阅读 · 0 评论 -
Leetcode进阶之路——Parentheses
本文介绍两道Parentheses的题,解法都与stack相关 先是入门题20.Valid Parentheses 这道题的题意就是给一些由(、)、{、}、[、]组成的字串,判断其是否是合法的 解法如图: 首先要知道stack是先进后出的,那么每次输入一个新的样例,若是括号的左部,则将其push近栈中,而若是括号的右部,且与栈顶搭配,则将栈顶元素pop出来(右部也不需要push进),如...原创 2018-07-21 14:41:16 · 254 阅读 · 0 评论 -
Leetcode进阶之路——Trie(Prefix Tree)
关于前缀树的介绍网上已有很多文章,简单的说,这是一种典型的“空间换时间”的数据结构 前缀树可构造如下:class TrieNode{ int val; TrieNode* children[num]; bool flag;public: TrieNode() { val = 0; memset(children,...原创 2018-09-12 14:08:45 · 868 阅读 · 0 评论 -
Leetcode进阶之路——Balloons
最近做了两道扎气球的题,都很有意思,特别是第二道,用到了动态规划,第一次看见还是有些难度的,现将其记录如下。452.Minimum Number of Arrows to Burst BalloonsThere are a number of spherical balloons spread in two-dimensional space. For each balloon, provi...原创 2018-10-13 22:53:05 · 205 阅读 · 0 评论 -
Leetcode进阶之路——LRU Cache C++三种解法
去年网易秋招面试的时候有一道现场手撕LRU的题,跟这道几乎一样,现记录如下:146. LRU CacheDesign and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) - Ge...原创 2019-01-05 21:02:57 · 3165 阅读 · 0 评论 -
Leetcode进阶之路——并查集
关于并查集的基础理解,网上已有很多,推荐这篇博客并查集讲得很细,个人认为对理解很有帮助,而我这里则单纯举个例子帮助理解:一个班有26个人,分别用A~Z标记假设:如果A和B是朋友,B和C是朋友,那么A和C也是朋友(间接的),于是A、B、C三个人组成了一个小圈子,这时候如果D和B也是朋友,那么ABCD四个人就是一个小圈子那么怎么判断这若干人是否同属于一个圈子呢?先设置一个数组pre[N],其...原创 2019-01-23 17:26:07 · 4566 阅读 · 1 评论 -
Leetcode进阶之路——Weekly Contest 127
1005. Maximize Sum Of Array After K NegationsGiven an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K tim...原创 2019-03-10 13:42:21 · 177 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 128
1012. Complement of Base 10 IntegerEvery non-negative integer N has a binary representation. For example, 5 can be represented as “101” in binary, 11 as “1011” in binary, and so on. Note that exce...原创 2019-03-17 18:44:57 · 367 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 129
先吐槽一下leetcode周赛时间又变了… 等进去的时候才发现都快结束了= =1020. Partition Array Into Three Parts With Equal SumGiven an array A of integers, return true if and only if we can partition the array into three non-empty ...原创 2019-03-24 14:07:01 · 342 阅读 · 3 评论 -
Leetcode进阶之路——Weekly Contest 130
1017. Convert to Base -2Given a number N, return a string consisting of "0"s and "1"s that represents its value in base -2 (negative two).The returned string must have no leading zeroes, unless the...原创 2019-03-31 18:14:41 · 192 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 131
1021. Remove Outermost ParenthesesA valid parentheses string is either empty (""), “(” + A + “)”, or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For e...原创 2019-04-08 13:58:17 · 167 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 133
1029 Two City Schedulinghere are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[...原创 2019-04-21 16:01:04 · 153 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 132
1025. Divisor GameAlice and Bob take turns playing a game, with Alice starting first.Initially, there is a number N on the chalkboard. On each player’s turn, that player makes a move consisting of...原创 2019-04-14 15:34:57 · 231 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 136
1041. Robot Bounded In CircleOn an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:“G”: go straight 1 unit;“L”: turn 90 degrees...原创 2019-05-13 22:18:04 · 195 阅读 · 0 评论 -
Leetcode进阶之路——Weekly Contest 153
1184. Distance Between Bus Stops给定一个distance数组,distance[i]表示从i到i(+1) % n的距离,返回从start 到 destination的最短路径由于必定是一个环,因此有两种路径,顺时针和逆时针先用一个前缀和判断某个方向(如顺时针),用总距离与之相减即为另一方向的距离,返回两者中较小值class Solution {publi...原创 2019-09-08 15:14:18 · 167 阅读 · 0 评论