Skip to content

Commit 3358194

Browse files
authored
Improved readmes
1 parent ff5b457 commit 3358194

File tree

18 files changed

+17
-476
lines changed

18 files changed

+17
-476
lines changed

src/main/python/g0001_0100/s0001_two_sum/readme.md

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,4 @@ print(solution.twoSum(nums3, target3)) # Output: [0, 1]
101101
```
102102

103103
### Time Complexity:
104-
The time complexity of this algorithm is O(n), where n is the number of elements in the array. The dictionary lookup operation is constant time.
105-
106-
## Solution
107-
108-
```python
109-
class Solution:
110-
def twoSum(self, numbers: List[int], target: int) -> List[int]:
111-
index_map = {}
112-
for i, num in enumerate(numbers):
113-
required_num = target - num
114-
if required_num in index_map:
115-
return [index_map[required_num], i]
116-
index_map[num] = i
117-
return [-1, -1]
118-
```
104+
The time complexity of this algorithm is O(n), where n is the number of elements in the array. The dictionary lookup operation is constant time.

src/main/python/g0001_0100/s0002_add_two_numbers/readme.md

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -127,36 +127,4 @@ result = solution.addTwoNumbers(l1, l2)
127127
# Output: [8, 9, 9, 9, 0, 0, 0, 1]
128128
```
129129

130-
This code defines a `ListNode` class for the singly-linked list and a `Solution` class with a method `addTwoNumbers` that takes two linked lists as input and returns the result as a linked list. The example usage demonstrates how to create instances of the `ListNode` class and call the `addTwoNumbers` method with different inputs.
131-
132-
## Solution
133-
134-
```python
135-
# Definition for singly-linked list.
136-
# class ListNode:
137-
# def __init__(self, val=0, next=None):
138-
# self.val = val
139-
# self.next = next
140-
class Solution:
141-
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
142-
dummy_head = ListNode(0)
143-
p, q, curr = l1, l2, dummy_head
144-
carry = 0
145-
146-
while p is not None or q is not None:
147-
x = p.val if p is not None else 0
148-
y = q.val if q is not None else 0
149-
sum = carry + x + y
150-
carry = sum // 10
151-
curr.next = ListNode(sum % 10)
152-
curr = curr.next
153-
if p is not None:
154-
p = p.next
155-
if q is not None:
156-
q = q.next
157-
158-
if carry > 0:
159-
curr.next = ListNode(carry)
160-
161-
return dummy_head.next
162-
```
130+
This code defines a `ListNode` class for the singly-linked list and a `Solution` class with a method `addTwoNumbers` that takes two linked lists as input and returns the result as a linked list. The example usage demonstrates how to create instances of the `ListNode` class and call the `addTwoNumbers` method with different inputs.

src/main/python/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,30 +120,4 @@ s4 = ""
120120
print(solution.lengthOfLongestSubstring(s4)) # Output: 0
121121
```
122122

123-
This code defines a `Solution` class with a method `lengthOfLongestSubstring` that takes a string `s` as input and returns the length of the longest substring without repeating characters. The example usage demonstrates how to create an instance of the `Solution` class and call the `lengthOfLongestSubstring` method with different inputs.
124-
125-
## Solution
126-
127-
```python
128-
class Solution:
129-
def lengthOfLongestSubstring(self, s: str) -> int:
130-
last_indices = [-1] * 256
131-
max_len = 0
132-
cur_len = 0
133-
start = 0
134-
135-
for i, char in enumerate(s):
136-
if last_indices[ord(char)] < start:
137-
last_indices[ord(char)] = i
138-
cur_len += 1
139-
else:
140-
last_index = last_indices[ord(char)]
141-
start = last_index + 1
142-
cur_len = i - start + 1
143-
last_indices[ord(char)] = i
144-
145-
if cur_len > max_len:
146-
max_len = cur_len
147-
148-
return max_len
149-
```
123+
This code defines a `Solution` class with a method `lengthOfLongestSubstring` that takes a string `s` as input and returns the length of the longest substring without repeating characters. The example usage demonstrates how to create an instance of the `Solution` class and call the `lengthOfLongestSubstring` method with different inputs.

src/main/python/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,4 @@ nums2_5 = []
116116
print(solution.findMedianSortedArrays(nums1_5, nums2_5)) # Output: 2.00000
117117
```
118118

