Skip to content

Commit 6b5f9eb

Browse files
committed
Update all solutions' changes in 2025-09-11.
1 parent 4f5419b commit 6b5f9eb

61 files changed

Lines changed: 1368 additions & 51 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

en/1-1000/1-two-sum.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,3 @@ Dear LeetCoders! For a better LeetCode problem-solving experience, please visit
328328
Original link: [1. Two Sum - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/1-two-sum).
329329

330330
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).
331-

en/1-1000/13-roman-to-integer.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# 13. Roman to Integer - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions
2+
3+
Visit original link: [13. Roman to Integer - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/13-roman-to-integer) for a better experience!
4+
5+
LeetCode link: [13. Roman to Integer](https://leetcode.com/problems/roman-to-integer), difficulty: **Easy**.
6+
7+
## LeetCode description of "13. Roman to Integer"
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, `2` is written as `II` in Roman numeral, just two ones added together. `12` is written as `XII`, which is simply `X` + `II`. The number `27` 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+
Given a roman numeral, convert it to an integer.
30+
31+
### [Example 1]
32+
33+
**Input**: `s = "III"`
34+
35+
**Output**: `3`
36+
37+
### [Example 2]
38+
39+
**Input**: `s = "IV"`
40+
41+
**Output**: `4`
42+
43+
### [Example 3]
44+
45+
**Input**: `s = "IX"`
46+
47+
**Output**: `9`
48+
49+
### [Example 4]
50+
51+
**Input**: `s = "LVIII"`
52+
53+
**Output**: `58`
54+
55+
**Explanation**: `L = 50, V= 5, III = 3.`
56+
57+
### [Example 5]
58+
59+
**Input**: `s = "MCMXCIV"`
60+
61+
**Output**: `1994`
62+
63+
**Explanation**: `M = 1000, CM = 900, XC = 90, IV = 4.`
64+
65+
### [Constraints]
66+
67+
- `1 <= s.length <= 15`
68+
- `s` contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M')`.
69+
- It is **guaranteed** that `s` is a valid roman numeral in the range `[1, 3999]`.
70+
71+
### [Hints]
72+
73+
<details>
74+
<summary>Hint 1</summary>
75+
Problem is simpler to solve by working the string from back to front and using a map.
76+
77+
78+
</details>
79+
80+
## Intuition
81+
82+
* The correspondence between characters and values can be represented with a `Map`.
83+
* The intuition is to add the value to `result` whenever a digit is encountered.
84+
* But cases like `IV` may occur, so you need to consider whether to process from left to right or from right to left. Which processing direction do you choose?
85+
<details><summary>Click to view the answer</summary><p> Processing from right to left is more convenient, because once you see that the current character and the previous character form a specific combination, you can handle it directly. </p></details>
86+
* How do you deal with cases like `IV`?
87+
<details><summary>Click to view the answer</summary><p> Just handle it in reverse. In the forward direction you do addition, but now you do subtraction. </p></details>
88+
89+
## Complexity
90+
91+
- Time complexity: `O(N)`.
92+
- Space complexity: `O(1)`.
93+
94+
## Ruby
95+
96+
```ruby
97+
# @param {String} s
98+
# @return {Integer}
99+
def roman_to_int(s)
100+
symbol_to_value = {
101+
'I' => 1,
102+
'V' => 5,
103+
'X' => 10,
104+
'L' => 50,
105+
'C' => 100,
106+
'D' => 500,
107+
'M' => 1000,
108+
}
109+
result = 0
110+
previous_char = nil
111+
112+
(s.size - 1).downto(0).each do |i|
113+
char = s[i]
114+
if ('I' == char && ['V', 'X'].include?(previous_char)) ||
115+
('X' == char && ['L', 'C'].include?(previous_char)) ||
116+
('C' == char && ['D', 'M'].include?(previous_char))
117+
result -= symbol_to_value[char]
118+
else
119+
result += symbol_to_value[char]
120+
end
121+
previous_char = char
122+
end
123+
124+
result
125+
end
126+
```
127+
128+
## Python
129+
130+
```python
131+
class Solution:
132+
def romanToInt(self, s: str) -> int:
133+
symbol_to_value = {
134+
'I': 1,
135+
'V': 5,
136+
'X': 10,
137+
'L': 50,
138+
'C': 100,
139+
'D': 500,
140+
'M': 1000,
141+
}
142+
result = 0
143+
previous_char = None
144+
145+
for i in range(len(s) - 1, -1, -1):
146+
char = s[i]
147+
148+
if ('I' == char and previous_char in ['V', 'X']) or \
149+
('X' == char and previous_char in ['L', 'C']) or \
150+
('C' == char and previous_char in ['D', 'M']):
151+
result -= symbol_to_value[char]
152+
else:
153+
result += symbol_to_value[char]
154+
155+
previous_char = char
156+
157+
return result
158+
```
159+
160+
## Other languages
161+
162+
```java
163+
// Welcome to create a PR to complete the code of this language, thanks!
164+
```
165+
166+
Dear LeetCoders! For a better LeetCode problem-solving experience, please visit website [LeetCode.to](https://leetcode.to): Dare to claim the best practices of LeetCode solutions! Will save you a lot of time!
167+
168+
Original link: [13. Roman to Integer - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/13-roman-to-integer).
169+
170+
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).

en/1-1000/15-3sum.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,3 @@ Dear LeetCoders! For a better LeetCode problem-solving experience, please visit
512512
Original link: [15. 3Sum - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/15-3sum).
513513

514514
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).
515-

