Skip to content

Commit 56b0e2b

Browse files
committed
change 5-10 to md
1 parent 2a3ca48 commit 56b0e2b

8 files changed

+635
-0
lines changed
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 11. Container With Most Water
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Two Pointers.
5+
- Similar Questions: Trapping Rain Water.
6+
7+
## Problem
8+
9+
Given *n* non-negative integers *a1*, *a2*, ..., *an*, where each represents a point at coordinate (*i*, *ai*). *n* vertical 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.
10+
11+
Note: You may not slant the container and *n* is at least 2.
12+
13+
14+
## Solution
15+
16+
```javascript
17+
/**
18+
* @param {number[]} height
19+
* @return {number}
20+
*/
21+
var maxArea = function(height) {
22+
var max = 0;
23+
var l = 0;
24+
var r = height.length - 1;
25+
while (l < r) {
26+
max = Math.max(max, Math.min(height[l], height[r]) * (r - l));
27+
if (height[l] < height[r]) {
28+
l++;
29+
} else {
30+
r--;
31+
}
32+
}
33+
return max;
34+
};
35+
```
36+
37+
**Explain:**
38+
39+
双指针从两头开始,判断哪边更高。积水量是矮的那边决定的,移动高的那边不可能提升积水量,所以移动矮的那边。
40+
41+
**Complexity:**
42+
43+
* Time complexity : O(n).
44+
* Space complexity : O(1).

