LeetCode
文章平均质量分 88
volador_r
这个作者很懒,什么都没留下…
展开
-
LeetCode 841. Keys and Rooms 钥匙和房间(Java)
LeetCode 841. Keys and Rooms 钥匙和房间(Java)原创 2022-07-02 16:10:59 · 369 阅读 · 0 评论 -
LeetCode 138. Copy List with Random Pointer 复制带随机指针的链表(Java)
题目: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.The Linked List is represented in the input/output as a list of n nodes. Each node is re原创 2020-07-06 23:00:12 · 243 阅读 · 0 评论 -
LeetCode 137. Single Number II 只出现一次的数字(Java)
题目:Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?E原创 2020-05-11 13:30:30 · 208 阅读 · 0 评论 -
LeetCode 134. Gas Station 加油站(Java)
题目:There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an emp原创 2020-05-10 16:22:47 · 243 阅读 · 0 评论 -
LeetCode 133. Clone Graph 克隆图(Java)
题目:Given a reference of a node in a connected undirected graph.Return a deep copy (clone) of the graph.Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.class No...原创 2020-05-08 11:20:27 · 334 阅读 · 0 评论 -
LeetCode 131. Palindrome Partitioning 分割回文串(Java)
题目: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...原创 2020-05-06 16:16:16 · 202 阅读 · 0 评论 -
LeetCode 130. Surrounded Regions 被围绕的区域(Java)
题目:Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions surrounded by ‘X’.A region is captured by flipping all 'O’s into 'X’s in that surrounded region.Example:X X X XX O ...原创 2020-05-06 11:29:16 · 276 阅读 · 0 评论 -
LeetCode 129. Sum Root to Leaf Numbers 根到叶子节点数字之和(Java)
题目: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.Find the to...原创 2020-05-05 16:37:58 · 200 阅读 · 0 评论 -
LeetCode 127. Word Ladder 单词接龙(Java)
题目:Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at ...原创 2020-04-28 20:09:16 · 237 阅读 · 0 评论 -
LeetCode 120. Triangle 三角形最小路径和(Java)
题目: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], [3,4], [6,...原创 2020-04-21 23:43:52 · 428 阅读 · 0 评论 -
LeetCode 117. Populating Next Right Pointers in Each Node II 填充每个节点的下一个右侧节点指针II(Java)
题目:Given a binary treestruct Node { int val; Node *left; Node *right; Node *next;}Populate each next pointer to point to its next right node. If there is no next right node, the next poin...原创 2020-04-21 21:14:28 · 242 阅读 · 0 评论 -
LeetCode116. Populating Next Right Pointers in Each Node 填充每个节点的下一个右侧节点指针(Java)
题目:You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:struct Node { int val; Node *left;...原创 2020-04-20 21:38:27 · 213 阅读 · 0 评论 -
LeetCode 114. Flatten Binary Tree to Linked List 二叉树展开为链表(Java)
题目:Given a binary tree, flatten it to a linked list in-place.解答:本题主要是怎样在原地 将二叉树转换为链表我采用的思路如下:判断 root.left 是否为空,若不为空,则找到 temp 节点为左子树的最右节点将 root 的右子树连接到左子树的最右节点 temp将左子树连接为 root 的右子树,并将 root 左子...原创 2020-04-20 19:57:43 · 188 阅读 · 0 评论 -
LeetCode 113. Path Sum II 寻找二叉树路径总和II(Java)
题目:Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.Note: A leaf is a node with no children.解答:本题为 LeetCode 112 题的进阶版。通过 DFS 深度优先搜索,找到满足的路径。在这...原创 2020-04-17 17:50:50 · 221 阅读 · 0 评论 -
LeetCode 109. Convert Sorted List to Binary Search Tree 有序链表转换二叉搜索树(Java)
题目: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 which t...原创 2020-04-17 15:39:22 · 178 阅读 · 0 评论 -
106. Construct Binary Tree from Inorder and Postorder Traversal 根据中序、后序遍历序列构建二叉树(Java)
题目:Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.解答:本题根据树的中序遍历和后序遍历,得到树的结构。与 LeetCode 105 思路基本一致整体思路为:...原创 2020-04-17 12:39:53 · 258 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal 根据前序、中序遍历序列构建二叉树(Java)
题目:Given preorder and inorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.解答:题目根据树的前序遍历和中序遍历,得到树的结构整体思路为:首先我们知道,前序遍历的第一个节点一定是根节点...原创 2020-04-17 12:13:33 · 245 阅读 · 0 评论 -
LeetCode 103. Binary Tree Zigzag Level Order Traversal 二叉树Z字形层级遍历(Java)
题目:Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).解答:解法一:堆栈与 LeetCode 102...原创 2020-04-14 16:01:16 · 229 阅读 · 0 评论 -
LeetCode 102. Binary Tree Level Order Traversal 二叉树的层级遍历(Java)
题目:Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).解答:解法一:这道题为二叉树的层级遍历借助ArrayList,实现了层级遍历,具体思路为:创建 list,用于存储当前遍历层的节点创建 nex...原创 2020-04-14 13:33:23 · 160 阅读 · 0 评论 -
LeetCode 96. Unique Binary Search Trees 不同的二叉搜索树(Java)
题目:Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n?解答:这道题与 LeetCode 95 题基本一致,但解法不相同。也是采用动态规划的思想,二叉搜索树的个数 = 左子树的个数 * 右子树的个数因此把问题换分为两个子问题,及求解当前根节点情况下,左子树与...原创 2020-04-13 22:14:43 · 163 阅读 · 0 评论 -
LeetCode 95. Unique Binary Search Trees II 不同的二叉搜索树II(Java)
题目:Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n.解答:本题没有想到解题思路,所以采用了官方给的解法,通过递归实现,值得好好学习。首先二叉搜索树的特点是:左子树 < 根节点 < 右子树所以要找到 1-n 所有的...原创 2020-04-13 21:17:20 · 179 阅读 · 0 评论 -
LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历(Java)
题目:Given a binary tree, return the inorder traversal of its nodes’ values.Follow up: Recursive solution is trivial, could you do it iteratively?解答:二叉树的中序遍历,很基础的题目,递归方法比较简单,题目要求我们尝试用非递归的方式求解,则借助栈 ...原创 2020-04-09 16:10:49 · 177 阅读 · 0 评论 -
LeetCode 93. Restore IP Addresses 复原IP地址(Java)
题目:Given a string containing only digits, restore it by returning all possible valid IP address combinations.Example:Input: “25525511135”Output: [“255.255.11.135”, “255.255.111.35”]解答:这类问题基本采用...原创 2020-04-09 15:38:21 · 153 阅读 · 0 评论 -
LeetCode 92. Reverse Linked List II 反转链表II(Java)
题目:Reverse a linked list from position m to n. 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->2->5...原创 2020-04-08 17:13:06 · 229 阅读 · 0 评论 -
LeetCode 91. Decode Ways 解码方式(Java)
题目:A message containing letters from A-Z is being encoded to numbers using the following mapping:‘A’ -> 1‘B’ -> 2…‘Z’ -> 26Given a non-empty string containing only digits, determine t...原创 2020-04-08 13:46:20 · 226 阅读 · 0 评论 -
LeetCode 90. Subsets II 子集II(Java)
题目:Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: [1,2,2]...原创 2020-04-07 17:35:35 · 215 阅读 · 0 评论 -
LeetCode 86. Partition List 分隔链表(Java)
题目: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 nodes in each o...原创 2020-04-07 16:51:56 · 156 阅读 · 0 评论 -
LeetCode 82. Remove Duplicates from Sorted List II 删除排序链表中的重复元素(Java)
题目:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.Return the linked list sorted as well.Example 1:Input: 1->2-&g...原创 2020-04-06 20:37:17 · 175 阅读 · 0 评论 -
LeetCode 81. Search in Rotated Sorted Array II 搜索旋转排序数组II(Java)
题目:Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).You are given a target value to search. If foun...原创 2020-04-06 19:01:18 · 150 阅读 · 0 评论 -
LeetCode 80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项II(Java)
题目:Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.Do not allocate extra space for another array, you must do this by ...原创 2020-04-04 20:42:31 · 199 阅读 · 0 评论 -
LeetCode 79. Word Search 单次搜索
题目:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or verticall...原创 2020-04-04 19:04:28 · 189 阅读 · 0 评论 -
LeetCode 78. Subsets 子集(Java)
题目:Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: nums = [1,2,3]Output:[[3],[1],...原创 2020-04-03 16:18:13 · 256 阅读 · 0 评论 -
LeetCode 77. Combinations 组合(Java)
题目:Given two integers n and k, return all possible combinations of k numbers out of 1 … n.Example:Input: n = 4, k = 2Output:[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]解答:解法一:回溯法这是一道明显回溯的题目...原创 2020-04-03 15:37:40 · 163 阅读 · 0 评论 -
LeetCode 75. Sort Colors 颜色分类
题目:Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the...原创 2020-04-03 12:21:46 · 148 阅读 · 0 评论 -
LeetCode 74. Search a 2D Matrix 搜索二维矩阵
题目: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 from left to right.The first integer of each ...原创 2020-04-02 21:29:35 · 119 阅读 · 0 评论 -
LeetCode 73. Set Matrix Zeroes 矩阵置零(Java)
题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Example 1:Input:[[1,1,1],[1,0,1],[1,1,1]]Output:[[1,0,1],[0,0,0],[1,0,1]]Example 2:In...原创 2020-04-02 20:43:21 · 151 阅读 · 0 评论 -
LeetCode 64. Minimum Path Sum 最小路径和
题目:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right a...原创 2020-04-01 17:41:12 · 129 阅读 · 0 评论 -
LeetCode 62/63. Unique Paths 不同路径I/II(Java)
题目一:A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach th...原创 2020-03-30 14:09:10 · 197 阅读 · 0 评论 -
LeetCode 61. Rotate List 旋转链表(Java)
题目:Given a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExpl...原创 2020-03-30 11:03:52 · 212 阅读 · 0 评论 -
LeetCode 60. Permutation Sequence 第k个排列(Java)
题目:The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, we get the following sequence for n = 3:“123”“132”“213”“231”“312”“3...原创 2020-03-29 20:40:22 · 197 阅读 · 0 评论