LeetCode
SYSUxx
时间流逝 你还有我
展开
-
算法分析与设计丨第二周丨LeetCode(4)——Maximum Subarray(Easy)
动态规划&分治法题目链接:点击打开链接这道题目要求子数组的最大和,用动态规划的方法会优美许多,而分治法的暂时没时间写,下次再补上。动态规划:易想到递推公式:局部最大和:max(curSum+i,i),如果当前子数列之和比遍历到的元素要小,那就取元素为局部最大和 全局最大和:max(maxSum,curSum),将当前全局最大和与局部最大和进行比较代码也可得出原创 2017-09-14 11:27:18 · 192 阅读 · 0 评论 -
算法分析与设计丨第十周丨LeetCode(14)——Edit distance(Hard)
动态规划题目链接:https://leetcode.com/problems/edit-distance/description/题目描述:Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is原创 2017-11-12 09:10:19 · 199 阅读 · 0 评论 -
算法分析与设计丨第十一周丨Sicily(15)—— 1003. 最近的0(Hard)
题目描述:输入一个N*M的01矩阵A,对矩阵的每个位置,求至少经过多少步可以到达一个0. 每一步可以往上下左右走一格. 请为下面的Solution类实现解决这一问题的函数nearestZero,函数参数A为给出的01矩阵,A的行数和列数均不大于100. 函数的返回值是问题的答案. class Solution {public: vector> nearestZ原创 2017-11-19 16:16:09 · 272 阅读 · 0 评论 -
算法分析与设计丨第十二周丨LeetCode(16)——Minimum Path Sum(Medium)
题目描述原创 2017-11-28 20:18:42 · 169 阅读 · 0 评论 -
算法分析与设计丨第十三周丨LeetCode(17)——Coin Change(Medium)
题目描述:You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amoun原创 2017-12-01 11:52:20 · 175 阅读 · 0 评论 -
算法分析与设计丨第十四周丨LeetCode(18)——Maximum Subarray(Medium)
题目描述:Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the l原创 2017-12-09 11:27:39 · 147 阅读 · 0 评论 -
算法分析与设计丨第十五周丨LeetCode(19)——Longest Palindromic Substring(Medium)
题目描述:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: "babad"Output: "bab"Note: "aba" is also a valid a原创 2017-12-13 21:09:09 · 196 阅读 · 0 评论 -
算法分析与设计丨第十六周丨LeetCode(20)——Unique Paths II(Medium)
题目描述:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectivel原创 2017-12-18 11:08:16 · 170 阅读 · 0 评论 -
算法分析与设计丨第十七周丨算法概论第八章习题——k-spanning tree(8.12)
8.12 The k-SPANNING TREE problem is the following. Input: An undirected graph G = (V,E) Output: Aspanningtreeof G in which each node has degree≤ k, if such a tree exists.原创 2017-12-28 15:03:36 · 471 阅读 · 0 评论 -
算法分析与设计丨第十八周丨LeetCode(21)——Binary Tree Maximum Path Sum(Hard)
题目描述:Given a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connection原创 2018-01-07 20:35:34 · 207 阅读 · 0 评论 -
算法分析与设计丨第九周丨LeetCode(13)——Redundant Connection(Medium)
并查集算法题目链接:https://leetcode.com/problems/redundant-connection/description/题目描述:In this problem, a tree is an undirected graph that is connected and has no cycles.The given input is a grap原创 2017-10-21 10:52:19 · 215 阅读 · 0 评论 -
算法分析与设计丨第八周丨LeetCode(12)——Friend Circles(Medium)
题目链接:https://leetcode.com/problems/friend-circles/description/题目描述:There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For exam原创 2017-10-20 19:54:11 · 224 阅读 · 0 评论 -
算法分析与设计丨第七周丨LeetCode(11)——Candy(Hard)
贪心算法题目链接:https://leetcode.com/problems/candy/description/这道题目主要是思路,有了思路便很好办了。之前我始终不太理解贪心算法究竟是什么意思,然后思考一番加做题后发现其实是利用局部的资源最优来判断,比如说这道题目的比左右两边ratings高的candy更多,Kruskal算法的始终取不构成回环的权值最小边,都是贪心算法的体现原创 2017-10-18 19:40:36 · 196 阅读 · 0 评论 -
算法分析与设计丨第二周丨LeetCode(3)——Kth Largest Element in an Array(Medium)
class Solution {public: int findKthLargest(vector& nums, int k) { int left = 0,right = nums.size() - 1; while(left < right)//其实类似于快排,但没有用到分治法的思想 { int myleft =原创 2017-09-13 15:56:43 · 216 阅读 · 0 评论 -
算法分析与设计丨第三周丨LeetCode(5)——Median of Two Sorted Arrays(Hard)
分治法题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/description/本来上个星期就想做这一题的,试一下hard的难度,想了半天,还是只想出一种朴素的方法:用两个标记分别指向两个vector的begin,这样一个个比较下去,直到数到中位数为止。也可以类似这道题目一样:http://blog.csdn.原创 2017-09-18 20:08:49 · 340 阅读 · 0 评论 -
算法分析与设计丨第一周丨 LeetCode(1)——Two Sum
太久没打代码了,c++都忘得差不多了。这道题就是作为入门题,对vector进行遍历即可class Solution {public: vector twoSum(vector& nums, int target) { vector my_vec; int num1,num2; int size = nu原创 2017-09-09 16:24:06 · 217 阅读 · 0 评论 -
算法分析与设计丨第一周丨LeetCode(2)——Different Ways to Add Parentheses(Medium)
点击打开链接这题目昨天做了今天才有时间写博客。因为这个星期老师讲了分治法所以我就找了这方面的题目,这道题是要处理所有加括号的情况,看了看众多前辈的提示,我用的与其类似的递归的方法,碰到每个符号时将其分为左右两边来处理,遍历一遍字符串,便可处理完毕。class Solution {public: vector diffWaysToCompute(string原创 2017-09-10 16:22:12 · 192 阅读 · 0 评论 -
算法分析与设计丨第三周丨LeetCode(6)——Find Largest Value in Each Tree Row(Medium)
层次遍历&深度遍历/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), ri原创 2017-09-20 17:46:25 · 228 阅读 · 0 评论 -
算法分析与设计丨第四周丨LeetCode(7)——Find Bottom Left Tree Value(Medium)
层次遍历题目链接:https://leetcode.com/problems/find-bottom-left-tree-value/description//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tree原创 2017-09-27 15:18:35 · 183 阅读 · 0 评论 -
算法分析与设计丨第四周丨LeetCode(8)——Binary Tree Level Order Traversal(Medium)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2017-09-29 17:22:02 · 244 阅读 · 0 评论 -
算法分析与设计丨第六周丨LeetCode(9)——Course Schedule(Medium)
拓扑排序题目链接:https://leetcode.com/problems/course-schedule/description/上周国庆休息,这周继续作业。这题用到了拓扑排序,都是以前数据结构说到的方法,看来得把书拿出来翻一翻了。#include #include#includeusing namespace std;class Solution {原创 2017-10-10 16:48:11 · 204 阅读 · 0 评论 -
算法分析与设计丨第六周丨LeetCode(10)——Best Time to Buy and Sell Stock(Easy)
class Solution {public: int maxProfit(vector& prices) { if(prices.empty()) return 0; int profit = 0; int budget = prices[0];原创 2017-10-13 19:26:58 · 295 阅读 · 0 评论 -
算法分析与设计丨第十九周丨LeetCode(22)——House Robber(Medium)
题目描述: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 adjac原创 2018-01-09 22:02:45 · 211 阅读 · 0 评论