119-
This code defines a `Solution` class with a method `findMedianSortedArrays` that takes two sorted arrays (`nums1` and `nums2`) as input and returns the median of the combined sorted array. The example usage demonstrates how to create an instance of the `Solution` class and call the `findMedianSortedArrays` method with different inputs.
120-
121-
## Solution
122-
123-
```python
124-
class Solution:
125-
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
126-
if len(nums2) < len(nums1):
127-
return self.findMedianSortedArrays(nums2, nums1)
128-
129-
n1, n2 = len(nums1), len(nums2)
130-
low, high = 0, n1
131-
132-
while low <= high:
133-
cut1 = (low + high) // 2
134-
cut2 = (n1 + n2 + 1) // 2 - cut1
135-
136-
l1 = float('-inf') if cut1 == 0 else nums1[cut1 - 1]
137-
l2 = float('-inf') if cut2 == 0 else nums2[cut2 - 1]
138-
r1 = float('inf') if cut1 == n1 else nums1[cut1]
139-
r2 = float('inf') if cut2 == n2 else nums2[cut2]
140-
141-
if l1 <= r2 and l2 <= r1:
142-
if (n1 + n2) % 2 == 0:
143-
return (max(l1, l2) + min(r1, r2)) / 2.0
144-
return max(l1, l2)
145-
elif l1 > r2:
146-
high = cut1 - 1
147-
else:
148-
low = cut1 + 1
149-
150-
return 0.0
151-
```
119+
This code defines a `Solution` class with a method `findMedianSortedArrays` that takes two sorted arrays (`nums1` and `nums2`) as input and returns the median of the combined sorted array. The example usage demonstrates how to create an instance of the `Solution` class and call the `findMedianSortedArrays` method with different inputs.

src/main/python/g0001_0100/s0005_longest_palindromic_substring/readme.md

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -117,42 +117,4 @@ s4 = "ac"
117117
print(solution.longestPalindrome(s4)) # Output: "a"
118118
```
119119

120-
This code defines a `Solution` class with a method `longestPalindrome` that takes a string `s` as input and returns the longest palindromic substring. The example usage demonstrates how to create an instance of the `Solution` class and call the `longestPalindrome` method with different inputs.
121-
122-
## Solution
123-
124-
```python
125-
class Solution:
126-
def longestPalindrome(self, s: str) -> str:
127-
# Create a new string with separators
128-
new_str = ['#'] * (2 * len(s) + 1)
129-
for i in range(len(s)):
130-
new_str[2 * i + 1] = s[i]
131-
132-
dp = [0] * len(new_str)
133-
friend_center = 0
134-
friend_radius = 0
135-
lps_center = 0
136-
lps_radius = 0
137-
138-
for i in range(len(new_str)):
139-
if friend_center + friend_radius > i:
140-
dp[i] = min(dp[2 * friend_center - i], (friend_center + friend_radius) - i)
141-
else:
142-
dp[i] = 1
143-
144-
while i + dp[i] < len(new_str) and i - dp[i] >= 0 and new_str[i + dp[i]] == new_str[i - dp[i]]:
145-
dp[i] += 1
146-
147-
if friend_center + friend_radius < i + dp[i]:
148-
friend_center = i
149-
friend_radius = dp[i]
150-
151-
if lps_radius < dp[i]:
152-
lps_center = i
153-
lps_radius = dp[i]
154-
155-
start = (lps_center - lps_radius + 1) // 2
156-
end = (lps_center + lps_radius - 1) // 2
157-
return s[start:end]
158-
```
120+
This code defines a `Solution` class with a method `longestPalindrome` that takes a string `s` as input and returns the longest palindromic substring. The example usage demonstrates how to create an instance of the `Solution` class and call the `longestPalindrome` method with different inputs.

src/main/python/g0001_0100/s0006_zigzag_conversion/readme.md

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,33 +118,4 @@ numRows3 = 1
118118
print(solution.convert(s3, numRows3)) # Output: "A"
119119
```
120120

