
计算机算法
LeetCode算法题及解法
katrina95
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
407. Trapping Rain Water II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining.Note:Both m and n are less th...原创 2019-08-25 02:06:15 · 191 阅读 · 0 评论 -
685. Redundant Connection II(java)
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, excep...原创 2019-10-03 05:37:16 · 172 阅读 · 0 评论 -
684. Redundant Connection(java)
In this problem, a tree is an undirected graph that is connected and has no cycles.The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additiona...原创 2019-10-03 02:15:51 · 283 阅读 · 0 评论 -
LeetCode 947. Most Stones Removed with Same Row or Column
On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at most one stone.Now, a move consists of removing a stone that shares a column or row with another s...原创 2019-04-07 13:26:13 · 228 阅读 · 0 评论 -
LeetCode 1091. Shortest Path in Binary Matrix
In an N by N square grid, each cell is either empty (0) or blocked (1).A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, …, C_k such that:Adjac...原创 2019-06-16 13:47:59 · 615 阅读 · 0 评论 -
LeetCode 48. Rotate Image(java)
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix direct原创 2018-01-21 13:48:16 · 453 阅读 · 0 评论 -
LeetCode 240. Search a 2D Matrix II(java)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right. Integers in each co原创 2018-01-26 08:49:16 · 299 阅读 · 0 评论 -
LeetCode之爱的魔力转圈圈 54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]Example...原创 2019-05-22 15:13:42 · 196 阅读 · 0 评论 -
LeetCode 242. Valid Anagram(java)
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note: You may assume the str原创 2018-02-04 09:36:02 · 353 阅读 · 0 评论 -
LeetCode 621. Task Scheduler(java)
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be原创 2018-02-04 09:22:54 · 821 阅读 · 0 评论 -
729. My Calendar I (java)
Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.Your class will have the method, book(int start, int end). Formally, th...原创 2019-07-11 12:42:27 · 254 阅读 · 0 评论 -
LeetCode 218. The Skyline Problem
**A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings ...原创 2019-01-22 07:09:09 · 156 阅读 · 0 评论 -
LeetCode 292. Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2018-01-11 13:43:27 · 125 阅读 · 0 评论 -
生日悖论
生日悖论— 摘自wiki生日悖论(Birthday paradox)是指,如果一个房间里有23个或23个以上的人,那么至少有两个人的生日相同的概率要大于50%。这就意味着在一个典型的标准小学班级(30人)中,存在两人生日相同的可能性更高。对于60或者更多的人,这种概率要大于99%。从引起逻辑矛盾的角度来说生日悖论并不是一种悖论,从这个数学事实与一般直觉相抵触的意义上,它才称得上是一个悖转载 2018-01-11 05:37:21 · 2134 阅读 · 0 评论 -
LeetCode 5. Longest Palindromic Substring(java)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: "babad"Output: "bab"Note: “aba” is also a valid answer.Example:Input: "原创 2018-01-16 05:09:43 · 150 阅读 · 0 评论 -
LeetCode 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:In...原创 2019-01-08 17:00:06 · 102 阅读 · 0 评论 -
LeetCode 340. Longest Substring with At Most K Distinct Characters
Given a string, find the length of the longest substring T that contains at most k distinct characters.Example 1:Input: s = "eceba", k = 2Output: 3Explanation: T is "ece" which its length is 3....原创 2019-01-09 08:35:07 · 314 阅读 · 0 评论 -
KMP算法:两个字符串的匹配,S1里匹配S2
通过创建一个部分匹配表来优化指针后移的效率。http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html原创 2019-10-04 02:18:29 · 380 阅读 · 0 评论 -
LeetCode 58. Length of Last Word(java)
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined as原创 2018-02-07 08:40:31 · 266 阅读 · 0 评论 -
LeetCode 65. Valid Number
Validate if a given string can be interpreted as a decimal number.Some examples:“0” => true" 0.1 " => true“abc” => false“1 a” => false“2e10” => true" -90e3 " => true" 1e" =...原创 2019-04-11 12:58:29 · 123 阅读 · 0 评论 -
LeetCode 299. Bulls and Cows(java)
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that原创 2018-02-05 10:47:47 · 314 阅读 · 0 评论 -
LeetCode 151. Reverse Words in a String
Given an input string, reverse the string word by word.Example: Input: "the sky is blue",Output: "blue is sky the".Note:A word is defined as a sequence of non-space characters.Input string ma...原创 2019-01-07 15:25:10 · 93 阅读 · 0 评论 -
LeetCode 6. ZigZag Conversion(java)
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I原创 2018-01-13 08:33:05 · 264 阅读 · 0 评论 -
LeetCode 38. Count and Say(java)
The count-and-say sequence is the sequence of integers with the first five terms as following:111211211111221 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as原创 2018-02-05 09:16:36 · 894 阅读 · 0 评论 -
LeetCode 151. Reverse Words in a String(java)
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".思路:从后向前走,然后遍历到第i个的时候,判断第i个和第i-1个的关系,从而进行不同的操作,最后一个case 4的代码是LeetCode可以跑过的代码。cas原创 2018-02-06 15:04:13 · 337 阅读 · 0 评论 -
LeetCode 591. Tag Validator(java)
Given a string representing a code snippet, you need to implement a tag validator to parse the code and return whether it is valid. A code snippet is valid if all the following rules hold:The code m...原创 2018-02-11 15:50:45 · 406 阅读 · 0 评论 -
LeetCode 205. Isomorphic Strings(java)
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another chara原创 2018-02-03 14:12:03 · 621 阅读 · 0 评论 -
LeetCode 159. Longest Substring with At Most Two Distinct Characters
Given a string s , find the length of the longest substring t that contains at most 2 distinct characters.Example 1:Input: "eceba"Output: 3Explanation: t is "ece" which its length is 3.Example...原创 2019-01-07 15:26:41 · 119 阅读 · 0 评论 -
LeetCode 340. Longest Substring with At Most K Distinct Characters(java)
Given a string, find the length of the longest substring T that contains at most k distinct characters.For example, Given s = “eceba” and k = 2,T is "ece" which its length is 3.用伪哈希表和start来标志开头,每次遇到新的哈原创 2018-02-12 11:49:48 · 612 阅读 · 0 评论 -
LeetCode 68. Text Justification(java)
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pa原创 2018-02-06 13:19:34 · 508 阅读 · 0 评论 -
LeetCode 161. One Edit Distance(java)
Given two strings S and T, determine if they are both one edit distance apart. 注意:当s和t完全相同时,需要返回false.最快的解法:先调整s和t的长度,使得s的长度小于t,解法中的解决方法很巧妙,通过交换参数,再次调用函数实现~。然后开始判断m和n的长度,如果相差大于1则返回false。遍历两个字符串,当出现两个字原创 2018-01-22 07:51:43 · 378 阅读 · 0 评论 -
LeetCode 777. Swap Adjacent in LR String
LeetCode 777. Swap Adjacent in LR StringIn a string composed of ‘L’, ‘R’, and ‘X’ characters, like “RXXLRXRXL”, a move consists of either replacing one occurrence of “XL” with “LX”, or replacing one ...原创 2018-12-31 10:32:37 · 220 阅读 · 0 评论 -
LeetCode 38. Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:1112112111112211 is read off as “one 1” or 11.11 is read off as “two 1s” or 21.21...原创 2019-01-07 14:14:49 · 127 阅读 · 0 评论 -
LeetCode 722. Remove Comments(java)
Given a C++ program, remove comments from it. The program source is an array where source[i] is the i-th line of the source code. This represents the result of splitting the original source code string原创 2018-02-11 15:39:38 · 551 阅读 · 0 评论 -
LeetCode 1023. Camelcase Matching
A query word matches a given pattern if we can insert lowercase letters to the pattern word so that it equals the query. (We may insert each character at any position, and may insert 0 characters.)Gi...原创 2019-04-11 11:48:44 · 388 阅读 · 0 评论 -
71. Simplify Path(java)
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Corner Cases:Did you consider the case where path = "/../"?In thi原创 2018-02-07 08:56:44 · 341 阅读 · 0 评论 -
LeetCode 383. Ransom Note(java)
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; oth原创 2018-02-04 09:46:32 · 416 阅读 · 0 评论 -
Calculator系列问题三 LeetCode 772. Basic Calculator III
LeetCode 772. Basic Calculator IIIImplement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, ...原创 2019-01-17 02:54:55 · 1039 阅读 · 0 评论 -
Calculator系列问题二 LeetCode 227. Basic Calculator II
LeetCode 227. Basic Calculator IIImplement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces ...原创 2019-01-17 02:52:18 · 337 阅读 · 0 评论 -
5. Longest Palindromic Substring(java)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.Exampl...原创 2019-07-11 13:06:24 · 300 阅读 · 0 评论