Leetcode 9-12

No. 9 Palindrome Number (简单,PASS)

Solution:

Firstly, if the interger is negative, then we can confidently return false.

Then, we can convert the integer to the string and check whether it is palindrome.


No. 10 Regular Expression Matching (HARD)

https://leetcode.com/problems/regular-expression-matching/discuss/5723/My-DP-approach-in-Python-with-comments-and-unittest


No. 11 Container With Most Water

注意点:本题目并不是求已给出的height中符合要求的sum最大,而是给出最大能盛水的量。

Solution: Two Pointers

Note: The most water = width * min(left, right), where width = j - i, i and j are the indexes of two sides.

We can use two pointers starting from the begging and the end of the array to solve this problem. The core of the problem is how to update the position of two pointers. When to update? Who to update? +1 or -1?

Let's denote i, and j are the index of the array, and initialize i = 0 and j = len(height)-1.

If height[i] < height[j], then we can know that the volume of  the water depends on height[i]. What if we move i to the right? Maybe in the next round, the water depends on height[j].

code:

 1 class Solution:
 2     def maxArea(self, height: List[int]) -> int:
 3         if not height:
 4             return 0
 5         
 6         res = 0
 7         i, j, w = 0, len(height)-1, len(height)-1
 8         while i < j:
 9             res = max(res, min(height[i], height[j]) * w)
10             if height[i] < height[j]:
11                 i += 1
12             else:
13                 j -= 1
14             w = j - i
15         return res

 


No. 12 Integer to Roman

将整数转换成罗马数字(Roman),字符表对应如下:

其中,有六种特殊情况,如下:

Solution:不断整除,分母分别为[1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]。

posted @ 2019-06-03 11:58  ArthurH  阅读(167)  评论(0编辑  收藏  举报