121-
This code defines a `Solution` class with a method `convert` that takes a string `s` and the number of rows `numRows` as input and returns the zigzag conversion of the string. The example usage demonstrates how to create an instance of the `Solution` class and call the `convert` method with different inputs.
122-
123-
## Solution
124-
125-
```python
126-
class Solution:
127-
def convert(self, s: str, numRows: int) -> str:
128-
s_len = len(s)
129-
if numRows == 1:
130-
return s
131-
132-
max_dist = numRows * 2 - 2
133-
buf = []
134-
135-
for i in range(numRows):
136-
index = i
137-
if i == 0 or i == numRows - 1:
138-
while index < s_len:
139-
buf.append(s[index])
140-
index += max_dist
141-
else:
142-
while index < s_len:
143-
buf.append(s[index])
144-
index += max_dist - i * 2
145-
if index >= s_len:
146-
break
147-
buf.append(s[index])
148-
index += i * 2
149-
return ''.join(buf)
150-
```
121+
This code defines a `Solution` class with a method `convert` that takes a string `s` and the number of rows `numRows` as input and returns the zigzag conversion of the string. The example usage demonstrates how to create an instance of the `Solution` class and call the `convert` method with different inputs.

src/main/python/g0001_0100/s0007_reverse_integer/readme.md

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,4 @@ x4 = 0
124124
print(solution.reverse(x4)) # Output: 0
125125
```
126126

127-
This code defines a `Solution` class with a method `reverse` that takes an integer `x` as input and returns the reversed integer. The example usage demonstrates how to create an instance of the `Solution` class and call the `reverse` method with different inputs.
128-
129-
## Solution
130-
131-
```python
132-
class Solution:
133-
def reverse(self, x: int) -> int:
134-
rev = 0
135-
sign = -1 if x < 0 else 1
136-
x = abs(x)
137-
138-
while x != 0:
139-
rev = rev * 10 + x % 10
140-
x //= 10
141-
142-
rev *= sign
143-
144-
if rev > 2**31 - 1 or rev < -2**31:
145-
return 0
146-
147-
return rev
148-
```
127+
This code defines a `Solution` class with a method `reverse` that takes an integer `x` as input and returns the reversed integer. The example usage demonstrates how to create an instance of the `Solution` class and call the `reverse` method with different inputs.

src/main/python/g0001_0100/s0008_string_to_integer_atoi/readme.md

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -206,37 +206,4 @@ s5 = "-91283472332"
206206
print(solution.myAtoi(s5)) # Output: -2147483648
207207
```
208208

209-
This code defines a `Solution` class with a method `myAtoi` that takes a string `s` as input and returns the 32-bit signed integer. The example usage demonstrates how to create an instance of the `Solution` class and call the `myAtoi` method with different inputs.
210-
211-
## Solution
212-
213-
```python
214-
class Solution:
215-
def myAtoi(self, s: str) -> int:
216-
s = s.lstrip()
217-
if s == '':
218-
return 0
219-
220-
result = ''
221-
i = 0
222-
223-
negative = False
224-
if s[0] == '-':
225-
negative = True
226-
i += 1
227-
elif s[0] == '+':
228-
i += 1
229-
230-
while i < len(s) and s[i].isnumeric():
231-
result += s[i]
232-
i += 1
233-
234-
if result:
235-
result = -1*int(result) if negative else int(result)
236-
if result < -2**31:
237-
return -2**31
238-
elif result > 2**31 - 1:
239-
return 2**31 - 1
240-
return result
241-
return 0
242-
```
209+
This code defines a `Solution` class with a method `myAtoi` that takes a string `s` as input and returns the 32-bit signed integer. The example usage demonstrates how to create an instance of the `Solution` class and call the `myAtoi` method with different inputs.

