自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 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 135

原创 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 145

原创 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 125

原创 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 70

原创 557. 反转字符串中的单词 III

给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。示例 1:输入: “Let’s take LeetCode contest”输出: “s’teL ekat edoCteeL tsetnoc”注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。方法一:(istringstream)//istringstream对象可以绑定一行...

2020-05-06 16:53:50 109

原创 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 59

原创 leetcode 19. 删除链表的倒数第N个节点

给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。示例:给定一个链表: 1->2->3->4->5, 和 n = 2.当删除了倒数第二个节点后,链表变为 1->2->3->5.说明:给定的 n 保证是有效的。进阶:你能尝试使用一趟扫描实现吗?使用快慢指针方法一://快慢指针/** * Definition for ...

2020-04-27 16:52:11 92 1

原创 leetcode 3. 无重复字符的最长子串

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。示例 1:输入: “abcabcbb”输出: 3解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。示例 2:输入: “bbbbb”输出: 1解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。示例 3:输入: “pwwkew”输出: 3解释: 因为无重复字符的最长子串是 “wke”,所...

2020-04-27 15:49:01 62

原创 pat树问题总结2 dfs,bfs

目录根据前中后三种遍历构建树dfs,bfs堆。结合dfs、完全二叉树的一些性质注:dfs写出路径的一般写法,先写出边界条件,在此条件下输出保存路径的数组,然后通过下面所示的方法,将路径的index放入保存路径的数组,并递归。(结合A1053和A1155)(A1053)给出树的结构和权值,找出从根节点到叶子结点路径上权值相加之和等于给定数字的路径,并且从大到小输出路径。分析:模板题。...

2019-07-26 17:32:53 172

原创 pat树问题总结3 建树

并不是说其他题不用建树,只是这类题是真正用一个build函数建。其实要看他们的输入,一般来说都会给出二叉树所有结点的data。(A1115)输出一个二叉搜索树最后两层的结点个数a和b,以及他们的和c :“a + b = c”点评:build一棵二叉树,dfs确认其深度及每层结点个数,最后按要求加和即可。#include <iostream>#include <vector...

2019-07-26 16:39:39 262

原创 pat树问题总结1 根据前中后三种遍历构建树

目录根据前中后三种遍历构建树dfs,bfs堆。结合dfs、完全二叉树的一些性质(A1120) 给出一棵二叉树的前序遍历和中序遍历,求这棵树的层序遍历。分析:模板题。#include<iostream>#include<vector>#include<string>#include<queue>#include<algor...

2019-07-24 17:18:59 120

原创 动态规划问题

目录动态规划的递归写法和递推写法典型例题总结1.动态规划的递归写法和递推写法1.1 动态规划的递归算法以斐波那契数列为例,用一般的递归写法写出如下代码:int F(int n){ if(n==0||n==1) return 1; else return F(n-1)+F(n-2);}但上面代码会产生许多的重复计算,为了避免重复计算,可以新开一个一维数组dp,用来保存已经...

2019-06-17 16:09:31 269

原创 并查集

目录并查集的定义并查集的基本操作路径压缩例题1.并查集的定义并查集,顾名思义:Union,Find,Set. 即支持两个操作:合并和查找。并查集如何实现呢?其实就是一个数组int father[N];,其中father[i]表示元素i的父亲结点,而父亲结点本身就是这个集合内的元素。举个例子,如father[1]=2就是说元素1的父亲为2,。另外,当father[i]==i时,则说...

2019-06-05 17:10:59 96

原创 图算法知识点和模板(未完待续)

目录图的储存(邻接表和邻接矩阵)图的遍历(DFS和BFS)最短路径(Dijkstra算法、Bellman-Ford算法与SPFA算法,Floyd算法)最小生成树(Prim算法,Kruskal算法)拓扑排序关键路径1. 图的储存1.1 邻接矩阵就是一个二维数组G[N][N],当G[i][j]=1时,说明顶点i和顶点j存在边;当当G[i][j]=0时,说明顶点i和顶点j不存在边。...

2019-06-05 15:19:40 271

原创 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 59

原创 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 51

原创 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 92

原创 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 185

原创 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 84

原创 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 74

原创 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 111

原创 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 71

原创 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 126

原创 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 76

原创 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 140

原创 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 136

原创 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 139

原创 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 67

原创 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 137

原创 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 70

原创 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 60

原创 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 63

原创 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 62

原创 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 65

原创 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 98

原创 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 65

原创 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 60

原创 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 60

原创 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 99

原创 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 58

空空如也

空空如也

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

TA关注的人

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