
Leetcode题解
文章平均质量分 58
喵喵喵~
isee_nh
一般通过高槻弥生
展开
-
95. Unique Binary Search Trees II. Sol
官方解答写的很好搬运一下Given an integern, returnall the structurally uniqueBST's (binary search trees), which has exactlynnodes of unique values from1ton. Return the answer inany order.Example 1:Input: n = 3Output: [[1,null,2,null,3],[1,null,3,2],...原创 2022-02-23 21:23:20 · 142 阅读 · 0 评论 -
779. K-th Symbol in Grammar. Sol
这个题目是没有官方Sol的。用循环是不可以解出的(Memory or Time exceeded),罕见的必须要用递归的题目,而且递归的写法也很优雅We build a table ofnrows (1-indexed). We start by writing0in the1strow. Now in every subsequent row, we look at the previous row and replace each occurrence of0with01, and...原创 2022-02-22 18:57:36 · 184 阅读 · 0 评论 -
满血复活~喵呜~喵~呜~
满血复活原创 2022-02-20 18:35:43 · 160 阅读 · 0 评论 -
50. Pow(x, n). O(logN) Sol
搬运一下官方解答的二分法,时间复杂度为O(log)Implementpow(x, n), which calculatesxraised to the powern(i.e.,xn).Example 1:Input: x = 2.00000, n = 10Output: 1024.00000Example 2:Input: x = 2.10000, n = 3Output: 9.26100Example 3:Input: x = 2.00000, n...原创 2022-02-14 03:35:21 · 320 阅读 · 0 评论 -
70. Climbing Stairs. Iter--Sol
也是斐波那契数列的变体。区别是初始项不同。如果直接用递归做会比较耗时You are climbing a staircase. It takesnsteps to reach the top.Each time you can either climb1or2steps. In how many distinct ways can you climb to the top?Example 1:Input: n = 2Output: 2Explanation: There...原创 2022-02-14 03:20:08 · 269 阅读 · 0 评论 -
509. Fibonacci Number. Sol
还是Easy的题目,所以多做一点提升自信心(bushi)用递归很简单,不过这里不是用递归做的,是用普通的迭代,只需要一个for循环就够了TheFibonacci numbers, commonly denotedF(n)form a sequence, called theFibonacci sequence, such that each number is the sum of the two preceding ones, starting from0and1. That is,...原创 2022-02-14 03:10:38 · 192 阅读 · 0 评论 -
119. Pascal‘s Triangle II. Sol
Easy难度的题目,其实是二项式展开,不需要递归就可以了Given an integerrowIndex, return therowIndexth(0-indexed) row of thePascal's triangle.InPascal's triangle, each number is the sum of the two numbers directly above it as shown:Example 1:Input: rowIndex = 3O...原创 2022-02-14 01:50:21 · 213 阅读 · 0 评论 -
700. Search in a Binary Search Tree. Sol
是一道Easy题目,但是想要写的简洁一点也不容易You are given therootof a binary search tree (BST) and an integerval.Find the node in the BST that the node's value equalsvaland return the subtree rooted with that node. If such a node does not exist, returnnull.Examp...原创 2022-02-13 21:11:59 · 184 阅读 · 0 评论 -
344. Reverse String. Sol
搬运的官方sol,感觉不用区分奇偶还是不错滴Write a function that reverses a string. The input string is given as an array of characterss.You must do this by modifying the input arrayin-placewithO(1)extra memory.Example 1:Input: s = ["h","e","l","l","o"]Output:...原创 2022-02-13 19:22:30 · 218 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree. Sol
只需要遍历两次的解法Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodespandqas the lowest node inTthat has both...原创 2022-02-12 18:16:21 · 275 阅读 · 0 评论 -
297. Serialize and Deserialize Binary Tree. Sol
懒得写了。。直接搬运一下官方答案Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or an原创 2022-02-12 16:27:08 · 106 阅读 · 0 评论 -
117. Populating Next Right Pointers in Each Node II. Sol
自己写的普通层序遍历from collections import dequeclass Solution: def connect(self, root): if not root or (root.left==None and root.right==None): return root queue = deque([root,]) while queue: # number of elem原创 2022-02-12 00:58:57 · 207 阅读 · 0 评论 -
117. Populating Next Right Pointers in Each Node II. Sol
官方解答写的非常好,搬运一下class Solution: def processChild(self, childNode, prev, leftmost): if childNode: # If the "prev" pointer is alread set i.e. if we # already found atleast one node on the next level,原创 2022-02-12 00:54:18 · 592 阅读 · 0 评论 -
前序和中序遍历重建二叉树--105. Construct Binary Tree from Preorder and Inorder Traversal. Sol
Given two integer arrayspreorderandinorderwherepreorderis the preorder traversal of a binary tree andinorderis the inorder traversal of the same tree, construct and returnthe binary tree.Example 1:Input: preorder = [3,9,20,15,7], inorder ...原创 2022-02-11 18:40:25 · 217 阅读 · 0 评论 -
后序和中序遍历重建二叉树--106. Construct Binary Tree from Inorder and Postorder Traversal. Sol
非常有意思的一道题Given two integer arraysinorderandpostorderwhereinorderis the inorder traversal of a binary tree andpostorderis the postorder traversal of the same tree, construct and returnthe binary tree.Example 1:Input: inorder = [9,3,15,20...原创 2022-02-11 18:18:55 · 357 阅读 · 0 评论 -
妖梦你还有很多幽灵要杀
原创 2022-02-11 16:27:20 · 217 阅读 · 0 评论 -
250. Count Univalue Subtrees. Sol
不讲武德地使用了set()函数Given therootof a binary tree, return the number ofuni-valuesubtrees.Auni-value subtreemeans all nodes of the subtree have the same value.Example 1:Input: root = [5,1,5,5,5,null,5]Output: 4Example 2:Input: root = []...原创 2022-02-11 16:22:26 · 129 阅读 · 0 评论 -
112. Path Sum. Sol
Recursion 简洁的递归算法Given therootof a binary tree and an integertargetSum, returntrueif the tree has aroot-to-leafpath such that adding up all the values along the path equalstargetSum.Aleafis a node with no children.Example 1:Input: ...原创 2022-02-10 20:48:32 · 282 阅读 · 0 评论 -
101. Symmetric Tree. Sol
提供一种很有意思的递归解法Given therootof a binary tree,check whether it is a mirror of itself(i.e., symmetric around its center).Example 1:Input: root = [1,2,2,3,4,4,3]Output: trueExample 2:Input: root = [1,2,2,null,3,null,3]Output: false...原创 2022-02-10 19:44:20 · 216 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree. Sol
Given therootof a binary tree, returnits maximum depth.A binary tree'smaximum depthis the number of nodes along the longest path from the root node down to the farthest leaf node.from collections import dequeclass Solution: def maxDepth(self...原创 2022-02-10 16:34:09 · 186 阅读 · 0 评论 -
二叉树Morris遍历——Binary Tree Morris Traversal without Modifying Tree
基于官方解答的改进,Morris Traversal 且不用更改原来树的结构Morris原文Traversing binary trees simply and cheaply - ScienceDirectPreOrder 前序遍历 class Solution(object): def preorderTraversal(self, root): node, output = root, [] while node: i原创 2022-02-10 02:40:32 · 429 阅读 · 0 评论 -
61. Rotate List. Sol
Given theheadof a linkedlist, rotate the list to the right bykplaces.Example 1:Input: head = [1,2,3,4,5], k = 2Output: [4,5,1,2,3]Example 2:Input: head = [0,1,2], k = 4Output: [2,0,1]Constraints:The number of nodes in the l...原创 2022-02-06 02:18:00 · 327 阅读 · 0 评论 -
138. Copy List with Random Pointer. Sol
LeetCode官方教程参考答案,穷学生只充了一个月的会员,记录下来备忘A linked list of lengthnis given such that each node contains an additional random pointer, which could point to any node in the list, ornull.Construct adeep copyof the list. The deep copy should consist of exac...原创 2022-02-06 00:11:58 · 101 阅读 · 0 评论 -
2. Add Two Numbers. Sol
重点是添加新的结点。You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse order, and each of their nodes contains a single digit. Add the two numbers and return the sumas a linked list.You may assume t...原创 2022-02-05 19:35:08 · 762 阅读 · 0 评论 -
708. Insert into a Sorted Circular Linked List. Sol
Given a Circular Linked List node, which is sorted in ascending order, write a function to insert a valueinsertValinto the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list and may not nece..原创 2022-02-05 23:49:58 · 372 阅读 · 0 评论 -
430. Flatten a Multilevel Doubly Linked List. Sol
You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additionalchild pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These chi.原创 2022-02-05 22:19:35 · 123 阅读 · 0 评论 -
21. Merge Two Sorted Lists. Sol
LeetCode官方教程参考答案,穷学生只充了一个月的会员,记录下来备忘You are given the heads of two sorted linked listslist1andlist2.Merge the two lists in a onesortedlist. The list should be made by splicing together the nodes of the first two lists.Returnthe head of the merg...原创 2022-02-05 17:10:01 · 73 阅读 · 0 评论 -
203. Remove Linked List Elements. Sol
Given theheadof a linked list and an integerval, remove all the nodes of the linked list that hasNode.val == val, and returnthe new head.Example 1:Input: head = [1,2,6,3,4,5,6], val = 6Output: [1,2,3,4,5]Example 2:Input: head = [], val...原创 2022-02-04 06:12:58 · 615 阅读 · 0 评论 -
206. Reverse Linked List. Sol
LeetCode官方教程参考答案,穷学生只充了一个月的会员,记录下来备忘本专栏无以上特殊说明的其他Sol是自己写的Given theheadof a singly linked list, reverse the list, and returnthe reversed list.Example 1:Input: head = [1,2,3,4,5]Output: [5,4,3,2,1]Example 2:Input: head = [1,2]Outp...原创 2022-02-04 05:05:16 · 443 阅读 · 0 评论 -
19. Remove Nth Node From End of List. Sol
Given theheadof a linked list, remove thenthnode from the end of the list and return its head.Example 1:Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5]Example 2:Input: head = [1], n = 1Output: []Example 3:Input: head = [1,2], n...原创 2022-02-04 04:30:26 · 836 阅读 · 0 评论 -
160. Intersection of Two Linked Lists.Sol
Given the heads of two singly linked-listsheadAandheadB, returnthe node at which the two lists intersect. If the two linked lists have no intersection at all, returnnull.For example, the following two linked lists begin to intersect at nodec1:T...原创 2022-02-04 00:29:20 · 105 阅读 · 0 评论 -
142. Linked List Cycle II .Sol
总之官方答案是错的。。。下面是自己写的O(1)解法,缺点是比较慢。。。Given theheadof a linked list, returnthe node where the cycle begins. If there is no cycle, returnnull. (其实应该是None,太久没写Python了比较生,语法都不会了)There is a cycle in a linked list if there is some node in the list that c...原创 2022-02-03 21:02:16 · 828 阅读 · 0 评论 -
希望接下来的一年能继续努力呀~
首先每日一问自己啥时候写完leetcode原创 2022-02-03 19:46:27 · 488 阅读 · 0 评论 -
LeetCode 141. Linked List Cycle Sol.
LeetCode官方教程参考答案,穷学生只充了一个月的会员,记录下来备忘Givenhead, the head of a linked list, determine if the linked list has a cycle in it.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following thenextp...原创 2022-01-29 18:06:41 · 321 阅读 · 0 评论 -
LeetCode 707. Design Linked List Sol.
LeetCode官方教程参考答案,穷学生只充了一个月的会员,记录下来备忘Design your implementation of the linked list. You can choose to use a singly or doubly linked list.A node in a singly linked list should have two attributes:valandnext.valis the value of the current node, andne...原创 2022-01-29 01:27:32 · 136 阅读 · 0 评论 -
LeetCode 学习笔记
56. Merge Intervals(合并区间)给出一个区间的集合, 请合并所有重叠的区间。示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].下面贴出自己写的有点繁琐的代码。。。除了sort直接借用STL快排,好在剩下的部分一次遍历就解决了(≧▽≦),而且也没有占用额外的空间。这个题目的解题思路是,如果第一个区间的上界小于第二个区间...原创 2018-04-09 22:37:14 · 163 阅读 · 0 评论 -
Leetcode学习笔记-556. 下一个更大元素 III
556. 下一个更大元素 III给定一个32位正整数 n,你需要找到最小的32位整数,其与 n 中存在的位数完全相同,并且其值大于n。如果不存在这样的32位整数,则返回-1。示例 1:输入: 12输出: 21示例 2:输入: 21输出: -1class Solution {public: static bool compare(int a,int b){ retu...原创 2018-04-15 13:48:06 · 633 阅读 · 0 评论 -
Leetcode 学习笔记
2.两数相加问题描述给定两个链表分别代表两个非负整数,链表的每个结点分别存储整数的每位数字,且是逆序存储,即:数字最低位存储在链表表头,数字最高位存储在链表表尾。求解这两个整数的和并以相同的链表形式返回计算的结果。例如: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8下面贴出官方代码(C++)class S...原创 2018-04-05 10:27:27 · 286 阅读 · 0 评论 -
LeetCode 学习笔记
48. 旋转图像给定一个 n × n 的二维矩阵表示一个图像。将图像旋转 90 度(顺时针)。注意:你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像。例 1:给出的输入矩阵 = [ [1,2,3], [4,5,6], [7,8,9]],旋转输入矩阵,使其变为 :[ [7,4,1], [8,5,2], [9,6,3]] 例 2:给出的输入矩阵 =[ ...原创 2018-04-07 11:17:12 · 160 阅读 · 0 评论 -
145. 二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历。示例:输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1]进阶: 递归算法很简单,你可以通过迭代算法完成吗?解题思路:(迭代)按照“正常”的解题思路,先左子树后右子树最后结点本身实在想不出来……可能因为笨吧所以采用懒人算法。先序遍历应该大家都懂,这里不赘述了。正常的先序遍历是中->左-...原创 2018-04-23 00:39:52 · 264 阅读 · 0 评论