src/main/python/g0001_0100/s0009_palindrome_number/readme.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,4 @@ x4 = -101
103103
print(solution.isPalindrome(x4)) # Output: False
104104
```
105105

106-
This code defines a `Solution` class with a method `isPalindrome` that takes an integer `x` as input and returns `True` if it is a palindrome and `False` otherwise. The example usage demonstrates how to create an instance of the `Solution` class and call the `isPalindrome` method with different inputs. The `reverse` method is used to reverse the digits of a positive integer.
107-
108-
## Solution
109-
110-
```python
111-
class Solution:
112-
def isPalindrome(self, x: int) -> bool:
113-
return str(x) == str(x)[::-1]
114-
```
106+
This code defines a `Solution` class with a method `isPalindrome` that takes an integer `x` as input and returns `True` if it is a palindrome and `False` otherwise. The example usage demonstrates how to create an instance of the `Solution` class and call the `isPalindrome` method with different inputs. The `reverse` method is used to reverse the digits of a positive integer.

src/main/python/g0001_0100/s0010_regular_expression_matching/readme.md

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,4 @@ s5, p5 = "mississippi", "mis*is*p*."
119119
print(solution.isMatch(s5, p5)) # Output: False
120120
```
121121

122-
This code defines a `Solution` class with a method `isMatch` that takes a string `s` and a pattern `p` as input and returns `True` if they match and `False` otherwise. The example usage demonstrates how to create an instance of the `Solution` class and call the `isMatch` method with different inputs using the recursive approach.
123-
124-
## Solution
125-
126-
```python
127-
class Solution:
128-
def __init__(self):
129-
self.cache = None
130-
131-
def isMatch(self, s: str, p: str) -> bool:
132-
self.cache = [[None] * (len(p) + 1) for _ in range(len(s) + 1)]
133-
return self._isMatch(s, p, 0, 0)
134-
135-
def _isMatch(self, s: str, p: str, i: int, j: int) -> bool:
136-
if j == len(p):
137-
return i == len(s)
138-
139-
if self.cache[i][j] is not None:
140-
return self.cache[i][j]
141-
142-
first_match = i < len(s) and (s[i] == p[j] or p[j] == '.')
143-
144-
if j + 1 < len(p) and p[j + 1] == '*':
145-
result = (first_match and self._isMatch(s, p, i + 1, j)) or self._isMatch(s, p, i, j + 2)
146-
else:
147-
result = first_match and self._isMatch(s, p, i + 1, j + 1)
148-
149-
self.cache[i][j] = result
150-
return result
151-
```
122+
This code defines a `Solution` class with a method `isMatch` that takes a string `s` and a pattern `p` as input and returns `True` if they match and `False` otherwise. The example usage demonstrates how to create an instance of the `Solution` class and call the `isMatch` method with different inputs using the recursive approach.

src/main/python/g0001_0100/s0011_container_with_most_water/readme.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,4 @@ height4 = [1, 2, 1]
114114
print(solution.maxArea(height4)) # Output: 2
115115
```
116116

117-
This code defines a `Solution` class with a method `maxArea` that takes an array of heights as input and returns the maximum area that can be formed by a container. The example usage demonstrates how to create an instance of the `Solution` class and call the `maxArea` method with different inputs.
118-
119-
## Solution
120-
121-
```python
122-
class Solution:
123-
def maxArea(self, height: List[int]) -> int:
124-
max_area = -1
125-
left = 0
126-
right = len(height) - 1
127-
while left < right:
128-
if height[left] < height[right]:
129-
max_area = max(max_area, height[left] * (right - left))
130-
left += 1
131-
else:
132-
max_area = max(max_area, height[right] * (right - left))
133-
right -= 1
134-
return max_area
135-
```
117+
This code defines a `Solution` class with a method `maxArea` that takes an array of heights as input and returns the maximum area that can be formed by a container. The example usage demonstrates how to create an instance of the `Solution` class and call the `maxArea` method with different inputs.

0 commit comments

Comments
 (0)