leetcode
我真的不会Coding
任何时候努力都不晚,要保持自信
展开
-
leetcode刷题精讲地址
https://github.com/azl397985856/leetcode/tree/master/problems大家都是如何刷 LeetCode 的? :https://www.zhihu.com/question/280279208/answer/510354868https://github.com/MisterBooo/LeetCodeAnimation...原创 2020-01-03 10:42:35 · 319 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree最短深度 递归 BFS DFS
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.Note: A leaf is a node with no childre...原创 2018-08-02 22:27:48 · 192 阅读 · 0 评论 -
leetcode回文例题
132. Palindrome Partitioning II 回文分割Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partition...原创 2018-08-04 12:40:17 · 161 阅读 · 0 评论 -
leetcode2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i...转载 2018-07-23 09:52:44 · 104 阅读 · 0 评论 -
leetcode18. 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of tar...转载 2018-07-16 10:13:20 · 176 阅读 · 0 评论 -
110. 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 node never diff...转载 2018-07-29 17:51:23 · 97 阅读 · 0 评论 -
leetcode11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two ...原创 2018-07-15 16:53:32 · 174 阅读 · 0 评论 -
leetcode106. 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.For example, giveninorder = [9,3,15,20,7]postorder = [9...原创 2018-07-29 12:48:13 · 113 阅读 · 0 评论 -
leetcode5最长回文子
在网上找了很多相关算法,一直想证明自己的思路有没有问题,但是leetcode提交时没找到错误,细节上有问题,思路:?1234567891011121314151617181920212223242526272829303132既然是最大连续的回文字符串,那么我可以这样构成dp的状态方程,以string[i]为结尾的最长连续会问字符串dp[i],当然会有这么一个maxlen=max{maxlen,d...原创 2018-07-14 17:54:51 · 190 阅读 · 0 评论 -
leetcode115. Distinct Subsequences
描述:Given a string S and a string T, count the number of distinct subsequences of S which equals T.A subsequence of a string is a new string which is formed from the original string by deleting some (c...原创 2018-07-14 17:53:48 · 144 阅读 · 0 评论 -
leetcode 54 Spiral Matrix&&LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
leetcode 54 Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]O...原创 2018-07-20 09:58:25 · 137 阅读 · 0 评论 -
leetcode89
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Example 1:Input: [1,3,null,null,2] 1 / 3 \ 2Output: [3,1,null,nul...原创 2018-07-27 23:23:46 · 204 阅读 · 0 评论 -
leetcode7. Reverse Integer整数反转
Given a 32-bit signed integer, reverse digits of an integer.Difficulty:EasyExample 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:A...原创 2018-08-01 15:34:32 · 136 阅读 · 0 评论 -
eetcode题解(10): Regular Expression Matching——DP解决正则匹配
两个参考:http://xiaohuiliucuriosity.blogspot.com/2014/12/regular-expression-matching.htmlhttps://www.youtube.com/watch?v=l3hda49XcDE&list=PLrmLmBdmIlpuE5GEMDXWf0PWbBD9Ga1lO问题Implement regular ...原创 2018-08-20 22:22:28 · 478 阅读 · 0 评论 -
LeetCode234_PalindromeLinkedList (判断是否为回文链表)c++
思路一:最容易想到的,把遍历过得结点放进栈中,然后利用栈的后进先出的特性逐个和原链表比较是否相同class Solution {public: bool isPalindrome(ListNode* head) { stack<ListNode*>stack; ListNode *p=head; if...原创 2018-08-20 22:17:01 · 693 阅读 · 0 评论 -
LeetCodeBug-member access within null pointer of type 'struct ListNode
转:https://blog.csdn.net/zy2317878/article/details/79115498写在前面这个BUG是我在做LeetCode的链表一类题目的时候遇到的,觉得还是蛮有代表性的,因为刚开始遇到这个BUG,我完全不知道哪里有问题,还与正确答案反复对比,结果发现完全一样,但我的就报错,后来发现原来是这个知识点完全没有接触过,所以就通过这篇博客来记录一下自己犯得错误...转载 2018-08-20 12:50:17 · 3353 阅读 · 0 评论 -
LeetCode 547. Friend Circles 合并集
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then ...原创 2018-08-26 22:28:00 · 139 阅读 · 0 评论 -
判断一棵二叉树是否是平衡二叉树
输入一棵二叉树,判断该二叉树是否是平衡二叉树。 首先,平衡二叉树的定义:/*平衡二叉搜索树(Balanced Binary Tree)具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树*/ 所以我想到的是递归的方法,判断左右的高度差。 第一种方法用来判断该二叉树是否为平衡二叉树struct TreeNo...原创 2018-08-16 10:26:54 · 324 阅读 · 0 评论 -
存储路径:递归或者是DFSleetcode113. Path Sum II路径之和II递归DFS
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,...原创 2018-08-03 15:20:29 · 208 阅读 · 0 评论 -
leetcode112. Path Sum路径之和 递归
DescriptionHintsSubmissionsDiscussSolutionPick OneGiven 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...原创 2018-08-03 09:24:56 · 169 阅读 · 0 评论 -
leecode97. Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.Example 1:Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"Output: trueExample 2:Input: s1 = "aabcc", s2 = "dbbca", s3...原创 2018-07-13 22:54:11 · 115 阅读 · 0 评论 -
leetcode6. ZigZag Conversion
Share simple C++ solution22.9KVIEWS114Last Edit: July 6, 2018 3:17 PMenze98 114问题描述:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to dis...转载 2018-07-13 22:29:10 · 111 阅读 · 0 评论 -
leetcode41. First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer.Example 1:Input: [1,2,0]Output: 3Example 2:Input: [3,4,-1,1]Output: 2Example 3:Input: [7,8,9,11,12]Ou...原创 2018-07-16 15:57:48 · 223 阅读 · 0 评论 -
leetcode72. 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 characterRe...原创 2018-07-09 09:44:46 · 187 阅读 · 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 any...原创 2018-07-08 22:52:58 · 139 阅读 · 0 评论 -
leetcode63. Unique Paths II
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 the bott...原创 2018-07-08 21:56:03 · 140 阅读 · 0 评论 -
背包问题
背包问题,背包问题详解第一种普通背包,有这样的两个个vector<int>数组,代表每个物品的重量和价值,在这n个物品中选取最合适的物品放进背包中,前提是容量不能超过背包最大含量Capbility解法首先考虑状态转移方程可以这样考虑,当背包容量不同时,他的最大价值都不一样,脑子里可以浮现那个表格。每行代表的意思是当放第i个物品(前i-1已经放进去了一部分),在相应容量j的条件下,最大价...原创 2018-06-10 15:27:11 · 129 阅读 · 0 评论 -
二叉树的层序遍历--队列的使用
层序遍历,顾名思义,就是每次打印一行,这一行代表当前树的一层,如果按照普通要求,就是把每个节点值按照层序从上到下从左到右依次输出,那么为什么会选择队列来保存结点呢头节点放进队列中,队列不为空,然后对队列中的元素依次进行操作rel.push——back(root) while (!rel.empty())BinaryTree* front = rel.front();然后对这个front操作,再把该...原创 2018-06-10 10:50:06 · 1782 阅读 · 0 评论 -
toudao问题House Robber
关于toudao问题的leetcode你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。示例 1:输入: [1,2,3,1] 输出: 4 解释: 偷窃 1 号房屋 (金...翻译 2018-06-01 23:19:01 · 887 阅读 · 0 评论 -
二叉树和递归
leetcode 101判断一颗二叉树是否 镜像对称。1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : va...原创 2018-06-26 09:04:51 · 419 阅读 · 0 评论 -
最大上升子序列最大公共子序列
【LeetCode-面试算法经典】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】这道题记得刷一下最大上升子序列下手角度:状态方程这样入手,首先,LIS(i)表示的是,以第i个数为结尾的最大上升子序列的长度(这个第i个数一定是这个前i个数中最大上升子序列的最后一个数,也就是必须选取的,以往的都是分成可以选或者不选)那么想要知道以i结尾的最大值,就要去...原创 2018-06-06 22:30:29 · 188 阅读 · 0 评论 -
动态规划解最长回文子序列并优化空间复杂度
一个字符串有许多子序列,比如字符串abcfgbda,它的子序列有a、bfg、bfgbd,在这些子序列中肯定有回文字符串。现在要对任意字符串求其最长的回文子序列。注意,本文不是解决最长回文子串,回文子串是连续的,回文子序列是不连续的。 字符串abcfgbda的最长回文子序列为abcba,长度为5。 输入:包含若干行,每行有一个字符串,字符串由大小写字母构成,长度不超过100。 ...转载 2018-07-16 16:48:10 · 488 阅读 · 0 评论 -
leetcode95--树
95Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.Example:Input: 3Output:[ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3],...原创 2018-07-03 21:25:04 · 983 阅读 · 0 评论 -
leetcode:99. Recover Binary Search Tree 难度:hard
问题描述Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Example 1:Input: [1,3,null,null,2] 1 / 3 \ 2Output: [3,1,null,null,2...转载 2018-07-04 14:31:08 · 124 阅读 · 0 评论 -
leetcode 32. Longest Valid Parentheses DP
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.Example 1:Input: "(()"Output: 2Explanation: The longest valid par...转载 2018-07-07 14:24:16 · 209 阅读 · 0 评论 -
leetcode163Sum Closest
问题描述:Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input w...转载 2018-07-06 09:24:23 · 141 阅读 · 0 评论 -
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], ...转载 2018-07-05 22:58:13 · 108 阅读 · 0 评论 -
leetcode45. Jump Game II&&55. Jump Game
Jump GameGiven 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.Dete...转载 2018-07-18 11:02:18 · 338 阅读 · 0 评论 -
leetcode123. Best Time to Buy and Sell Stock III
It's not difficult to get the DP recursive formula: dp[k, i] = max(dp[k, i-1], prices[i] - prices[j] + dp[k-1, j-1]), j=[0..i-1] For k transactions, on i-th day,if we don't trade then the p...转载 2018-07-17 21:48:33 · 165 阅读 · 0 评论 -
leetcode105. Construct Binary Tree from Preorder and Inorder TraversalGiven preorder and inorder tra
问题描述:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, givenpreorder = [3,9,20,15,7]inorder = [9,3,15...转载 2018-07-05 13:51:06 · 135 阅读 · 0 评论