![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
即将变成胖子的nzc
这个作者很懒,什么都没留下…
展开
-
《算法概论》第八章课后题
8.12 k-生成树问题是这样的: 输入:无向图G=(V,E) 输出:G的一个生成树,其中所有节点度数不超过k——如果该树存在。 请证明对任意k>=2: (a)k-生成树问题是一个搜索问题。 (b)k-生成树问题是NP-完全的。答案: (a)显然k -SPANNING TREE 问题是可在多项式时间内验证的,因此是搜索问题。 (b)若k = 2 ,此时的 2 -SPANNING T原创 2017-07-05 16:24:36 · 332 阅读 · 0 评论 -
133. Clone Graph
问题描述 Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.解决思路 深度搜索,广度搜索进行复制都可以。。。关键是用一个map记录哪些节点遍历过,已经有对应关系了。代码/** * Definition for undirected graph.原创 2017-03-12 01:14:24 · 177 阅读 · 0 评论 -
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 partitioning of s.For example, given s = “aab”, Return 1原创 2017-03-12 01:11:03 · 207 阅读 · 0 评论 -
399. Evaluate Division
1.问题描述 Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the原创 2017-03-19 21:18:50 · 191 阅读 · 0 评论 -
95. Unique Binary Search Trees II
问题描述 Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below.1原创 2017-02-26 23:54:35 · 177 阅读 · 0 评论 -
文章标题
问题描述 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: Recursive solution is trivial, could you do it i原创 2017-02-26 23:48:08 · 222 阅读 · 0 评论 -
93. Restore IP Addresses
问题描述 Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example: Given “25525511135”,return [“255.255.11.135”, “255.255.111.35”]. (Order doe原创 2017-02-26 23:28:21 · 196 阅读 · 0 评论 -
92. Reverse Linked List II
问题描述 Reverse a linked list from position m to n. Do it in-place and in one-pass.For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note: Given m, n satisfy the follo原创 2017-02-26 23:08:45 · 266 阅读 · 0 评论 -
131. Palindrome Partitioning
问题描述 Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = “aab”, Return[["aa","b"],["a","原创 2017-03-09 00:40:56 · 223 阅读 · 0 评论 -
130. Surrounded Regions
问题描述 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.For example,X X X XX O原创 2017-03-09 00:26:52 · 163 阅读 · 0 评论 -
134. Gas Station
问题描述 There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to i原创 2017-03-12 01:21:14 · 171 阅读 · 0 评论 -
135. Candy
问题描述 There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one c原创 2017-03-12 01:31:16 · 182 阅读 · 0 评论 -
136. Single Number
问题描述 Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using e原创 2017-03-12 01:35:04 · 214 阅读 · 0 评论 -
198. House Robber && 213. House Robber II
问题描述 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原创 2017-05-06 23:51:50 · 339 阅读 · 0 评论 -
300. Longest Increasing Subsequence
问题描述 Given an unsorted array of integers, find the length of longest increasing subsequence.For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], th原创 2017-04-16 16:17:40 · 218 阅读 · 0 评论 -
142. Linked List Cycle II
问题描述 Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up: Can you solve it without using extra space?解决思路原创 2017-03-26 20:22:09 · 211 阅读 · 0 评论 -
316. Remove Duplicate Letters
问题描述 Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical o原创 2017-04-03 00:39:35 · 199 阅读 · 0 评论 -
141. Linked List Cycle
问题描述 Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?解决思路 开两个指针,一个每次走一步,一个每次走两步,看它们是否会重合。代码/** * Definition for singly-linked list.原创 2017-03-26 20:09:35 · 167 阅读 · 0 评论 -
139. Word Break
问题描述 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may原创 2017-03-26 20:07:03 · 184 阅读 · 0 评论 -
137. Single Number II
问题描述 Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could原创 2017-03-26 20:01:13 · 283 阅读 · 0 评论 -
文章标题
问题描述 Given a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3},1\ 2/3return [3,2,1].解决思路 其实后序遍历可以转换成前序遍历:先遍历根节点,然后遍历右子树,然后遍历左子树,然后得到的结果反原创 2017-04-02 16:41:59 · 183 阅读 · 0 评论 -
150. Evaluate Reverse Polish Notation
问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples:["2", "1", "+", "3", "原创 2017-04-09 22:33:48 · 166 阅读 · 0 评论 -
124. Binary Tree Maximum Path Sum
问题描述 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 connections. The原创 2017-03-05 16:12:03 · 186 阅读 · 0 评论 -
123. Best Time to Buy and Sell Stock III
问题描述 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 at most two transactions.Note: You may not原创 2017-03-05 16:07:07 · 150 阅读 · 0 评论 -
80. Remove Duplicates from Sorted Array II
问题描述 Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five原创 2017-02-24 00:25:01 · 180 阅读 · 0 评论 -
73.Set Matrix Zeroes
问题描述 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.解决思路 (1) 很蠢很蠢的思路,另外开一个m*n的二维数组来存储变0的位置。 (2) 一样很蠢的思路,开两个一维数组,一个存储变0的行,一个存储变0的列 (3) 利用第一行,第一列存储变原创 2017-02-23 01:24:12 · 189 阅读 · 0 评论 -
74. Search a 2D Matrix
问题描述 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 from left to right. The first integer of each原创 2017-02-23 01:35:31 · 177 阅读 · 0 评论 -
72. Edit Distance
问题描述 Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a wor原创 2017-02-23 01:12:09 · 173 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal
问题描述 Given preorder and inorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.解决思路 利用递归建树的思想,找到左右子树的list,然后递归下去,就可以了。代码/** * Defin原创 2017-02-28 22:47:37 · 160 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
问题描述 Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).For example: Given binary原创 2017-02-28 22:45:12 · 165 阅读 · 0 评论 -
文章标题
问题描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).For example: Given binary tree [3,9,20,null,null,15,7], 3/ \9 20 / \原创 2017-02-28 22:37:17 · 147 阅读 · 0 评论 -
99. Recover Binary Search Tree
问题描述 wo elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note: A solution using O(n) space is pretty straight forward. Could you devise a原创 2017-02-28 22:32:22 · 134 阅读 · 0 评论 -
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原创 2017-02-28 22:27:07 · 202 阅读 · 0 评论 -
76. Minimum Window Substring
问题描述 Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example, S = “ADOBECODEBANC” T = “ABC” Minimum window is “BANC”.原创 2017-02-24 00:46:47 · 182 阅读 · 0 评论 -
文章标题
问题描述 Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or verticall原创 2017-02-24 00:53:57 · 165 阅读 · 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 stoc原创 2017-03-05 16:03:24 · 256 阅读 · 0 评论 -
89. Gray Code
问题描述 The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence o原创 2017-02-25 23:32:26 · 171 阅读 · 0 评论 -
88. Merge Sorted Array
问题描述 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold add原创 2017-02-25 23:12:24 · 171 阅读 · 0 评论 -
87. Scramble String
问题描述 Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = “great”:great/ \gr eat/ \原创 2017-02-25 22:59:11 · 131 阅读 · 0 评论 -
1. Search in Rotated Sorted Array II
问题描述 Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why? Suppose an array sorted in ascending order is rotated at s原创 2017-02-25 01:57:33 · 209 阅读 · 0 评论