
LeetCode
weixin_42741175
这个作者很懒,什么都没留下…
展开
-
199. 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。示例:输入: [1,2,3,null,5,null,4]输出: [1, 3, 4]解释:1 <—/ 2 3 <—\ 5 4 <—两种方法,bfs和dfsbfs://bfs/** * Definition for a binary tree node. * struct TreeNode {原创 2020-05-27 17:21:16 · 221 阅读 · 0 评论 -
78. 子集
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。说明:解集不能包含重复的子集。示例:输入: nums = [1,2,3]输出:[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]]方法一:动态规划//动态规划class Solution {public: vector<vector<int>> subsets(vector<int>& nums) {原创 2020-05-11 20:48:35 · 222 阅读 · 0 评论 -
53. 最大子序和
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。示例:输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。方法一:dpclass Solution {public: int maxSubArray(vector<int>& nums) { in原创 2020-05-11 16:52:33 · 223 阅读 · 0 评论 -
148. 排序链表
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。示例 1:输入: 4->2->1->3输出: 1->2->3->4示例 2:输入: -1->5->3->4->0输出: -1->0->3->4->5用归并排序解决,归并排序有两种写法,所以有两种方法。方法一:(迭代的归并排序)...原创 2020-05-07 15:29:51 · 97 阅读 · 0 评论 -
557. 反转字符串中的单词 III
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。示例 1:输入: “Let’s take LeetCode contest”输出: “s’teL ekat edoCteeL tsetnoc”注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。方法一:(istringstream)//istringstream对象可以绑定一行...原创 2020-05-06 16:53:50 · 142 阅读 · 0 评论 -
231. 2的幂
给定一个整数,编写一个函数来判断它是否是 2 的幂次方。示例 1:输入: 1输出: true解释: 20 = 1示例 2:输入: 16输出: true解释: 24 = 16示例 3:输入: 218输出: false方法一:log函数:若底数为m,则为log(n)/log(m)pow函数: pow(2,3)=2^3round: 四舍五入取整class Solution...原创 2020-05-05 17:21:36 · 118 阅读 · 0 评论 -
leetcode 300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.Example:Input: [10,9,2,5,3,7,101,18]Output: 4Explanation: The longest increasing subsequence is [2,3,7,101], ...原创 2019-05-14 22:20:59 · 91 阅读 · 0 评论 -
leetcode 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node’s key.The rig...原创 2019-05-14 20:18:02 · 81 阅读 · 0 评论 -
leetcode 501.Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.Assume a BST is defined as follows:The left subtree of a node contains ...原创 2019-05-13 22:24:42 · 120 阅读 · 0 评论 -
leetcode 72. Edit Distance 编辑距离(字符串动态规划)
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.You have the following 3 operations permitted on a word:Insert a characterDelete a characte...原创 2019-05-12 22:27:57 · 241 阅读 · 0 评论 -
leetcode 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Example 1:Input: root = [3,1,4,n...原创 2019-05-12 16:56:30 · 113 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p a...原创 2019-05-12 16:06:29 · 106 阅读 · 0 评论 -
leetcode 455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a coo...原创 2019-05-11 17:19:32 · 144 阅读 · 0 评论 -
leetcode 96. Unique Binary Search Trees (好题)
Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n?Example:Input: 3Output: 5Explanation:Given n = 3, there are a total of 5 unique BST’s:1 3 3 ...原创 2019-05-11 16:56:24 · 109 阅读 · 0 评论 -
leetcode 174. Dungeon Game (动态规划经典题)
The demons had captured the princess § and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially posit...原创 2019-05-09 21:32:20 · 159 阅读 · 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 at an...原创 2019-05-09 20:24:40 · 107 阅读 · 0 评论 -
leetcode 120.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],[3,4],[6,5,7],[4,1,8,3]...原创 2019-05-08 16:52:05 · 197 阅读 · 0 评论 -
leetcode 53. Maximum Subarray(动态规划经典题)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation:...原创 2019-05-08 15:33:09 · 168 阅读 · 0 评论 -
Leetcode 198. House Robber(动态规划经典题)
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house...原创 2019-05-08 15:01:13 · 174 阅读 · 0 评论 -
LeetCode 70. 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?Note: Given n will be a positive i...原创 2019-05-07 23:56:33 · 102 阅读 · 0 评论 -
leetcode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.Example:Input: 1->2->6->3->4->5->6, val = 6Output: 1->2->3->4->5大意:移除等于val的元素(可能不止一个)方法:遍历链表,...原创 2019-05-07 23:38:22 · 224 阅读 · 0 评论 -
leetcode 122. Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one...原创 2019-05-07 23:23:14 · 113 阅读 · 0 评论 -
leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes’ values.Example:Input: [1,null,2,3]12/3Output: [1,2,3]Follow up: Recursive solution is trivial, could you do it iteratively?大意...原创 2019-05-07 23:11:29 · 87 阅读 · 0 评论 -
leetcode 114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example, given the following tree:1/ 2 5/ \ 3 4 6The flattened tree should look like:123456大意:将二叉树展开为链表方法:有两种展...原创 2019-05-07 22:54:28 · 92 阅读 · 0 评论 -
leetcode 55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you ...原创 2019-05-06 23:27:08 · 89 阅读 · 0 评论 -
leetcode 113. Path Sum II
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.Example:Given the below binary tree and sum = 22, 5 ...原创 2019-05-06 23:14:37 · 89 阅读 · 0 评论 -
leetcode112. 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.Note: A leaf is a node with no children.Example:G...原创 2019-05-06 22:49:08 · 172 阅读 · 0 评论 -
leetcode 61. Rotate List
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->NULLExplanati...原创 2019-05-06 20:36:39 · 110 阅读 · 0 评论 -
leetcode92. Reverse Linked List II
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->...原创 2019-05-06 20:19:48 · 90 阅读 · 0 评论 -
leetcode206. Reverse Linked List
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 or recursively. ...原创 2019-05-06 19:39:10 · 89 阅读 · 0 评论 -
leetcode 199. 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.Example:Input: [1,2,3,null,5,null,4]Output: [1, 3, 4]E...原创 2019-05-05 09:55:41 · 169 阅读 · 0 评论 -
LeetCode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,null,15,7]...原创 2019-05-05 09:32:33 · 84 阅读 · 0 评论 -
LeetCode 104. 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.Note: A leaf is a node with no children....原创 2019-05-05 08:59:45 · 97 阅读 · 0 评论 -
LeetCode 102. 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,null,null,15,7],3/ 9 20/ 15 7retu...原创 2019-05-04 23:19:25 · 86 阅读 · 0 评论 -
LeetCode 100. Same Tree
Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Example 1:...原创 2019-05-04 23:10:39 · 82 阅读 · 0 评论 -
LeetCode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.Example:Input: [1,null,2,3]12/3Output: [1,3,2]Follow up: Recursive solution is trivial, could you do it iteratively?*中文...原创 2019-05-04 23:04:19 · 90 阅读 · 0 评论