Skip to content

Commit e555522

Browse files
committed
add 12, 13
1 parent 225539a commit e555522

7 files changed

+393
-77
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5151
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5252
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5353
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
54-
| [🔲] | [9c-top.md](./list/9c-top.md) | 16/52 | 1 vip |
54+
| [🔲] | [9c-top.md](./list/9c-top.md) | 18/52 | 1 vip |
5555

5656
**Solved**: 481 problems
5757

@@ -96,7 +96,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
9696

9797
## Math
9898

99-
| Link | Problem(24) | Solution | Tag | Time | Space | Ref |
99+
| Link | Problem(26) | Solution | Tag | Time | Space | Ref |
100100
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
101101
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Math | O\(N\) | O\(1\) | - |
102102
| [Leetcode-504](https://leetcode.com/problems/base-7/) | Base 7 | [c++](./leetcode/504.base-7.cpp), [python3](./leetcode/504.base-7.py) | Math | O\(N\) | O\(1\) | - |
@@ -110,12 +110,14 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
110110
| [Leetcode-168](https://leetcode.com/problems/excel-sheet-column-title/) | Excel Sheet Column Title | [c++](./leetcode/168.excel-sheet-column-title.cpp), [python3](./leetcode/168.excel-sheet-column-title.py) | Math | O\(logN\) | O\(1\) | - |
111111
| [Leetcode-172](https://leetcode.com/problems/factorial-trailing-zeroes/) | Factorial Trailing Zeroes | [c++](./leetcode/172.factorial-trailing-zeroes.cpp), [python3](./leetcode/172.factorial-trailing-zeroes.py) | Math | O\(logN\) | O\(1\) | - |
112112
| [Leetcode-202](https://leetcode.com/problems/happy-number/) | Happy Number | [c++](./leetcode/202.happy-number.cpp), [python3](./leetcode/202.happy-number.py) | Math | O\(S\) | O\(1\) | - |
113+
| [Leetcode-12](https://leetcode.com/problems/integer-to-roman/) | Integer To Roman | [c++](./leetcode/12.integer-to-roman.cpp), [python3](./leetcode/12.integer-to-roman.py) | Math | O\(N\) | O\(1\) | - |
113114
| [Leetcode-973](https://leetcode.com/problems/k-closest-points-to-origin/) | K Closest Points To Origin | [c++](./leetcode/973.k-closest-points-to-origin.cpp), [python3](./leetcode/973.k-closest-points-to-origin.py) | Math | O\(NlogN\) | O\(K\) | - |
114115
| [Leetcode-149](https://leetcode.com/problems/max-points-on-a-line/) | Max Points On A Line | [c++](./leetcode/149.max-points-on-a-line.cpp), [python3](./leetcode/149.max-points-on-a-line.py) | Math | O\(N^2\) | O\(N\) | - |
115116
| [Leetcode-462](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | Minimum Moves To Equal Array Elements II | [c++](./leetcode/462.minimum-moves-to-equal-array-elements-ii.cpp), [python3](./leetcode/462.minimum-moves-to-equal-array-elements-ii.py) | Math | O\(NlogN\) | O\(1\) | - |
116117
| [Leetcode-326](https://leetcode.com/problems/power-of-three/) | Power Of Three | [c++](./leetcode/326.power-of-three.cpp), [python3](./leetcode/326.power-of-three.py) | Math | O\(1\) | O\(1\) | - |
117118
| [Leetcode-50](https://leetcode.com/problems/powx-n/) | Powx N | [c++](./leetcode/50.powx-n.cpp), [python3](./leetcode/50.powx-n.py) | Math | O\(logN\) | O\(1\) | - |
118119
| [Leetcode-836](https://leetcode.com/problems/rectangle-overlap/) | Rectangle Overlap | [c++](./leetcode/836.rectangle-overlap.cpp), [python3](./leetcode/836.rectangle-overlap.py) | Math | O\(1\) | O\(1\) | - |
120+
| [Leetcode-13](https://leetcode.com/problems/roman-to-integer/) | Roman To Integer | [c++](./leetcode/13.roman-to-integer.cpp), [python3](./leetcode/13.roman-to-integer.py) | Math | O\(N\) | O\(1\) | - |
119121
| [Leetcode-189](https://leetcode.com/problems/rotate-array/) | Rotate Array | [c++](./leetcode/189.rotate-array.cpp), [python3](./leetcode/189.rotate-array.py) | Math | O\(N\) | O\(1\) | - |
120122
| [Leetcode-48](https://leetcode.com/problems/rotate-image/) | Rotate Image | [c++](./leetcode/48.rotate-image.cpp), [python3](./leetcode/48.rotate-image.py) | Math | O\(N^2\) | O\(1\) | - |
121123
| [Leetcode-69](https://leetcode.com/problems/sqrtx/) | Sqrtx | [c++](./leetcode/69.sqrtx.cpp), [python3](./leetcode/69.sqrtx.py) | Math | N\(logN\) | O\(1\) | - |
@@ -405,7 +407,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
405407

406408
## Hash Table
407409

408-
| Link | Problem(52) | Solution | Tag | Time | Space | Ref |
410+
| Link | Problem(54) | Solution | Tag | Time | Space | Ref |
409411
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
410412
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Hash Table | O\(MlogM\) | O\(N\) | - |
411413
| [Leetcode-432](https://leetcode.com/problems/all-oone-data-structure/) | All Oone Data Structure | [c++](./leetcode/432.all-oone-data-structure.cpp), [python3](./leetcode/432.all-oone-data-structure.py) | Hash Table | O\(1\) | O\(N\) | - |
@@ -422,6 +424,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
422424
| [Leetcode-202](https://leetcode.com/problems/happy-number/) | Happy Number | [c++](./leetcode/202.happy-number.cpp), [python3](./leetcode/202.happy-number.py) | Hash Table | O\(S\) | O\(1\) | - |
423425
| [Leetcode-208](https://leetcode.com/problems/implement-trie-prefix-tree/) | Implement Trie Prefix Tree | [c++](./leetcode/208.implement-trie-prefix-tree.cpp), [python3](./leetcode/208.implement-trie-prefix-tree.py) | Hash Table | O\(N\) | O\(L\) | - |
424426
| [Leetcode-380](https://leetcode.com/problems/insert-delete-getrandom-o1/) | Insert Delete Getrandom O1 | [c++](./leetcode/380.insert-delete-getrandom-o1.cpp), [python3](./leetcode/380.insert-delete-getrandom-o1.py) | Hash Table | O\(1\) | O\(N\) | - |
427+
| [Leetcode-12](https://leetcode.com/problems/integer-to-roman/) | Integer To Roman | [c++](./leetcode/12.integer-to-roman.cpp), [python3](./leetcode/12.integer-to-roman.py) | Hash Table | O\(N\) | O\(1\) | - |
425428
| [Leetcode-160](https://leetcode.com/problems/intersection-of-two-linked-lists/) | Intersection Of Two Linked Lists | [c++](./leetcode/160.intersection-of-two-linked-lists.cpp), [python3](./leetcode/160.intersection-of-two-linked-lists.py) | Hash Table | O\(N\) | O\(1\) | - |
426429
| [Leetcode-205](https://leetcode.com/problems/isomorphic-strings/) | Isomorphic Strings | [c++](./leetcode/205.isomorphic-strings.cpp), [python3](./leetcode/205.isomorphic-strings.py) | Hash Table | O\(N\) | O\(N\) | - |
427430
| [Leetcode-141](https://leetcode.com/problems/linked-list-cycle/) | Linked List Cycle | [c++](./leetcode/141.linked-list-cycle.cpp), [python3](./leetcode/141.linked-list-cycle.py) | Hash Table | O\(N\) | O\(1\) | - |
@@ -439,6 +442,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
439442
| [Leetcode-268](https://leetcode.com/problems/missing-number/) | Missing Number | [c++](./leetcode/268.missing-number.cpp), [python3](./leetcode/268.missing-number.py) | Hash Table | O\(N\) | O\(1\) | - |
440443
| [Leetcode-763](https://leetcode.com/problems/partition-labels/) | Partition Labels | [c++](./leetcode/763.partition-labels.cpp), [python3](./leetcode/763.partition-labels.py) | Hash Table | O\(N\) | O\(N\) | - |
441444
| [Leetcode-383](https://leetcode.com/problems/ransom-note/) | Ransom Note | [c++](./leetcode/383.ransom-note.cpp), [python3](./leetcode/383.ransom-note.py) | Hash Table | O\(N\) | O\(N\) | - |
445+
| [Leetcode-13](https://leetcode.com/problems/roman-to-integer/) | Roman To Integer | [c++](./leetcode/13.roman-to-integer.cpp), [python3](./leetcode/13.roman-to-integer.py) | Hash Table | O\(N\) | O\(1\) | - |
442446
| [Leetcode-73](https://leetcode.com/problems/set-matrix-zeroes/) | Set Matrix Zeroes | [c++](./leetcode/73.set-matrix-zeroes.cpp), [python3](./leetcode/73.set-matrix-zeroes.py) | Hash Table | O\(NM\) | O\(N \+ M\) | - |
443447
| [Leetcode-480](https://leetcode.com/problems/sliding-window-median/) | Sliding Window Median | [c++](./leetcode/480.sliding-window-median.cpp), [python3](./leetcode/480.sliding-window-median.py) | Hash Table | O\(NlogK\) | O\(K\) | - |
444448
| [Leetcode-451](https://leetcode.com/problems/sort-characters-by-frequency/) | Sort Characters By Frequency | [c++](./leetcode/451.sort-characters-by-frequency.cpp), [python3](./leetcode/451.sort-characters-by-frequency.py) | Hash Table | O\(N\) | O\(N\) | - |
@@ -921,7 +925,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
921925

922926
## Other
923927

924-
| Link | Problem(126) | Solution | Tag | Time | Space | Ref |
928+
| Link | Problem(124) | Solution | Tag | Time | Space | Ref |
925929
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
926930
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
927931
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -965,7 +969,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
965969
| [Leetcode-249](https://leetcode.com/problems/group-shifted-strings/) | Group Shifted Strings | [c++](./leetcode/249.group-shifted-strings.cpp) | Other | \- | \- | - |
966970
| [Leetcode-57](https://leetcode.com/problems/insert-interval/) | Insert Interval | [c++](./leetcode/57.insert-interval.cpp), [python3](./leetcode/57.insert-interval.py) | Other | \- | \- | - |
967971
| [Leetcode-273](https://leetcode.com/problems/integer-to-english-words/) | Integer To English Words | [python3](./leetcode/273.integer-to-english-words.py) | Other | \- | \- | - |
968-
| [Leetcode-12](https://leetcode.com/problems/integer-to-roman/) | Integer To Roman | [c++](./leetcode/12.integer-to-roman.cpp), [python3](./leetcode/12.integer-to-roman.py) | Other | \- | \- | - |
969972
| [Leetcode-349](https://leetcode.com/problems/intersection-of-two-arrays/) | Intersection Of Two Arrays | [c++](./leetcode/349.intersection-of-two-arrays.cpp), [python3](./leetcode/349.intersection-of-two-arrays.py) | Other | \- | \- | - |
970973
| [Leetcode-350](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | Intersection Of Two Arrays II | [c++](./leetcode/350.intersection-of-two-arrays-ii.cpp), [python3](./leetcode/350.intersection-of-two-arrays-ii.py) | Other | \- | \- | - |
971974
| [Leetcode-751](https://leetcode.com/problems/ip-to-cidr/) | Ip To Cidr | [python3](./leetcode/751.ip-to-cidr.py) | Other | \- | \- | - |
@@ -1016,7 +1019,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
10161019
| [Leetcode-344](https://leetcode.com/problems/reverse-string/) | Reverse String | [c++](./leetcode/344.reverse-string.cpp), [python3](./leetcode/344.reverse-string.py) | Other | \- | \- | - |
10171020
| [Leetcode-345](https://leetcode.com/problems/reverse-vowels-of-a-string/) | Reverse Vowels Of A String | [c++](./leetcode/345.reverse-vowels-of-a-string.cpp), [python3](./leetcode/345.reverse-vowels-of-a-string.py) | Other | \- | \- | - |
10181021
| [Leetcode-657](https://leetcode.com/problems/robot-return-to-origin/) | Robot Return To Origin | [python3](./leetcode/657.robot-return-to-origin.py) | Other | \- | \- | - |
1019-
| [Leetcode-13](https://leetcode.com/problems/roman-to-integer/) | Roman To Integer | [c++](./leetcode/13.roman-to-integer.cpp), [python3](./leetcode/13.roman-to-integer.py) | Other | \- | \- | - |
10201022
| [Leetcode-61](https://leetcode.com/problems/rotate-list/) | Rotate List | [python3](./leetcode/61.rotate-list.py) | Other | \- | \- | - |
10211023
| [Leetcode-702](https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/) | Search In A Sorted Array Of Unknown Size | [c++](./leetcode/702.search-in-a-sorted-array-of-unknown-size.cpp), [python3](./leetcode/702.search-in-a-sorted-array-of-unknown-size.py) | Other | \- | \- | - |
10221024
| [Leetcode-444](https://leetcode.com/problems/sequence-reconstruction/) | Sequence Reconstruction | [c++](./leetcode/444.sequence-reconstruction.cpp), [python3](./leetcode/444.sequence-reconstruction.py) | Other | \- | \- | - |

leetcode/12.integer-to-roman.cpp

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,114 @@
1+
// Tag: Hash Table, Math, String
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Seven different symbols represent Roman numerals with the following values:
8+
//
9+
//
10+
//
11+
// Symbol
12+
// Value
13+
//
14+
//
15+
//
16+
//
17+
// I
18+
// 1
19+
//
20+
//
21+
// V
22+
// 5
23+
//
24+
//
25+
// X
26+
// 10
27+
//
28+
//
29+
// L
30+
// 50
31+
//
32+
//
33+
// C
34+
// 100
35+
//
36+
//
37+
// D
38+
// 500
39+
//
40+
//
41+
// M
42+
// 1000
43+
//
44+
//
45+
//
46+
// Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:
47+
//
48+
// If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
49+
// If the value starts with 4 or 9 use the subtractive form representing one symbol subtracted from the following symbol, for example, 4 is 1 (I) less than 5 (V): IV and 9 is 1 (I) less than 10 (X): IX. Only the following subtractive forms are used: 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD) and 900 (CM).
50+
// Only powers of 10 (I, X, C, M) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (V), 50 (L), or 500 (D) multiple times. If you need to append a symbol 4 times use the subtractive form.
51+
//
52+
// Given an integer, convert it to a Roman numeral.
53+
//  
54+
// Example 1:
55+
//
56+
// Input: num = 3749
57+
// Output: "MMMDCCXLIX"
58+
// Explanation:
59+
//
60+
// 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M)
61+
// 700 = DCC as 500 (D) + 100 (C) + 100 (C)
62+
// 40 = XL as 10 (X) less of 50 (L)
63+
// 9 = IX as 1 (I) less of 10 (X)
64+
// Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places
65+
//
66+
//
67+
// Example 2:
68+
//
69+
// Input: num = 58
70+
// Output: "LVIII"
71+
// Explanation:
72+
//
73+
// 50 = L
74+
// 8 = VIII
75+
//
76+
//
77+
// Example 3:
78+
//
79+
// Input: num = 1994
80+
// Output: "MCMXCIV"
81+
// Explanation:
82+
//
83+
// 1000 = M
84+
// 900 = CM
85+
// 90 = XC
86+
// 4 = IV
87+
//
88+
//
89+
//  
90+
// Constraints:
91+
//
92+
// 1 <= num <= 3999
93+
//
94+
//
95+
196
class Solution {
297
public:
398
string intToRoman(int num) {
499

5-
string romans[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
6-
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
100+
vector<string> romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
101+
vector<int> numbers = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
7102

8103
string res = "";
9104

10-
for (auto i = 0; i < 13; ++i)
11-
{
12-
while(num - values[i] >= 0)
13-
{
105+
for (auto i = 0; i < numbers.size(); ++i) {
106+
while(num - numbers[i] >= 0) {
14107
res += romans[i];
15-
num -= values[i];
108+
num -= numbers[i];
16109
}
17110
}
18111

19112
return res;
20113
}
21-
};
114+
};

leetcode/12.integer-to-roman.py

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,110 @@
1+
# Tag: Hash Table, Math, String
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# Seven different symbols represent Roman numerals with the following values:
8+
#
9+
#
10+
#
11+
# Symbol
12+
# Value
13+
#
14+
#
15+
#
16+
#
17+
# I
18+
# 1
19+
#
20+
#
21+
# V
22+
# 5
23+
#
24+
#
25+
# X
26+
# 10
27+
#
28+
#
29+
# L
30+
# 50
31+
#
32+
#
33+
# C
34+
# 100
35+
#
36+
#
37+
# D
38+
# 500
39+
#
40+
#
41+
# M
42+
# 1000
43+
#
44+
#
45+
#
46+
# Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:
47+
#
48+
# If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
49+
# If the value starts with 4 or 9 use the subtractive form representing one symbol subtracted from the following symbol, for example, 4 is 1 (I) less than 5 (V): IV and 9 is 1 (I) less than 10 (X): IX. Only the following subtractive forms are used: 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD) and 900 (CM).
50+
# Only powers of 10 (I, X, C, M) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (V), 50 (L), or 500 (D) multiple times. If you need to append a symbol 4 times use the subtractive form.
51+
#
52+
# Given an integer, convert it to a Roman numeral.
53+
#  
54+
# Example 1:
55+
#
56+
# Input: num = 3749
57+
# Output: "MMMDCCXLIX"
58+
# Explanation:
59+
#
60+
# 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M)
61+
# 700 = DCC as 500 (D) + 100 (C) + 100 (C)
62+
# 40 = XL as 10 (X) less of 50 (L)
63+
# 9 = IX as 1 (I) less of 10 (X)
64+
# Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places
65+
#
66+
#
67+
# Example 2:
68+
#
69+
# Input: num = 58
70+
# Output: "LVIII"
71+
# Explanation:
72+
#
73+
# 50 = L
74+
# 8 = VIII
75+
#
76+
#
77+
# Example 3:
78+
#
79+
# Input: num = 1994
80+
# Output: "MCMXCIV"
81+
# Explanation:
82+
#
83+
# 1000 = M
84+
# 900 = CM
85+
# 90 = XC
86+
# 4 = IV
87+
#
88+
#
89+
#  
90+
# Constraints:
91+
#
92+
# 1 <= num <= 3999
93+
#
94+
#
95+
196
class Solution:
297
def intToRoman(self, num: int) -> str:
3-
4-
romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
5-
values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
98+
romans = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
99+
numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
6100
res = ''
7101

8-
for i in range(len(romans)):
9-
while (num - values[i] >= 0):
102+
i = 0
103+
while i < len(numbers):
104+
if num >= numbers[i]:
10105
res += romans[i]
11-
num -= values[i]
106+
num -= numbers[i]
107+
else:
108+
i += 1
12109

13110
return res

0 commit comments

Comments
 (0)