001-100/12. Integer to Roman.md

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# 12. Integer to Roman
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Math, String.
5+
- Similar Questions: Roman to Integer, Integer to English Words.
6+
7+
## Problem
8+
9+
Roman numerals are represented by seven different symbols: ```I```, ```V```, ```X```, ```L```, ```C```, ```D``` and ```M```.
10+
11+
```
12+
Symbol Value
13+
I 1
14+
V 5
15+
X 10
16+
L 50
17+
C 100
18+
D 500
19+
M 1000
20+
```
21+
22+
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```.
23+
24+
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:
25+
26+
- ```I``` can be placed before ```V``` (5) and ```X``` (10) to make 4 and 9. 
27+
- ```X``` can be placed before ```L``` (50) and ```C``` (100) to make 40 and 90. 
28+
- ```C``` can be placed before ```D``` (500) and ```M``` (1000) to make 400 and 900.
29+
30+
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
31+
32+
**Example 1:**
33+
34+
```
35+
Input: 3
36+
Output: "III"
37+
```
38+
39+
**Example 2:**
40+
41+
```
42+
Input: 4
43+
Output: "IV"
44+
```
45+
46+
**Example 3:**
47+
48+
```
49+
Input: 9
50+
Output: "IX"
51+
```
52+
53+
**Example 4:**
54+
55+
```
56+
Input: 58
57+
Output: "LVIII"
58+
Explanation: C = 100, L = 50, XXX = 30 and III = 3.
59+
```
60+
61+
**Example 5:**
62+
63+
```
64+
Input: 1994
65+
Output: "MCMXCIV"
66+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
67+
```
68+
69+
70+
## Solution 1
71+
72+
```javascript
73+
/**
74+
* @param {number} num
75+
* @return {string}
76+
*/
77+
var intToRoman = function(num) {
78+
var str = [
79+
['I', 'V'],
80+
['X', 'L'],
81+
['C', 'D'],
82+
['M']
83+
];
84+
var res = '';
85+
var i = 0;
86+
var tmp = 0;
87+
while (num > 0) {
88+
tmp = num % 10;
89+
if (tmp === 9) {
90+
res = str[i][0] + str[i + 1][0] + res;
91+
} else if (tmp >= 5) {
92+
res = str[i][1] + str[i][0].repeat(tmp - 5) + res;
93+
} else if (tmp === 4) {
94+
res = str[i][0] + str[i][1] + res;
95+
} else {
96+
res = str[i][0].repeat(tmp) + res;
97+
}
98+
num = Math.floor(num / 10);
99+
i++;
100+
}
101+
return res;
102+
};
103+
```
104+
105+
**Explain:**
106+
107+
nope.
108+
109+
**Complexity:**
110+
111+
* Time complexity : O(log(n)).
112+
* Space complexity : O(1).
113+
114+
## Solution 2
115+
116+
```javascript
117+
/**
118+
* @param {number} num
119+
* @return {string}
120+
*/
121+
var intToRoman = function(num) {
122+
var map = [
123+
[1, "I"],
124+
[4, "IV"],
125+
[5, "V"],
126+
[9, "IX"],
127+
[10, "X"],
128+
[40, "XL"],
129+
[50, "L"],
130+
[90, "XC"],
131+
[100, "C"],
132+
[400, "CD"],
133+
[500, "D"],
134+
[900, "CM"],
135+
[1000, "M"]
136+
];
137+
var res = '';
138+
var i = 12;
139+
var tmp = 0;
140+
while (num > 0) {
141+
res += map[i][1].repeat(Math.floor(num / map[i][0]));
142+
num %= map[i][0];
143+
i--;
144+
}
145+
return res;
146+
};
147+
```
148+
149+
**Explain:**
150+
151+
nope.
152+
153+
**Complexity:**
154+
155+
* Time complexity : O(log(n)).
156+
* Space complexity : O(1).
157+
158+
## Solution 3
159+
160+
```javascript
161+
/**
162+
* @param {number} num
163+
* @return {string}
164+
*/
165+
var intToRoman = function(num) {
166+
var M =["", "M", "MM", "MMM"];
167+
var C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
168+
var X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
169+
var I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
170+
return M[Math.floor(num / 1000)]
171+
+ C[Math.floor((num % 1000) / 100)]
172+
+ X[Math.floor((num % 100) / 10)]
173+
+ I[num % 10];
174+
};
175+
```
176+
177+
**Explain:**
178+
179+
nope.
180+
181+
**Complexity:**
182+
183+
* Time complexity : O(1).
184+
* Space complexity : O(1).

001-100/13. Roman to Integer.md

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# 13. Roman to Integer
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Math, String.
5+
- Similar Questions: Integer to Roman.
6+
7+
## Problem
8+
9+
Roman numerals are represented by seven different symbols: ```I```, ```V```, ```X```, ```L```, ```C```, ```D``` and ```M```.
10+
11+
```
12+
Symbol Value
13+
I 1
14+
V 5
15+
X 10
16+
L 50
17+
C 100
18+
D 500
19+
M 1000
20+
```
21+
22+
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```.
23+
24+
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:
25+
26+
- ```I``` can be placed before ```V``` (5) and ```X``` (10) to make 4 and 9. 
27+
- ```X``` can be placed before ```L``` (50) and ```C``` (100) to make 40 and 90. 
28+
- ```C``` can be placed before ```D``` (500) and ```M``` (1000) to make 400 and 900.
29+
30+
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
31+
32+
**Example 1:**
33+
34+
```
35+
Input: "III"
36+
Output: 3
37+
```
38+
39+
**Example 2:**
40+
41+
```
42+
Input: "IV"
43+
Output: 4
44+
```
45+
46+
**Example 3:**
47+
48+
```
49+
Input: "IX"
50+
Output: 9
51+
```
52+
53+
**Example 4:**
54+
55+
```
56+
Input: "LVIII"
57+
Output: 58
58+
Explanation: C = 100, L = 50, XXX = 30 and III = 3.
59+
```
60+
61+
**Example 5:**
62+
63+
```
64+
Input: "MCMXCIV"
65+
Output: 1994
66+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
67+
```
68+
69+
## Solution
70+
71+
```javascript
72+
/**
73+
* @param {string} s
74+
* @return {number}
75+
*/
76+
var romanToInt = function(s) {
77+
var map = {
78+
I: 1,
79+
IV: 4,
80+
V: 5,
81+
IX: 9,
82+
X: 10,
83+
XL: 40,
84+
L: 50,
85+
XC: 90,
86+
C: 100,
87+
CD: 400,
88+
D: 500,
89+
CM: 900,
90+
M: 1000
91+
};
92+
var len = s.length;
93+
var i = 0;
94+
var res = 0;
95+
while (i < len) {
96+
if (map[s.substr(i, 2)]) {
97+
res += map[s.substr(i, 2)];
98+
i += 2;
99+
} else {
100+
res += map[s[i]];
101+
i += 1;
102+
}
103+
}
104+
return res;
105+
};
106+
```
107+
108+
**Explain:**
109+
110+
nope.
111+
112+
**Complexity:**
113+
114+
* Time complexity : O(n).
115+
* Space complexity : O(1).

0 commit comments

Comments
 (0)