problem 42,43,44,45
42. Trapping Rain Water
Hard
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
Example:
1 | |
这个题主要考虑几种情况,
第一种是最简单的,能找到边界,如 [1,0,2] [ 2,1,0,1,3],这种情况要求起始指针能找到一个大于等于他的指针,这是优先考虑的情况。
如果找不到上面的情况依旧能够装水,只需要有一个gap就能装水,如[3,2,1,2],当然这个用上面的思路也能解决,但是也有解决不了的,如[3,1,2],这种情况需要记录两个指针,
1 | |
43. Multiply Strings
Medium
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
Example 1:
1 | |
Example 2:
1 | |
Note:
- The length of both
num1andnum2is < 110. - Both
num1andnum2contain only digits0-9. - Both
num1andnum2do not contain any leading zero, except the number 0 itself. - You must not use any built-in BigInteger library or convert the inputs to integerdirectly.
1 | |
44. Wildcard Matching
Hard
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.
1 | |
The matching should cover the entire input string (not partial).
Note:
scould be empty and contains only lowercase lettersa-z.pcould be empty and contains only lowercase lettersa-z, and characters like?or*.
Example 1:
1 | |
Example 2:
1 | |
Example 3:
1 | |
Example 4:
1 | |
Example 5:
1 | |
动态规划
1 | |
45. Jump Game II
Hard
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
1 | |
解法一:递归,每次跳到下一次能调到最远的地方
1 | |
迭代:相比于上面的方法能够减少迭代次数,很多重复去的地方不用重复去
1 | |