
LeetCode Algorithm
文章平均质量分 76
Bob__yuan
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode - 289. Game of Life
LeetCode - 289. Game of Life根据 百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机。给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态:1 即为活细胞(live),或 0 即为死细胞(dead)。每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:...原创 2020-04-02 09:35:15 · 283 阅读 · 0 评论 -
LeetCode - 138. Copy List with Random Pointer 随机节点链表复制
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list. 节点结构如下:class Node {public: ...原创 2020-01-30 10:29:43 · 278 阅读 · 0 评论 -
单词翻转
一、字符串按照单词顺序翻转 LeetCode - 151. Reverse Words in a StringGiven an input string, reverse the string word by word.Input: “the sky is blue”Output: “blue is sky the” Example 2:Input: " hello world! ...原创 2019-11-17 12:29:57 · 501 阅读 · 0 评论 -
LeetCode - 1247. Minimum Swaps to Make Strings Equal
1247. Minimum Swaps to Make Strings Equal 给出两个只包含x和 y的字符串,问最少多少步交换可以让两个字符串相同? 交换是指任意s1[i]和s2[j]的一次交换。如果不能达到相同,输出-1,如果能输出最少交换次数。Input1: s1 = “xx”, s2 = “yy”Output1: 1Input: s1 = “xy”, s2 = “yx...原创 2019-11-09 13:59:59 · 299 阅读 · 0 评论 -
【DP】LeetCode - House Robber 问题
这个系列共三道题,前两天类似,是一维数组dp问题,第三题是树的遍历:198. House Robber213. House Robber II337. House Robber IIILeetCode - 198. House Robber 给定一个非负整数的数组nums,表示每个房子的价值,小偷偷房子里的东西,不能偷两个相邻的房子,问最多投多少?Input: [1,2,3...原创 2019-10-27 23:38:23 · 339 阅读 · 0 评论 -
【DP】LeetCode - 1235. Maximum Profit in Job Scheduling
LeetCode - 1235. Maximum Profit in Job Scheduling 正常的dp用的都是数组然后以下标的形式表示每个位置的dp值,但是这里可以看出我们需要的下标值不是连续的,而是每个 Job 的 endTime,所以就想到用 mao 当做 dp 数组使用,就和正常的 dp 一样了。 把所有任务按照 endTime 进行排序,然后每次计算 dp[i] 的时候,先...原创 2019-10-27 15:58:21 · 833 阅读 · 0 评论 -
LeetCode - 234. Palindrome Linked List 判断回文链表
LeetCode - 234. Palindrome Linked ListGiven a singly linked list, determine if it is a palindrome.Input: 1->2 Output: falseInput: 1->2->2->1 Output: trueFollow up: Could you do it i...原创 2019-10-13 14:47:46 · 333 阅读 · 0 评论 -
字符串去除匹配所有括号
每一个字符串,比如 (())()()Abc(DEF)(g)))(hi)((,去除所有匹配括号,也就是变成 AbcDEFg))hi((。 可以从左往右遍历,也可以从右往左遍历。这种左右括号的题目,一般都要用栈,从左往右的方式如下:void erase_braces(string& s) { stack<int> st; for(int i = 0; i < s...原创 2019-10-12 21:09:51 · 1577 阅读 · 0 评论 -
【二叉树】遍历(递归、迭代、Morris Traversal)
二叉树的遍历,是程序员面试的最基本问题,对于基础分为三种遍历顺序:前序、中序、后序,这个 “前、中、后” 都是指根,也就是对应先根序、中根序、后根序,左右子节点的顺序默认都是先左后右。 LeetCode 中有详细的解释: https://leetcode.com/explore/learn/card/data-structure-tree/134/traverse-a-tree/992/...原创 2019-08-14 18:42:01 · 572 阅读 · 0 评论 -
LeetCode - 665. Non-decreasing Array
LeetCode - 665. Non-decreasing ArrayGiven an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if ar...原创 2019-08-29 15:20:55 · 150 阅读 · 0 评论 -
【贪心】LeetCode - 135. Candy 及其变体
LeetCode - 135. CandyThere are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must...原创 2019-08-29 16:44:33 · 161 阅读 · 0 评论 -
【洗牌算法】Fisher-Yates Shuffle
这里记录了一些有趣的算法,http://www.keithschwarz.com/interesting/,里边就包括洗牌算法 Fisher-Yates Shuffle。 算法目的,是在一个数组中,将所有元素等概率随机打乱,就像完美的洗牌一样。 算法流程就是,在长度为 n 的数组 arr 中,第一次在 [ 0, n-1 ] 中随机出一个位置 i,将 arr[i] 和 arr[0] 交换...原创 2019-08-23 10:42:12 · 464 阅读 · 0 评论 -
【快排】数组第k大元素 LeetCode - 215. Kth Largest Element in an Array
快排的详细总结可以看 快排的思想就是,每次选一个 pivot(比如最后一个数),然后将比它小的放它左边(或者右边),把比它大的放他右边(或者左边),相当于每次排好一个数,然后递归的排他左右两部分,这个数自己就不会再动了。 面试时经常回问的一道题就是,找出一个数组第 k 大的元素:LeetCode - 215. Kth Largest Element in an ArrayFind...原创 2019-08-29 20:30:03 · 296 阅读 · 0 评论 -
【滑动窗口】LeetCode - 3. 最长连续非重复子串
LeetCode - 3. Longest Substring Without Repeating CharactersGiven a string, find the length of the longest substring without repeating characters.Given “abcabcbb”, the answer is “abc”, which the l...原创 2019-09-10 10:28:09 · 385 阅读 · 0 评论 -
【二叉树】重建,已知前序、中序,已知中序、后序
f 对于一棵二叉树而言,遍历方式基本分为三种:前序、中序、后序。 想要通过已有的遍历顺序重建二叉树,需要两种遍历方式,其中必须包括中序遍历。这是因为前序和后序只能提供哪个是根节点(两种方式提供的信息其实是一样的,只不过提供方式稍有不同),不能提供左右子树的信息,想要把左右子树区分开,一定要用中序遍历。因为在中序遍历中,知道了根节点的位置,那么它左边就都是左子树的节点,右边就都是右子树的...原创 2019-08-16 15:05:11 · 447 阅读 · 0 评论 -
【DP】【LCS】最长公共子序列及其应用
Given two strings text1 and text2, return the length of their longest common subsequence.A subsequence of a string is a new string generated from the original string with some characters(can be non...原创 2019-08-17 15:30:57 · 994 阅读 · 0 评论 -
【DP】【LIS】最长递增子序列 - O(N^2)方法 + O(NlogN)方法
LeetCode - 300. Longest Increasing SubsequenceGiven an unsorted array of integers, find the length of longest increasing subsequence(LIS).Example:Input: [10,9,2,5,3,7,101,18]Output: 4Explanat...原创 2019-08-17 21:38:57 · 482 阅读 · 0 评论 -
【DP】【LPS】最长回文子序列
Given a string s, find the longest palindromic subsequence’s length in s.You may assume that the maximum length of s is 1000.Example 1:Input: “bbbab”Output: 4One possible longest palindromic sub...原创 2019-08-17 18:42:07 · 312 阅读 · 0 评论 -
【Trie】 (Prefix Tree) - 前缀树、字典树
LeetCode - 208. Implement Trie (Prefix Tree) Trie, 又称前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定。一个节点的所有子孙都有相同的前缀,也就是这个节点对应的字符串,而根节点对应空字符串。一般情况下,不是所有的节点都有对应的值,只有叶子节点和部分内部节点...原创 2019-08-11 21:38:39 · 499 阅读 · 0 评论 -
【并查集】Union Find
LeetCode - 547. Friend CirclesThere are N students. Some of them are friends, while some are not. Their friendship is transitive in nature.For example, if A is a friend of B, and B is a friend of C...原创 2019-08-24 12:28:45 · 211 阅读 · 0 评论 -
【DP】LeetCode - 120. Triangle、贝壳找房“采木头,锯子斧头”问题
锯子和斧头轮流砍树问题: 第一行输入树的个数 n, 接下来的 n 行,每行分别输入三个数 a、b、c,分别代表用锯子和斧头砍该棵树的时间,以及换工具砍树所需要的时间。现在手上是斧头,问看完这些树,最短需要多长时间。输入:320 40 2010 4 2590 100 5输出:139Explanation:第一棵树用斧头砍(40),第二颗树还用斧头(4),第三棵树,换成锯子(5 + ...原创 2019-08-11 15:03:59 · 468 阅读 · 1 评论 -
LeetCode - 1156. Swap For Longest Repeated Character Substring
Given a string text, we are allowed to swap two of the characters in the string.Find the length of the longest substring with repeated characters.Input: "ababa" Output: 3Input: "aaabaaa" ...原创 2019-08-11 13:25:02 · 481 阅读 · 0 评论 -
【DP】【DFS】LeetCode - Word Break I - II、Concatenated Words
LeetCode - 139. Word BreakGiven a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more di...原创 2019-08-30 18:46:11 · 179 阅读 · 0 评论 -
【DP】LeetCode - 1218. 最长等差数列子数组
给一个已排序数组,问最长的等差数列子数组多长。这道题和 LIS 基本一模一样,LIS 可以参考 https://blog.youkuaiyun.com/Bob__yuan/article/details/99696806。样例输入:51 2 3 4 5样例输出: 5 用一个二维 dp 矩阵,dp[i][d] 表示以 i 位置为结尾的,差为 d 的等差数列的长度。#include <...原创 2019-09-16 23:20:51 · 533 阅读 · 0 评论 -
LeetCode - 236. 二叉树两节点公共祖先
LeetCode - 236. Lowest Common Ancestor of a Binary TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia:“Th...原创 2019-10-09 14:14:11 · 388 阅读 · 0 评论 -
【旋转数组】LeetCode中旋转数组问题总结
共四道题:1、33. Search in Rotated Sorted Array - 旋转数组中的查找(没有重复元素)2、81. Search in Rotated Sorted Array II - 旋转数组中的查找(有重复元素)3、153. Find Minimum in Rotated Sorted Array - 旋转数组中最小值(没有重复元素)4、154. Find Minim...原创 2019-10-08 19:13:57 · 188 阅读 · 0 评论 -
LeetCode -11. Container With Most Water(最大盛水、经典“哨兵”题)
LeetCode - 11. Container With Most WaterGiven n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of l...原创 2019-10-08 16:59:40 · 191 阅读 · 0 评论 -
LeetCode -166. Fraction to Recurring Decimal 计算除法
LeetCode - 166. Fraction to Recurring DecimalGiven two integers representing the numerator and denominator of afraction, return the fraction in string format.If the fractional part is repeating, e...原创 2019-10-08 11:03:59 · 221 阅读 · 0 评论 -
LeetCode - 1191. K-Concatenation Maximum Sum
LeetCode - 1191. K-Concatenation Maximum Sumint maxSubArray(const vector<int>& nums) { int max_sum = INT_MIN, cur_sum = 0; for (const int& i : nums) { cur_sum = max(i, cur_sum + i);...原创 2019-09-26 15:30:31 · 348 阅读 · 0 评论 -
LeetCode - 1190. Reverse Substrings Between Each Pair of Parentheses
LeetCode - 1190. Reverse Substrings Between Each Pair of ParenthesesYou are given a string s that consists of lower case English lettersand brackets.Reverse the strings in each pair of matching pa...原创 2019-09-26 13:06:52 · 659 阅读 · 0 评论 -
将数组分成两部分,使得 |sum1 - sum2| 最小. LeetCode - 1049
将一个数组分成两部分,不要求两部分所包含的元素个数相等,要求使得这两个部分的和的差值绝对值最小(|S1 - S2|,S1、S2 是两个数组的和)。比如对于数组 {1,0,1,7,2,4},可以分成 {1,0,1,2,4} 和 {7},使得这两部分的差值最小。 这个问题可以转化为求数组的一个子集,使得这个子集中的元素的和尽可能接近sum/2,其中sum为数组中所有元素的和。这样转换之后这个问...原创 2019-09-17 09:59:01 · 6230 阅读 · 0 评论 -
队列实现栈,栈实现队列
很常见的两道题:1、用 queue 实现 stack:LeetCode - 225. Implement Stack using Queues2、用 stack 实现 queue:LeetCode - 232. Implement Queue using Stacks一、Implement Stack using Queues 就是用 queue 来模拟 stack,实现 push、...原创 2019-09-16 10:52:32 · 218 阅读 · 0 评论 -
一维数组中最大连续子数组、二维矩阵中最大子矩阵
共两道题,都是不难,还是记录一下:1、在一个一维数组中,找出连续子数组的最大和:LeetCode - 53. Maximum Subarray2、在一个二维矩阵中,找出子矩阵最大和:https://www.51nod.com/Challenge/Problem.html#problemId=1051一、数组子数组最大和Given an integer array nums, find...原创 2019-09-15 11:57:47 · 923 阅读 · 0 评论 -
【DP】LeetCode - 1155. Number of Dice Rolls With Target Sum
You have d dice, and each die has f faces numbered 1, 2, …, f.Return the number of possible ways (out of fd total ways) modulo 10^9 + 7 > to roll the dice so the sum of the face up numbers equals...原创 2019-09-04 20:33:19 · 269 阅读 · 0 评论 -
【DP】【编辑距离】LeetCode - 72. Edit Distance
LeetCode - 72. Edit DistanceGiven two words word1 and word2, find the minimum number of operations required to convert word1 to word2.You have the following 3 operations permitted on a word:Inser...原创 2019-09-02 20:53:26 · 1264 阅读 · 0 评论 -
数组中元素出现次数相关问题 Majority Element
共两道:1、数组中,出现次数超过一半的元素(一定存在):LeetCode - 169. Majority Element2、数组中,出现次数超过 n / 3 的元素(不一定存在):LeetCode - 229. Majority Element II一、LeetCode - 169. Majority ElementGiven an array of size n, find the m...原创 2019-09-02 17:32:53 · 233 阅读 · 0 评论 -
【拓扑排序】LeetCode - Course Schedule I、II - 判断有向图中是否有环
There are a total of n courses you have to take, labeled from 0 to n-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:...原创 2019-07-12 14:19:18 · 683 阅读 · 0 评论 -
链表正向、反向相加
就是原封不动的这道题:LeetCode - 445. Add Two Numbers IIYou are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contai...原创 2019-07-15 20:43:26 · 703 阅读 · 0 评论 -
LeetCode - 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.Example:Input: “aab”Output: [ [“aa”,“b”], [“a”,“a”,“b...原创 2019-07-01 12:02:05 · 170 阅读 · 0 评论 -
LeetCode - 73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Input: [ [1,1,1], Output: [ [1,0,1], [1,0,1], [0,...原创 2018-09-11 20:36:29 · 135 阅读 · 0 评论