ZMR小站

很高兴遇见你~


  • 首页

  • 标签

  • 分类

  • 归档

p22

发表于 2016-08-17 | 分类于 blog

LeetCode 22 Generate Parentheses 题解

1.题目:

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
“((()))”,
“(()())”,
“(())()”,
“()(())”,
“()()()”
]

阅读全文 »

p377

发表于 2016-08-16 | 分类于 blog

LeetCode 377 Combination Sum IV 题解

1.题目:

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
nums = [1, 2, 3]
target = 4
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
Therefore the output is 7.

阅读全文 »

p312

发表于 2016-08-16 | 分类于 blog

LeetCode 312 Burst Balloons 题解

1.题目:

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] nums[i] nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
(1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
(2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
Example:
Given [3, 1, 5, 8]
Return 167
nums = [3,1,5,8] –> [3,5,8] –> [3,8] –> [8] –> []
coins = 315 + 358 + 138 + 181 = 167

阅读全文 »

p230

发表于 2016-08-15 | 分类于 blog

LeetCode 230 Kth Smallest Element in a BST 题解

1.题目:

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

阅读全文 »

p318

发表于 2016-08-15 | 分类于 blog

LeetCode 318 Maximum Product of Word Lengths 题解

1.题目:

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
Example 1:
Given [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”]
Return 16
The two words can be “abcw”, “xtfn”.
Example 2:
Given [“a”, “ab”, “abc”, “d”, “cd”, “bcd”, “abcd”]
Return 4
The two words can be “ab”, “cd”.
Example 3:
Given [“a”, “aa”, “aaa”, “aaaa”]
Return 0

阅读全文 »

p378

发表于 2016-08-15 | 分类于 blog

LeetCode 378 Kth Smallest Element in a Sorted Matrix 题解

1.题目:

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
return 13.

阅读全文 »

p52

发表于 2016-08-15 | 分类于 blog

LeetCode 52 N-Queens II 题解

1.题目:

Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

阅读全文 »
1…232425…33
ZhangMengRou

ZhangMengRou

ZMR小站

230 日志
4 分类
14 标签
© 2019 ZhangMengRou
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.4