![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 72
fight_to_dead
这个作者很懒,什么都没留下…
展开
-
Leetcode Add Two Numbers 两个链表表示的数相加
题目:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return原创 2015-05-07 20:00:07 · 461 阅读 · 0 评论 -
Leetcode Construct Binary Tree from Preorder and Inorder Traversal 前序中序遍历重组二叉树
题目:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.分析:1. 前序遍历的第一个节点为树的根节点2. 在中序遍历中找到根原创 2015-05-13 16:29:44 · 432 阅读 · 0 评论 -
Leetcode Construct Binary Tree from Inorder and Postorder Traversal 中序后序遍历重组二叉树
题目:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.分析:1. 后序遍历的最后一个节点为树的根节点2. 找到根节点在中原创 2015-05-13 17:00:21 · 445 阅读 · 0 评论 -
Leetcode Minimum Depth of Binary Tree 二叉树最小深度
题目:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.分析:这道题可以和求最大深度一原创 2015-05-13 17:43:17 · 319 阅读 · 0 评论 -
Leetcode Remove Duplicates from Sorted List II 删除链表中重复的元素II
题目:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.原创 2015-05-05 21:48:56 · 393 阅读 · 0 评论 -
Leetcode Remove Linked List Elements 删除链表中的元素
题目:Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5分析:这道原创 2015-05-05 20:55:22 · 314 阅读 · 0 评论 -
Leetcode Remove Duplicates from Sorted List 删除链表中的重复元素
题目:Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.分析:这原创 2015-05-05 21:10:25 · 347 阅读 · 0 评论 -
Leetcode Balanced Binary Tree 平衡二叉树
题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every nod原创 2015-05-12 14:55:22 · 329 阅读 · 0 评论 -
Leetcode Convert Sorted Array to Binary Search Tree 有序数组转换成二叉搜索树BST
题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.分析:这道题和有序链表转换成BST的方法相同,都是用递归。 1. 递归的结束条件是当start>=end2. 找到数组的中点,作为根节点3. 递归的在左半边构建原创 2015-05-12 12:20:47 · 462 阅读 · 1 评论 -
Leetcode Triangle
题目:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2],原创 2015-05-12 11:34:33 · 255 阅读 · 0 评论 -
Leetcode Single Number
题目:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear run time complexity. Could you implement it wit原创 2015-05-12 18:19:01 · 252 阅读 · 0 评论 -
Leetcode Insertion Sort List 插入排序链表
题目:Sort a linked list using insertion sort.分析:1. 创建一个新的链表。2. 对原链表中的每个元素,从新链表表头开始找该元素插入的位置,找到后插入元素。Java代码实现:/** * Definition for singly-linked list. * public class L原创 2015-05-06 15:34:25 · 260 阅读 · 0 评论 -
Leetcode Reverse Linked List 反转单链表
题目:Reverse a singly linked list.分析:1. 反转后头结点的next为null。2. 需要同时记录三个节点,分别是当前节点,它的前节点和next节点。Java代码实现:/** * Definition for singly-linked list. * public class ListNode原创 2015-05-06 11:25:03 · 411 阅读 · 0 评论 -
Leetcode Partition List 分割链表
题目:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nod原创 2015-05-07 21:38:59 · 440 阅读 · 0 评论 -
Leetcode Intersection of Two Linked Lists 单链表交叉
题目:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2015-05-07 14:30:46 · 290 阅读 · 0 评论 -
Leetcode Sum Root to Leaf Numbers
题目:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.原创 2015-05-14 21:55:52 · 379 阅读 · 0 评论 -
Leetcode Linked List Cycle 循环链表
题目:Given a linked list, determine if it has a cycle in it.分析:1. 使用快慢指针,如果有环,快慢指针会相遇,如果没有环,快指针会首先到达null。2. fast = fast.next.next语句注意fast.next可能已经是null,这是会出现空指针错误。因此,需要先进行判断。Java代码原创 2015-05-07 14:42:26 · 197 阅读 · 0 评论 -
Leetcode Path Sum
题目:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary原创 2015-05-14 22:13:41 · 322 阅读 · 0 评论 -
Leetcode Populating Next Right Pointers in Each Node II
题目:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only原创 2015-05-14 10:53:12 · 246 阅读 · 0 评论 -
Leetcode Binary Search Tree Iterator 二叉搜索树迭代器
题目:Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.原创 2015-05-14 11:18:19 · 325 阅读 · 0 评论 -
Leetcode Binary Tree Right Side View
题目:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary原创 2015-05-14 11:32:03 · 292 阅读 · 0 评论 -
Leetcode Populating Next Right Pointers in Each Node
题目:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next原创 2015-05-14 10:41:48 · 284 阅读 · 0 评论 -
Leetcode Reverse Linked List II 反转部分单向链表
题目:Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.分析:1. 链表原创 2015-05-06 12:07:39 · 343 阅读 · 0 评论 -
Leetcode Climbing Stairs 爬楼梯
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?分析:这道题属于动原创 2015-05-11 20:34:50 · 311 阅读 · 0 评论 -
Leetcode Binary Tree Postorder Traversal 二叉树后序遍历
题目:Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Re原创 2015-05-11 19:48:03 · 274 阅读 · 0 评论 -
Leetcode Same Tree 判断两棵二叉树是否相同
题目:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.原创 2015-05-11 11:53:57 · 563 阅读 · 0 评论 -
Leetcode Sort List 链表排序
题目:Sort a linked list in O(n log n) time using constant space complexity.分析:选择用归并排序来解决。但是找中点的时候需要注意对于 1 --> 2 --> null 的情况,我们需要mid为1,才能把1和2分开,找中点的时候让fast先走一步即可。1. 如果链表为空或只有一个节点原创 2015-05-11 11:19:03 · 238 阅读 · 0 评论 -
Leetcode Reorder List 链表重排序
题目:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, r原创 2015-05-10 11:42:39 · 316 阅读 · 0 评论 -
Leetcode Reverse Nodes in k-Group
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should re原创 2015-05-10 16:00:47 · 211 阅读 · 0 评论 -
Leetcode Convert Sorted List to Binary Search Tree 把有序链表转换成二叉搜索树
题目:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.分析:举一个例子, 假如链表为 1--> 2 --> 3 --> 4 --> 5 --> 6 --> 7。 根据二叉搜索树的性质 root.va原创 2015-05-10 10:13:49 · 426 阅读 · 0 评论 -
Leetcode Rotate List 旋转链表
题目:Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.分析:1. k的值可能大于链原创 2015-05-09 14:31:15 · 372 阅读 · 0 评论 -
Leetcode Merge k Sorted Lists 合并k个链表
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.分析:1. 用暴力的方法一个一个合并链表会超时,分析所需要的时间复杂度:假设链表的长度为n,每两个链表合并所需要的时间为n+n,k个链表一共需要合并k原创 2015-05-09 14:03:05 · 348 阅读 · 0 评论 -
Leetcode Remove Nth Node From End of List 删除链表倒数第n个元素
题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from原创 2015-05-08 18:41:56 · 279 阅读 · 0 评论 -
Leetcode 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.分析:1. 遍历链表,原创 2015-05-08 17:02:46 · 274 阅读 · 0 评论 -
Leetcode Linked List Cycle II 循环链表入口
题目:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.分析:个人觉得这题的原理没有必要一定用数学公式深究,记住这个方法就可以,因为对别的题并不具备推广性,有兴趣的话网上的数学推导也有很多。1. 首先利用快慢原创 2015-05-08 15:39:32 · 313 阅读 · 0 评论 -
Leetcode Maximum Depth of Binary Tree 二叉树最大深度
题目:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.分析:递归的向下找, 取左右子原创 2015-05-11 12:06:15 · 349 阅读 · 0 评论 -
Leetcode Binary Tree Level Order Traversal 二叉树分层遍历
题目:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \原创 2015-05-11 14:07:00 · 292 阅读 · 0 评论 -
Leetcode Best Time to Buy and Sell Stock
题目:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share o原创 2015-05-11 22:32:59 · 306 阅读 · 0 评论 -
Leetcode Binary Tree Inorder Traversal 二叉树中序遍历
题目:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recu原创 2015-05-11 18:54:48 · 250 阅读 · 0 评论 -
Leetcode Unique Paths II
题目:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectiv原创 2015-05-11 22:58:51 · 249 阅读 · 0 评论