自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(81)
  • 收藏
  • 关注

原创 三步问题

#include <iostream>#include <vector>using namespace std;/*题目描述:三步问题。有个小孩正在上楼梯,楼梯有n阶台阶,小孩一次可以上1阶、2阶或3阶。实现一种方法,计算小孩有多少种上楼梯的方式。结果可能很大,你需要对结果模1000000007。思考:自底向上动态规划。memo[i]=memo[i-1]+memo[i-2]+memo[i-3]。 memo[1]=1,memo[2]=

2021-05-01 17:21:24 125

原创 剑指 Offer 42. 连续子数组的最大和

最长子序和问题;动态规划#include <iostream>#include <vector>#include <algorithm>using namespace std;/*题目描述:输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。 求所有子数组的和的最大值。要求时间复杂度为O(n)。思考:动态规划。memo[i]表示以i为结尾的子数组和的最大值。 memo[i]=max(memo[i-1]+nums[i]

2021-05-01 16:31:28 96

原创 lc392.判断子序列

#include <iostream>#include <vector>using namespace std;/*题目描述:给定字符串 s 和 t ,判断 s 是否为 t 的子序列。字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。思考:暴力解法:双重循环,在t中找s中元素,找到后退出内层循环并记录下该索引,选取下个t中 元素

2021-05-01 14:33:28 119

原创 lc121买股票最佳时机

动态规划#include <iostream>#include <vector>#include <algorithm>using namespace std;/*题目描述:给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。返回你可以从这笔交易中获取的最大利润。如果你不能获取任何

2021-05-01 11:39:08 76

原创 leetcode53.最大子序和

动态规划#include <iostream>#include <vector>#include <algorithm>using namespace std;/*题目描述:给定一个整数数组 nums ,找到一个具有最大和的连续子数组 (子数组最少包含一个元素),返回其最大和。思考:一个约束条件下的最优化问题。 自顶向下思考,递归,f(i)=max(f(i-1)+v[i],f(i-1)); 自底向上,填满MAX[0..

2021-04-30 13:18:53 60

原创 N Queens

回溯法#include <iostream>#include <vector>#include <cassert>using namespace std;/*问题描述:在棋盘格子中的每一行都摆上皇后,确保每个皇后的同一行、同一列、正对角线、反对角线 上都没有皇后。、思考:使用回溯法。首先在第一行第一个位置摆放,在满足“规则”的情况下在第二行找位置摆放,依次类推。 当某行不存在符合“规则”的摆放位置时,回溯到上一行,尝试该行满足

2021-04-30 11:21:58 62

原创 Number of Islands

floodfill;二维平面//floodfill算法,二维平面上的回溯#include <iostream>#include <vector>using namespace std;/*题目描述:给定一个二维阵列,“1代表岛屿,“0”代表水域。返回给二维阵列中的岛屿数目。*/class Solution{private: //辅助的变量、函数(同word search) //尝试向四个方向位移的辅助数组 int d[4][2]={{0,1

2021-04-28 10:22:32 46

原创 Word Search

二维平面中的回溯法;//二维平面上的回溯法#include <iostream>#include <vector>#include <cassert>using namespace std;/*问题描述:给定一个二维平面的字母和一个单词,判断是否可以通过该二维平面找到该单词。 */class Solution{private: //向上、右、下、左进行移动(自上开始的顺时针顺序) int d[4][2]={{-1,0},{0,1}

2021-04-27 16:21:28 388

原创 Combination

回溯法;组合问题//回溯法解决组合类问题#include <iostream>#include <vector>using namespace std;/*问题描述:给定两个数字n和k,求在1...n这n个数字中选出k个数字所有组合。思考:大体参见排列类问题。*/class Solution{private: vector<vector<int>> res; //从start位置开始找新的元素,已有的组合存

2021-04-27 11:12:16 125

原创 Permutations

回溯法;排列类问题//回溯法解决排列类问题#include <iostream>#include <vector>using namespace std;/*问题描述:给定一个整型数组,其中每一个元素都各不相同,返回这些元素所有排列的可能。思考:可将问题抽象为树形结构问题,于是可进一步采用回溯法解决; 函数表达。Perms(nums[0...n-1])={取出一个数字}+Perms(nums[{0...n-1-该数字}])*/class Solut

2021-04-27 09:48:51 77

原创 Letter Combinations of a Phone Number

递归结构;回溯法;树形问题;一对多的数据存储处理;#include <iostream>#include <vector>#include <cassert>using namespace std;/*问题描述:电话拨号键上的数字(只考虑2、3、...、9)对应着多个字母,给定一个数字字符串,返回该数字字符串所有可以表达的 字母组合。思考:每一个数字对应着诺干个字母,问题天然为一个一对多的树形结构,每个叶子节点就是一个字母组合;

2021-04-26 13:50:43 64

原创 Climbing Stairs

递归、记忆化搜索改进递归、自底向上动态规划#include <iostream>#include <vector>using namespace std;/*问题描述:有一个n阶楼梯。每一次,可以上一个台阶,也可以上两个台阶。问爬上这样一个楼梯,有多少种不同的方法。思考:将问题抽象之后,就是斐波那契数列问题。思路一:递归。 问题的描述天然就是一个递归结构。F(n)=F(n-1)+F(n-2),爬上第n级台阶,可以从n-1级 上一个台阶,也可以从n-2

