![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
algorithms
jzylf
这个作者很懒,什么都没留下…
展开
-
104. Maximum Depth of Binary Tree
problem: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.solution:#include原创 2017-09-24 22:57:26 · 135 阅读 · 0 评论 -
Prove that STINGY SAT is NP-complete
Problem:STINGY SAT is the following problem:given a set of clauses(each a disjunction of literals) and an interger k, find a satisfying assignment in which at most k variables are true, if such an a原创 2017-12-31 11:50:47 · 434 阅读 · 0 评论 -
130. Surrounded Regions
problem:Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.Fo转载 2017-12-17 22:51:17 · 108 阅读 · 0 评论 -
473. Matchsticks to Square
problem:Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsti转载 2018-01-07 20:55:23 · 107 阅读 · 0 评论 -
1000. 分组
Problem: 对于一个整数数列A[0], A[1], ..., A[N-1]进行分组,要求每组1到2个数,并且同组之和不能大于w. 求最少可以分成多少组. 1 请实现下面Solution类中计算minPartition(A, w)的函数. class Solution {public: int minPartition(vector A,原创 2018-01-08 11:24:44 · 174 阅读 · 0 评论 -
1001. 最小差
Problem: 对于一个整数数列A[0], A[1], ..., A[N-1],要求在其中找两个数,使得它们的差的绝对值最小. 2 请实现下面Solution类中计算minDifference(A)的函数,返回值为能得到的最小差. class Solution {public: int minDifference(vector A) {原创 2018-01-08 11:26:10 · 226 阅读 · 0 评论 -
1002. 合并二叉树
Problem:输入两个二叉树T1和T2,要求对T1和T2进行合并. 合并是指将二叉树同一位置节点上的数求和,作为合并后二叉树相应位置节点的数值. 如果某个位置上只有一个二叉树有节点,则合并后的二叉树对应位置上的节点上的数值就等于这个节点上的数值.例如: T1 T2 1原创 2018-01-08 11:28:46 · 261 阅读 · 0 评论 -
1003. 最近的0
Problem:输入一个N*M的01矩阵A,对矩阵的每个位置,求至少经过多少步可以到达一个0. 每一步可以往上下左右走一格. 请为下面的Solution类实现解决这一问题的函数nearestZero,函数参数A为给出的01矩阵,A的行数和列数均不大于100. 函数的返回值是问题的答案. class Solution {public: vector> neare原创 2018-01-08 11:30:16 · 133 阅读 · 0 评论 -
1006. 最长公共子串
Problem:给定两个字符串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类实现解决上述问题的函数longestSubstring原创 2018-01-08 11:31:39 · 149 阅读 · 0 评论 -
1005. 最小和
Problem:从数列A[0], A[1], A[2], ..., A[N-1]中选若干个数,要求对于每个i(0 1 请为下面的Solution类实现解决上述问题的函数minSum,函数参数A是给出的数列,返回值为所求的最小和. class Solution {public: int minSum(vector& A) {原创 2018-01-08 14:47:14 · 172 阅读 · 0 评论 -
1004. 拓扑序[Special judge]
Problem:在图论中,拓扑序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列. 且该序列必须满足下面两个条件: 1. 每个顶点出现且只出现一次.2. 若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 对于一个转载 2018-01-08 15:27:54 · 207 阅读 · 0 评论 -
141. Linked List Cycle
problems:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?solution:using namespace std; //Definition for singly-l转载 2017-12-23 15:29:49 · 103 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
problem: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原创 2017-12-23 10:50:24 · 173 阅读 · 0 评论 -
717. 1-bit and 2-bit Characters
problem:We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).Now given a string represented b转载 2017-11-23 16:10:17 · 115 阅读 · 0 评论 -
451. Sort Characters By Frequency
Problem:Given a string, sort it in decreasing order based on the frequency of characters.solution:#include#includeusing namespace std;class Solution {public:string frequenc原创 2017-09-17 23:46:44 · 128 阅读 · 0 评论 -
53.Maximum Subarray
Problem: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 subarray [原创 2017-09-10 20:34:23 · 125 阅读 · 0 评论 -
233. Number of Digit One
problem:Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in转载 2017-10-01 12:46:32 · 157 阅读 · 0 评论 -
50. Pow(x, n)
problem:Implement pow(x, n).solution:double myPow(double x, int n) {if (n == 1) return x;if (n == 0) return 1;if (n {x = 1 / x;n = -n;}if (n % 2){return myPow(x, n / 2) * myP原创 2017-10-01 14:55:18 · 181 阅读 · 0 评论 -
219. Contains Duplicate II
Problems:Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i a原创 2017-10-29 13:16:13 · 161 阅读 · 0 评论 -
51. N-Queens
Problem:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to t转载 2017-10-22 22:28:52 · 100 阅读 · 0 评论 -
6. ZigZag Conversion
Problem:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H转载 2017-11-13 17:22:34 · 126 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
problem:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].Note: Recursiv转载 2017-11-13 19:48:52 · 130 阅读 · 0 评论 -
687. Longest Univalue Path
Problem:Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.Note: The length of path betwe转载 2017-11-13 20:10:43 · 224 阅读 · 0 评论 -
331. Verify Preorder Serialization of a Binary Tree
Problem:One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value su转载 2017-11-13 21:20:53 · 104 阅读 · 0 评论 -
77. Combinations
Problem:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [转载 2017-12-10 22:45:11 · 129 阅读 · 0 评论 -
1007. 怪兽训练
Problem:贝爷的人生乐趣之一就是约战马会长. 他知道马会长喜欢和怪兽对决,于是他训练了N只怪兽,并对怪兽用0到N-1的整数进行编号. 贝爷训练怪兽的方式是让它们一对一互殴. 两只怪兽互殴会发生以下三种可能的结果:1) 什么事也没发生2) 第一只怪兽永远消失3) 第二只怪兽永远消失怪兽们经过了旷日持久的互殴. 贝爷不知道哪些怪兽进行了互殴也不知道它们互殴的顺转载 2018-01-08 15:32:10 · 353 阅读 · 0 评论