Skip to content

Commit abc3e3f

Browse files
committed
Added tasks 12-54
1 parent 8833331 commit abc3e3f

File tree

14 files changed

+1016
-134
lines changed

14 files changed

+1016
-134
lines changed

README.md

+161-130
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 12\. Integer to Roman
5+
6+
Medium
7+
8+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
9+
10+
Symbol Value
11+
I 1
12+
V 5
13+
X 10
14+
L 50
15+
C 100
16+
D 500
17+
M 1000
18+
19+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
20+
21+
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:
22+
23+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
24+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
25+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
26+
27+
Given an integer, convert it to a roman numeral.
28+
29+
**Example 1:**
30+
31+
**Input:** num = 3
32+
33+
**Output:** "III"
34+
35+
**Example 2:**
36+
37+
**Input:** num = 4
38+
39+
**Output:** "IV"
40+
41+
**Example 3:**
42+
43+
**Input:** num = 9
44+
45+
**Output:** "IX"
46+
47+
**Example 4:**
48+
49+
**Input:** num = 58
50+
51+
**Output:** "LVIII"
52+
53+
**Explanation:** L = 50, V = 5, III = 3.
54+
55+
**Example 5:**
56+
57+
**Input:** num = 1994
58+
59+
**Output:** "MCMXCIV"
60+
61+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
62+
63+
**Constraints:**
64+
65+
* `1 <= num <= 3999`
66+
67+
## Solution
68+
69+
```typescript
70+
function intToRoman(num: number): string {
71+
const values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
72+
const symbols = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
73+
let result = ''
74+
let i = 0
75+
while (num > 0) {
76+
if (num >= values[i]) {
77+
result += symbols[i]
78+
num -= values[i]
79+
} else {
80+
i++
81+
}
82+
}
83+
return result
84+
}
85+
86+
export { intToRoman }
87+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 13\. Roman to Integer
5+
6+
Easy
7+
8+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
9+
10+
Symbol Value
11+
I 1
12+
V 5
13+
X 10
14+
L 50
15+
C 100
16+
D 500
17+
M 1000
18+
19+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
20+
21+
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:
22+
23+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
24+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
25+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
26+
27+
Given a roman numeral, convert it to an integer.
28+
29+
**Example 1:**
30+
31+
**Input:** s = "III"
32+
33+
**Output:** 3
34+
35+
**Example 2:**
36+
37+
**Input:** s = "IV"
38+
39+
**Output:** 4
40+
41+
**Example 3:**
42+
43+
**Input:** s = "IX"
44+
45+
**Output:** 9
46+
47+
**Example 4:**
48+
49+
**Input:** s = "LVIII"
50+
51+
**Output:** 58
52+
53+
**Explanation:** L = 50, V= 5, III = 3.
54+
55+
**Example 5:**
56+
57+
**Input:** s = "MCMXCIV"
58+
59+
**Output:** 1994
60+
61+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
62+
63+
**Constraints:**
64+
65+
* `1 <= s.length <= 15`
66+
* `s` contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M')`.
67+
* It is **guaranteed** that `s` is a valid roman numeral in the range `[1, 3999]`.
68+
69+
## Solution
70+
71+
```typescript
72+
function romanToInt(s: string): number {
73+
let x = 0
74+
let y: string
75+
for (let i = 0; i < s.length; i++) {
76+
y = s.charAt(i)
77+
switch (y) {
78+
case 'I':
79+
x = getX(s, x, i, 1, 'V', 'X')
80+
break
81+
case 'V':
82+
x += 5
83+
break
84+
case 'X':
85+
x = getX(s, x, i, 10, 'L', 'C')
86+
break
87+
case 'L':
88+
x += 50
89+
break
90+
case 'C':
91+
x = getX(s, x, i, 100, 'D', 'M')
92+
break
93+
case 'D':
94+
x += 500
95+
break
96+
case 'M':
97+
x += 1000
98+
break
99+
default:
100+
break
101+
}
102+
}
103+
return x
104+
}
105+
106+
function getX(s: string, x: number, i: number, i2: number, v: string, x2: string): number {
107+
if (i + 1 === s.length) {
108+
x += i2
109+
} else if (s.charAt(i + 1) === v || s.charAt(i + 1) === x2) {
110+
x -= i2
111+
} else {
112+
x += i2
113+
}
114+
return x
115+
}
116+
117+
export { romanToInt }
118+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 14\. Longest Common Prefix
5+
6+
Easy
7+
8+
Write a function to find the longest common prefix string amongst an array of strings.
9+
10+
If there is no common prefix, return an empty string `""`.
11+
12+
**Example 1:**
13+
14+
**Input:** strs = ["flower","flow","flight"]
15+
16+
**Output:** "fl"
17+
18+
**Example 2:**
19+
20+
**Input:** strs = ["dog","racecar","car"]
21+
22+
**Output:** ""
23+
24+
**Explanation:** There is no common prefix among the input strings.
25+
26+
**Constraints:**
27+
28+
* `1 <= strs.length <= 200`
29+
* `0 <= strs[i].length <= 200`
30+
* `strs[i]` consists of only lower-case English letters.
31+
32+
## Solution
33+
34+
```typescript
35+
function longestCommonPrefix(strs: string[]): string {
36+
if (strs.length < 1) {
37+
return ''
38+
}
39+
if (strs.length === 1) {
40+
return strs[0]
41+
}
42+
let temp = strs[0]
43+
let i = 1
44+
let cur: string
45+
while (temp.length > 0 && i < strs.length) {
46+
if (temp.length > strs[i].length) {
47+
temp = temp.substring(0, strs[i].length)
48+
}
49+
cur = strs[i].substring(0, temp.length)
50+
if (cur !== temp) {
51+
temp = temp.substring(0, temp.length - 1)
52+
} else {
53+
i++
54+
}
55+
}
56+
return temp
57+
}
58+
59+
export { longestCommonPrefix }
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 26\. Remove Duplicates from Sorted Array
5+
6+
Easy
7+
8+
Given an integer array `nums` sorted in **non-decreasing order**, remove the duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears only **once**. The **relative order** of the elements should be kept the **same**.
9+
10+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
11+
12+
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
13+
14+
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
15+
16+
**Custom Judge:**
17+
18+
The judge will test your solution with the following code:
19+
20+
int[] nums = [...]; // Input array
21+
int[] expectedNums = [...]; // The expected answer with correct length
22+
23+
int k = removeDuplicates(nums); // Calls your implementation
24+
25+
assert k == expectedNums.length;
26+
for (int i = 0; i < k; i++) {
27+
assert nums[i] == expectedNums[i];
28+
}
29+
30+
If all assertions pass, then your solution will be **accepted**.
31+
32+
**Example 1:**
33+
34+
**Input:** nums = [1,1,2]
35+
36+
**Output:** 2, nums = [1,2,\_]
37+
38+
**Explanation:** Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
39+
40+
**Example 2:**
41+
42+
**Input:** nums = [0,0,1,1,1,2,2,3,3,4]
43+
44+
**Output:** 5, nums = [0,1,2,3,4,\_,\_,\_,\_,\_]
45+
46+
**Explanation:** Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
47+
48+
**Constraints:**
49+
50+
* <code>0 <= nums.length <= 3 * 10<sup>4</sup></code>
51+
* `-100 <= nums[i] <= 100`
52+
* `nums` is sorted in **non-decreasing** order.
53+
54+
## Solution
55+
56+
```typescript
57+
function removeDuplicates(nums: number[]): number {
58+
let n = nums.length
59+
let i = 0
60+
let j = 1
61+
if (n <= 1) {
62+
return n
63+
}
64+
while (j <= n - 1) {
65+
if (nums[i] !== nums[j]) {
66+
nums[i + 1] = nums[j]
67+
i++
68+
}
69+
j++
70+
}
71+
return i + 1
72+
}
73+
74+
export { removeDuplicates }
75+
```

0 commit comments

Comments
 (0)