2021-04-24 10:18:35 53

原创 斐波那契数列

递归、加入记忆化搜索的递归、自底向上动态规划#include <iostream>#include <vector>using namespace std;/*问题描述:求解斐波那契数列问题。思考:显然是一个递归的过程。思路一:递归。思路二:显然看到,在递归的过程中,存在很多重叠子问题,因此可以通过存储这些子问题的结果,达到优化思路三:自底向上分析,动态规划*///思路一:递归int Fib1(int n){ if(n==0){

2021-04-23 20:39:04 39

原创 最长上升子序列

自底向上递推,动态规划#include <iostream>#include <vector>using namespace std;/*问题描述:给定一个整数序列,求最长上升子序列的长度。思考:抽象为给定元素下的选取组合最优解问题。对于每个整数,都有取/不取两种策略,故共有(2^n)*n种 策略,这也是暴力解法的时间复杂度。 常规思路:递归、改进递归(记忆化搜索)、自底向上动态规划。 函数定义:LIS(i)表示第i个数字为结尾的最长上升

2021-04-22 21:52:30 45

原创 经典0-1背包问题

三种思路:递归、改进递归(记忆化搜索)、自底向上动态规划#include <iostream>#include <vector>#include <algorithm>using namespace std;/*问题描述:一个背包,容量为C。现在有n种不同的物品,编号为0-n-1,其中每一种 物品的重量为w(i),价值为v(i)。问如何将物品装入背包使总价值量 最大。思考:双约束下的选取组合最优解问题,物品数、

2021-04-22 20:41:50 135

原创 leetcode236. 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 node...

2020-03-30 16:58:17 122

原创 leetcode230. 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,...

2020-03-28 16:19:56 129

原创 leetcode108. Convert Sorted Array to Binary Search Tree

题目描述Given an array 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 the depth of...

2020-03-28 16:06:44 172

原创 leetcode98. 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.Th...

2020-03-27 16:29:10 108

原创 leetcode235. Lowest Common Ancestor of a Binary Search Tree

题目描述Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw...

2020-03-26 20:09:25 112

原创 leetcode437. Path Sum III(递归嵌套递归)

题目描述You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it ...

2020-03-25 16:56:13 88

原创 leetcode404. Sum of Left Leaves

题目描述Find the sum of all left leaves in a given binary tree.Example:3/ 9 20/ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.来源:力扣(LeetCode)...

2020-03-22 16:59:41 83

原创 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.Examp...

2020-03-21 16:42:48 65

原创 leetcode222. Count Complete Tree Nodes(只掌握了普适方法)

题目描述Given a complete binary tree, count the number of nodes.Note:Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely ...

2020-03-19 17:11:42 72

原创 leetcode101. Symmetric Tree

题目描述Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric:1/ 2 2/ \ / 3 4 4 3But the fol...

2020-03-19 16:43:39 111

原创 leetcode100. 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.Examp...

2020-03-19 16:31:01 89

原创 leetcode226. Invert Binary Tree

题目描述Invert a binary tree.Example:Input: 4/ 2 7/ \ / 1 3 6 9Output: 4/ 7 2/ \ / 9 6 3 1代码递归#include <iostream>using namespace std;/// Definition fo...

2020-03-18 17:31:00 91

原创 leetcode104. 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 childr...

2020-03-16 17:02:25 108

原创 优先队列

#include <iostream>#include <queue>#include <ctime>using namespace std;bool myCmp(int a , int b){ if(a%10 != b%10) //比较个位数 return a%10 > b%10; return a >...

2020-03-12 16:10:34 72

原创 leetcode279. Perfect Squares

题目描述Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.Example 1:Input: n = 12Output: 3Explanation: 12 = 4 + 4 + 4.来源:力扣(Lee...

2020-03-11 17:42:03 100

原创 leetcode199. 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,...

2020-03-10 17:14:37 98

原创 leetcode103. Binary Tree Zigzag Level Order Traversal

题目描述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).For example:Given binary...

2020-03-10 16:59:17 72

原创 leetcode107. Binary Tree Level Order Traversal II(同102)

题目描述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,...

2020-03-09 17:01:08 86

原创 leetcode102. 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 7...

2020-03-09 16:19:50 88

原创 leetcode341. Flatten Nested List Iterator(浅)

题目描述Given a nested list of integers, implement an iterator to flatten it.Each element is either an integer, or a list – whose elements may also be integers or other lists.Example 1:Input: [[1,1],2...

2020-03-09 15:41:13 61

原创 leetcode145. Binary Tree Postorder Traversal

题目描述Given a binary tree, return the postorder traversal of its nodes’ values.Example:Input: [1,null,2,3]12/3Output: [3,2,1]来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/binary-tree-pos...

2020-03-06 21:22:35 74

原创 leetcode94. Binary Tree Inorder Traversal(同144)

题目描述Given a binary tree, return the inorder traversal of its nodes’ values.Example:Input: [1,null,2,3]12/3Output: [1,3,2]来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/binary-tree-inord...

2020-03-06 21:08:42 103

原创 leetcode144. 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]来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/binary-tree-preo...

2020-03-06 20:56:13 124

原创 leetcode150. Evaluate Reverse Polish Notation

题目描述Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Note:Division between two integer...

2020-03-04 16:52:16 208

原创 leetcode20. Valid Parentheses

题目描述Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type o...

2020-03-04 16:26:16 136

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除