en/1-1000/151-reverse-words-in-a-string.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,3 @@ Dear LeetCoders! For a better LeetCode problem-solving experience, please visit
186186
Original link: [151. Reverse Words in a String - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/151-reverse-words-in-a-string).
187187

188188
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).
189-

en/1-1000/160-intersection-of-two-linked-lists.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,3 @@ Dear LeetCoders! For a better LeetCode problem-solving experience, please visit
498498
Original link: [160. Intersection of Two Linked Lists - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/160-intersection-of-two-linked-lists).
499499

500500
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).
501-

en/1-1000/169-majority-element.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 169. Majority Element - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions
2+
3+
Visit original link: [169. Majority Element - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/169-majority-element) for a better experience!
4+
5+
LeetCode link: [169. Majority Element](https://leetcode.com/problems/majority-element), difficulty: **Easy**.
6+
7+
## LeetCode description of "169. Majority Element"
8+
9+
Given an array `nums` of size `n`, return _the majority element_.
10+
11+
The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.
12+
13+
### [Example 1]
14+
15+
**Input**: `nums = [3,2,3]`
16+
17+
**Output**: `3`
18+
19+
### [Example 2]
20+
21+
**Input**: `nums = [2,2,1,1,1,2,2]`
22+
23+
**Output**: `2`
24+
25+
### [Constraints]
26+
27+
- `n == nums.length`
28+
- `1 <= n <= 5 * 10^4`
29+
- `-10^9 <= nums[i] <= 10^9`
30+
31+
**Follow-up**: Could you solve the problem in linear time and in `O(1)` space?
32+
33+
### [Hints]
34+
35+
<details>
36+
<summary>Hint 1</summary>
37+
How to solve the problem in `O(1)` space?
38+
39+
Please search `Boyer-Moore majority vote algorithm`.
40+
41+
42+
</details>
43+
44+
## Intuition
45+
46+
The key to solving this problem is to use a hash table to store the occurrence count of each `num`. The `key` is the `num`, and the `value` is the number of times it appears.
47+
48+
## Complexity
49+
50+
- Time complexity: `O(N)`.
51+
- Space complexity: `O(N)`.
52+
53+
## Python
54+
55+
```python
56+
class Solution:
57+
def majorityElement(self, nums: List[int]) -> int:
58+
num_to_count = defaultdict(int)
59+
60+
for num in nums:
61+
num_to_count[num] += 1
62+
if num_to_count[num] >= len(nums) / 2:
63+
return num
64+
65+
```
66+
67+
## Ruby
68+
69+
```ruby
70+
# @param {Integer[]} nums
71+
# @return {Integer}
72+
def majority_element(nums)
73+
num_to_count = Hash.new(0)
74+
75+
nums.each do |num|
76+
num_to_count[num] += 1
77+
78+
if num_to_count[num] > nums.size / 2
79+
return num
80+
end
81+
end
82+
end
83+
```
84+
85+
## Other languages
86+
87+
```java
88+
// Welcome to create a PR to complete the code of this language, thanks!
89+
```
90+
91+
Dear LeetCoders! For a better LeetCode problem-solving experience, please visit website [LeetCode.to](https://leetcode.to): Dare to claim the best practices of LeetCode solutions! Will save you a lot of time!
92+
93+
Original link: [169. Majority Element - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/169-majority-element).
94+
95+
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).

en/1-1000/18-4sum.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,3 @@ Dear LeetCoders! For a better LeetCode problem-solving experience, please visit
375375
Original link: [18. 4Sum - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/18-4sum).
376376

377377
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).
378-

en/1-1000/19-remove-nth-node-from-end-of-list.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,3 @@ Dear LeetCoders! For a better LeetCode problem-solving experience, please visit
387387
Original link: [19. Remove Nth Node From End of List - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/19-remove-nth-node-from-end-of-list).
388388

389389
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).
390-

en/1-1000/20-valid-parentheses.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,3 @@ Dear LeetCoders! For a better LeetCode problem-solving experience, please visit
353353
Original link: [20. Valid Parentheses - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/20-valid-parentheses).
354354

355355
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).
356-

en/1-1000/202-happy-number.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,3 @@ Dear LeetCoders! For a better LeetCode problem-solving experience, please visit
277277
Original link: [202. Happy Number - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/202-happy-number).
278278

279279
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).
280-

0 commit comments

Comments
 (0)