problem 11,12,13,14
11. Container With Most Water
Medium
Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). nvertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
1 | |
这个题是个数学问题,最简单的思路就是暴力破解法,运算复杂度为$O(n ^2)$
更简单的可以用两个指针来解决
1 | |
12. Integer to Roman
Medium
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
1 |
|
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
1 | |
Example 2:
1 | |
Example 3:
1 | |
Example 4:
1 | |
Example 5:
1 | |
解决这个问题要做分解,有几个比较重要的界限
1000,900,500,400,100,90,50,40,10,9,5,4
‘M’,’CM’,’D’,’CD’,’C’,’XC’,’L’,’XL’,’X’,’IX’,’V’,’IV’
1 | |
13. Roman to Integer
Easy
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
1 |
|
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
1 | |
Example 2:
1 | |
Example 3:
1 | |
Example 4:
1 | |
Example 5:
1 | |
这一题是上一个的反过程,个人觉得比上一个难,不过也是一个查字典的过程
主要的难点在于一个小字符出现在大字符之前
1 | |
14. Longest Common Prefix
Easy
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
1 | |
Example 2:
1 | |
Note:
All given inputs are in lowercase letters a-z.
下面这个做法是很蠢的做法,可读性很差,设置了两个flag,一个负责判断是否全匹配,另一个负责判断是否匹配到最后一位
1 | |
下面这个想法是利用排序解决问题,如果有共有pre,那么对pre进行排序结果不变,所以没有公共pre的必然是最小值或者最大值中的一个
1 | |
这样还不用疲于判断是否有空字符存在在list中