
LeetCode
LeetCode Practice
林下溪源
为学日益,为道日损
展开
-
68. Text Justification
Problem Description:Given an array of words and a widthmaxWidth, format the text such that each line has exactlymaxWidthcharacters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words ...原创 2020-09-03 19:05:51 · 233 阅读 · 0 评论 -
67. Add Binary
Problem Description:67.Add BinaryEasy1983285Add to ListShareGiven two binary strings, return their sum (also a binary string).The input strings are bothnon-emptyand contains only characters1or0.Example 1:Input: a = "11", b = "1"Outpu...原创 2020-08-17 14:32:50 · 241 阅读 · 0 评论 -
65. Valid Number
Problem Description:alidate if a given string can be interpreted asa decimal number.Some examples:"0"=>true" 0.1 "=>true"abc"=>false"1 a"=>false"2e10"=>true" -90e3 "=>true" 1e"=>false"e3"=>false" 6e-1"=>...原创 2020-08-17 12:42:44 · 201 阅读 · 0 评论 -
60. Permutation Sequence
Problem Description:The set[1,2,3,...,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order, we get the following sequence forn= 3:"123" "132" "213" "231" "312" "321"Givennandk, return the...原创 2020-08-03 17:49:54 · 141 阅读 · 0 评论 -
58. Length of Last Word
Problem Description:Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.If the last word does not ...原创 2020-08-03 17:22:25 · 237 阅读 · 0 评论 -
52. N-Queens II
Problem Description:Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return the number ofdistinct solutions to then-queens puzzle.Example:Input: 4Output...原创 2020-07-30 16:11:14 · 127 阅读 · 0 评论 -
51. N-Queens
Problem Description:Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinct solutions to then-queens puzzle.Each solution contains a distinct bo...原创 2020-07-30 16:03:39 · 171 阅读 · 0 评论 -
50. Pow(x, n)
Problem Description:Implementpow(x,n), which calculatesxraised to the powern(xn).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100Example 3:Input: 2.00000, -2Output: 0.25000Explanation: 2...原创 2020-07-29 15:35:14 · 128 阅读 · 0 评论 -
47. Permutations II
Problem Description:Given a collection of numbers that might contain duplicates, return all possible unique permutations.Example:Input: [1,1,2]Output:[ [1,1,2], [1,2,1], [2,1,1]]Code:class Solution { List<List<Integer&原创 2020-07-29 14:25:40 · 138 阅读 · 0 评论 -
46. Permutations
Problem Description:Given a collection ofdistinctintegers, return all possible permutations.Example:Input: [1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]Code:class Solution { List<List<Integer..原创 2020-07-29 12:21:28 · 127 阅读 · 0 评论 -
44. Wildcard Matching
Problem Description:Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching ...原创 2020-07-28 18:16:36 · 194 阅读 · 0 评论 -
700. Search in a Binary Search Tree
Problem Description:Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return原创 2020-06-01 18:11:00 · 184 阅读 · 0 评论 -
237. Delete Node in a Linked List
Problem:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Given linked list --head =[4,5,1,9], which looks like following:Exampl...原创 2020-03-26 11:42:07 · 164 阅读 · 0 评论 -
234. Palindrome Linked List
Problem:Given a singly linked list, determine if it is a palindrome.Example 1:Input: 1->2Output: falseExample 2:Input: 1->2->2->1Output: trueFollow up:Could you do it in ...原创 2020-03-24 12:26:14 · 124 阅读 · 0 评论 -
206. Reverse Linked List
Problem:Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively...原创 2020-03-24 12:20:29 · 155 阅读 · 0 评论 -
203. Remove Linked List Elements
Problem:Remove all elements from a linked list of integers that have valueval.Example:Input: 1->2->6->3->4->5->6, val = 6Output: 1->2->3->4->5Analysis:本题的...原创 2020-03-24 12:08:04 · 140 阅读 · 0 评论 -
160. Intersection of Two Linked Lists
Problem:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:begin to intersect at node c1.Example 1:...原创 2020-03-24 11:51:07 · 139 阅读 · 0 评论 -
148. Sort List
Problem:Sort a linked list inO(nlogn) time using constant space complexity.Example 1:Input: 4->2->1->3Output: 1->2->3->4Example 2:Input: -1->5->3->4->0...原创 2020-03-23 18:08:21 · 135 阅读 · 0 评论 -
147. Insertion Sort List
Problem:Sort a linked list using insertion sort.A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.With each iteration ...原创 2020-03-20 18:11:53 · 119 阅读 · 0 评论 -
143. Reorder List
Problem:Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You maynotmodify the values in the list's nodes, only nodes itself may be changed.Example 1:Giv...原创 2020-03-20 16:29:48 · 114 阅读 · 0 评论 -
142. Linked List Cycle II
Problem:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.To represent a cycle in the given linked list, we use an integerposwhich represents the po...原创 2020-03-20 15:34:38 · 104 阅读 · 0 评论 -
141. Linked List Cycle
Problem:Given a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integerposwhich represents the position (0-indexed)in the linked list wh...原创 2020-03-20 15:12:42 · 146 阅读 · 0 评论 -
109. Convert Sorted List to Binary Search Tree
Problem:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in...原创 2020-03-20 14:58:00 · 146 阅读 · 0 评论 -
92. Reverse Linked List II
Problem:Reverse a linked list from positionmton. Do it in one-pass.Note:1 ≤m≤n≤ length of list.Example:Input: 1->2->3->4->5->NULL, m = 2, n = 4Output: 1->4->3-&...原创 2020-03-19 17:39:29 · 124 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List
Problem:Given a sorted linked list, delete all duplicates such that each element appear onlyonce.Example 1:Input: 1->1->2Output: 1->2Example 2:Input: 1->1->2->3->3...原创 2020-03-19 15:29:13 · 109 阅读 · 0 评论 -
82. Remove Duplicates from Sorted List II
Problem:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.Return the linked list sorted as well.Example 1:Input: ...原创 2020-03-19 13:43:26 · 138 阅读 · 0 评论 -
61. Rotate List
Problem:Given a linkedlist, rotate the list to the right bykplaces, wherekis non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->...原创 2020-03-19 12:24:08 · 125 阅读 · 0 评论 -
274. H-Index
Problem:Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.According to thedefinition of h-index on Wikip...原创 2020-03-16 16:53:39 · 128 阅读 · 0 评论 -
205. Isomorphic Strings
Problem:Given two stringssandt, determine if they are isomorphic.Two strings are isomorphic if the characters inscan be replaced to gett.All occurrences of a character must be replaced wi...原创 2020-03-16 16:33:17 · 127 阅读 · 0 评论 -
242. Valid Anagram
Problem:Given two stringssandt, write a function to determine iftis an anagram ofs.Example 1:Input: s = "anagram", t = "nagaram"Output: trueExample 2:Input: s = "rat", t = "car"...原创 2020-03-14 16:48:19 · 106 阅读 · 0 评论 -
204. Count Primes
Problem:Count the number of prime numbers less than a non-negative number,n.Example:Input: 10Output: 4Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.Analysis:...原创 2020-03-13 16:15:17 · 143 阅读 · 0 评论 -
202. Happy Number
Problem:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of th...原创 2020-03-11 15:45:23 · 191 阅读 · 0 评论 -
187. Repeated DNA Sequences
Problem:All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the ...原创 2020-03-11 15:08:20 · 151 阅读 · 0 评论 -
149. Max Points on a Line
Problem:Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line.Example 1:Input: [[1,1],[2,2],[3,3]]Output: 3Explanation:^|| o| o| ...原创 2020-03-10 15:40:28 · 125 阅读 · 0 评论 -
138. Copy List with Random Pointer
Problem: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 adeep copyof the list.The Linked List is repr...原创 2020-03-04 13:34:55 · 158 阅读 · 0 评论 -
136. Single Number
Problem:Given anon-emptyarray of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it...原创 2020-03-03 17:59:56 · 132 阅读 · 0 评论 -
76. Minimum Window Substring
Problem:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).Example:Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC"...原创 2020-03-03 17:03:14 · 122 阅读 · 0 评论 -
49. Group Anagrams
Problem Description:Given an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat","tan"], ["bat"]]...原创 2020-02-24 12:05:13 · 113 阅读 · 0 评论 -
643. Maximum Average Subarray I
Problem Description:Given an array consisting ofnintegers, find the contiguous subarray of given lengthkthat has the maximum average value. And you need to output the maximum average value.Ex...原创 2019-12-17 14:49:20 · 162 阅读 · 0 评论 -
628. Maximum Product of Three Numbers
Problem Description:Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6Example 2:Input: [1,2,3,4]Out...原创 2019-12-17 14:38:45 · 136 阅读 · 0 评论