
算法
子长
这个作者很懒,什么都没留下…
展开
-
215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return原创 2017-09-24 12:41:52 · 219 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi转载 2017-11-02 00:34:05 · 173 阅读 · 0 评论 -
算法期中1005. 最小和
Description 从数列A[0], A[1], A[2], …, A[N-1]中选若干个数,要求对于每个,i(i >= 0, i <=N - 1)A[i]和A[i+1]至少选一个数,求能选出的最小和.1 <= N <= 100000, 1 <= A[i] <= 1000请为下面的Solution类实现解决上述问题的函数minSum,函数参数A是给出的数列,返回值为所求的最小和.class S原创 2017-11-13 19:03:10 · 259 阅读 · 0 评论 -
53. Maximum Subarray (9月12日)
53. Maximum SubarrayFind the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarra原创 2017-09-11 21:10:14 · 207 阅读 · 0 评论 -
688. Knight Probability in Chessboard
https://leetcode.com/problems/knight-probability-in-chessboard/description/ On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. The rows and col原创 2017-11-04 22:53:42 · 316 阅读 · 0 评论 -
1006. 最长公共子串
未结束,还没理解清楚,溜去睡觉了。Description 给定两个字符串x = x1x2…xn和y = y1y2…ym, 请找出x和y的最长公共子串的长度,也就是求出一个最大的k,使得存在下标i和j有xixi+1…xi+k-1 = yjyj+1…yj+k-1.x和y只含有小写字母,长度均在1和1000之间.请为下面的Solution类实现解决上述问题的函数longestSubs原创 2017-11-13 20:55:47 · 306 阅读 · 0 评论 -
算法期中1002. 合并二叉树
Description 输入两个二叉树T1和T2,要求对T1和T2进行合并. 合并是指将二叉树同一位置节点上的数求和,作为合并后二叉树相应位置节点的数值. 如果某个位置上只有一个二叉树有节点,则合并后的二叉树对应位置上的节点上的数值就等于这个节点上的数值.例如: T1 T2 1原创 2017-11-13 19:24:47 · 294 阅读 · 0 评论 -
算法期中1000. 分组
Description 对于一个整数数列A[0], A[1], …, A[N-1]进行分组,要求每组1到2个数,并且同组之和不能大于w. 求最少可以分成多少组.1 <= N <= 100000, 1 <= A[i] <= w <= 1000000000.请实现下面Solution类中计算minPartition(A, w)的函数.class Solution { public: i原创 2017-11-12 23:13:38 · 495 阅读 · 0 评论 -
算法期中1003. 最近的0
Description 输入一个N*M的01矩阵A,对矩阵的每个位置,求至少经过多少步可以到达一个0. 每一步可以往上下左右走一格.请为下面的Solution类实现解决这一问题的函数nearestZero,函数参数A为给出的01矩阵,A的行数和列数均不大于100. 函数的返回值是问题的答案.class Solution { public: vector<vector<int>>neares原创 2017-11-13 20:04:08 · 251 阅读 · 0 评论 -
647. Palindromic Substrings
题目:Given a string, your task is to count how many palindromic substrings in this string.The substrings with different start indexes or end indexes are counted as different substrings even they consist原创 2017-11-27 17:28:46 · 179 阅读 · 0 评论 -
《算法概论》chapter8 NP完全问题
部分题目解答:8.8题目:The EXACT 4SAT problem, the input is a set of clauses, which is a disjunction of exactly four literals, and such that each variable occurs at most once in each clause. The goal is to find原创 2017-12-31 22:10:15 · 393 阅读 · 0 评论 -
算法期中1004. 拓扑序[Special judge]
Description 在图论中,拓扑序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列. 且该序列必须满足下面两个条件: 每个顶点出现且只出现一次. 若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 对于一个含有n个节点的有向无环图(节点编号0到n-1),输出原创 2017-11-17 21:28:55 · 416 阅读 · 0 评论 -
343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, return 1转载 2017-11-10 23:41:30 · 162 阅读 · 0 评论 -
240. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in原创 2017-09-27 00:51:22 · 214 阅读 · 0 评论 -
Maximum Subarray (9月11日)
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous su转载 2017-09-11 21:05:42 · 203 阅读 · 0 评论 -
10. Regular Expression Matching (9月11日)
10. Regular Expression Matching ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The func转载 2017-09-11 20:49:36 · 230 阅读 · 0 评论 -
169. Majority Element (9月8日)
169. Majority Element169. Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array原创 2017-09-08 21:45:10 · 196 阅读 · 0 评论 -
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. 回顾一下深度优先和广度优先吧 使用递归的深度优先搜索(一行搞定) str原创 2017-10-06 16:24:53 · 213 阅读 · 0 评论 -
513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.Example 1: Input: 2 / \ 1 3 Output: 1 Example 2: Input: 1 / \ 2 3 / / \ 4 5 6 / 7 Output: 7 Not原创 2017-10-06 17:03:54 · 226 阅读 · 0 评论 -
241. Different Ways to Add Parentheses
241. Different Ways to Add ParenthesesGiven a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid opera原创 2017-09-12 15:43:41 · 179 阅读 · 0 评论 -
455.assign cookies
ssume 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 cooki原创 2017-10-29 23:12:50 · 198 阅读 · 0 评论 -
62. Unique Paths
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 botto原创 2017-11-08 01:19:01 · 186 阅读 · 0 评论 -
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 houses原创 2017-10-31 23:50:47 · 169 阅读 · 0 评论 -
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 (ie, buy one and原创 2017-10-15 22:10:11 · 178 阅读 · 0 评论 -
Add to List 392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, an原创 2017-10-15 22:49:28 · 192 阅读 · 0 评论 -
算法期中1007. 怪兽训练 (找出有向图中所有的强连通分量的Kosaraju算法)
Description 贝爷的人生乐趣之一就是约战马会长. 他知道马会长喜欢和怪兽对决,于是他训练了N只怪兽,并对怪兽用0到N-1的整数进行编号. 贝爷训练怪兽的方式是让它们一对一互殴. 两只怪兽互殴会发生以下三种可能的结果: 1) 什么事也没发生 2) 第一只怪兽永远消失 3) 第二只怪兽永远消失 怪兽们经过了旷日持久的互殴. 贝爷不知道哪些怪兽进行了互殴也不知道它们互殴的顺序,但他确原创 2017-11-27 21:36:28 · 1063 阅读 · 4 评论