Skip to content

Updated readmes #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0001_two_sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ You can return the answer in any order.

**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity?



## Solution

```python
Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0002_add_two_numbers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ You may assume the two numbers do not contain any leading zero, except the numbe
* `0 <= Node.val <= 9`
* It is guaranteed that the list represents a number that does not have leading zeros.



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ Given a string `s`, find the length of the **longest substring** without repeati
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
* `s` consists of English letters, digits, symbols and spaces.



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ The overall run time complexity should be `O(log (m+n))`.
* `1 <= m + n <= 2000`
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ Given a string `s`, return _the longest palindromic substring_ in `s`.
* `1 <= s.length <= 1000`
* `s` consist of only digits and English letters.



## Solution

```python
Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0006_zigzag_conversion/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ string convert(string s, int numRows);
* `s` consists of English letters (lower-case and upper-case), `','` and `'.'`.
* `1 <= numRows <= 1000`



## Solution

```python
Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0007_reverse_integer/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ Given a signed 32-bit integer `x`, return `x` _with its digits reversed_. If rev

* <code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code>



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ Since -91283472332 is less than the lower bound of the range [-2<sup>31</sup>, 2
* `0 <= s.length <= 200`
* `s` consists of English letters (lower-case and upper-case), digits (`0-9`), `' '`, `'+'`, `'-'`, and `'.'`.



## Solution

```python
Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0009_palindrome_number/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ An integer is a **palindrome** when it reads the same backward as forward. For e

**Follow up:** Could you solve it without converting the integer to a string?



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ The matching should cover the **entire** input string (not partial).
* `p` contains only lowercase English letters, `'.'`, and `'*'`.
* It is guaranteed for each appearance of the character `'*'`, there will be a previous valid character to match.



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ Given `n` non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n
* <code>2 <= n <= 10<sup>5</sup></code>
* <code>0 <= height[i] <= 10<sup>4</sup></code>



## Solution

```python
Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0015_3sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ Notice that the solution set must not contain duplicate triplets.
* `0 <= nums.length <= 3000`
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ A mapping of digit to letters (just like on the telephone buttons) is given belo
* `0 <= digits.length <= 4`
* `digits[i]` is a digit in the range `['2', '9']`.



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ Given the `head` of a linked list, remove the `nth` node from the end of the lis

**Follow up:** Could you do this in one pass?



## Solution

```python
Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0020_valid_parentheses/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ An input string is valid if:
* <code>1 <= s.length <= 10<sup>4</sup></code>
* `s` consists of parentheses only `'()[]{}'`.



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ Merge two sorted linked lists and return it as a **sorted** list. The list shoul
* `-100 <= Node.val <= 100`
* Both `l1` and `l2` are sorted in **non-decreasing** order.



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ Given `n` pairs of parentheses, write a function to _generate all combinations o

* `1 <= n <= 8`



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ _Merge all the linked-lists into one sorted linked-list and return it._
* `lists[i]` is sorted in **ascending order**.
* The sum of `lists[i].length` won't exceed `10^4`.



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ Given a linked list, swap every two adjacent nodes and return its head. You must
* The number of nodes in the list is in the range `[0, 100]`.
* `0 <= Node.val <= 100`



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ You may not alter the values in the list's nodes, only nodes themselves may be c

**Follow-up:** Can you solve the problem in O(1) extra memory space?



## Solution

```python
Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0031_next_permutation/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algor
* `1 <= nums.length <= 100`
* `0 <= nums[i] <= 100`



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ Given a string containing just the characters `'('` and `')'`, find the length o
* <code>0 <= s.length <= 3 * 10<sup>4</sup></code>
* `s[i]` is `'('`, or `')'`.



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ You must write an algorithm with `O(log n)` runtime complexity.
* `nums` is an ascending array that is possibly rotated.
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,73 @@ You must write an algorithm with `O(log n)` runtime complexity.
* `nums` is a non-decreasing array.
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>

To solve this problem efficiently with a runtime complexity of O(log n), we can use binary search to find the starting and ending positions of the target value in the sorted array.
To solve this problem efficiently with a runtime complexity of O(log n), we can use binary search to find the starting and ending positions of the target value in the sorted array. Here are the steps:

### Approach:

1. **Binary Search for Starting Position:**
- Use binary search to find the starting position of the target value.
- When the target value is found, continue searching towards the left to find the leftmost occurrence.
- Update the left boundary to narrow down the search space.

2. **Binary Search for Ending Position:**
- Use binary search to find the ending position of the target value.
- When the target value is found, continue searching towards the right to find the rightmost occurrence.
- Update the right boundary to narrow down the search space.

3. **Handle Missing Target:**
- If the target value is not found, return [-1, -1].

### Python Code:

```python
class Solution:
def searchRange(self, nums, target):
def search_boundary(nums, target, is_left):
left, right = 0, len(nums) - 1
boundary = -1

while left <= right:
mid = (left + right) // 2

if nums[mid] == target:
boundary = mid
if is_left:
right = mid - 1
else:
left = mid + 1
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1

return boundary

left_boundary = search_boundary(nums, target, is_left=True)
right_boundary = search_boundary(nums, target, is_left=False)

return [left_boundary, right_boundary]

# Example Usage:
solution = Solution()

# Example 1:
nums1 = [5, 7, 7, 8, 8, 10]
target1 = 8
print(solution.searchRange(nums1, target1)) # Output: [3, 4]

# Example 2:
nums2 = [5, 7, 7, 8, 8, 10]
target2 = 6
print(solution.searchRange(nums2, target2)) # Output: [-1, -1]

# Example 3:
nums3 = []
target3 = 0
print(solution.searchRange(nums3, target3)) # Output: [-1, -1]
```

This code defines a `Solution` class with a `searchRange` method to find the starting and ending positions of the target value in the given array. The example usage demonstrates how to create an instance of the `Solution` class and call the `searchRange` method with different inputs.

## Solution

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,65 @@ You must write an algorithm with `O(log n)` runtime complexity.
* `nums` contains **distinct** values sorted in **ascending** order.
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>

To solve this problem efficiently with a runtime complexity of O(log n), we can use binary search to find the insertion position of the target value in the sorted array.
To solve this problem efficiently with a runtime complexity of O(log n), we can use binary search to find the insertion position of the target value in the sorted array. Here are the steps:

### Approach:

1. **Binary Search:**
- Implement binary search to find the insertion position of the target value.
- If the target value is found, return its index.
- If the target value is not found, binary search will determine the index where the target would be inserted to maintain the sorted order.

### Python Code:

```python
class Solution:
def searchInsert(self, nums, target):
left, right = 0, len(nums) - 1

while left <= right:
mid = (left + right) // 2

if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1

# If the target is not found, return the insertion position (left).
return left

# Example Usage:
solution = Solution()

# Example 1:
nums1 = [1, 3, 5, 6]
target1 = 5
print(solution.searchInsert(nums1, target1)) # Output: 2

# Example 2:
nums2 = [1, 3, 5, 6]
target2 = 2
print(solution.searchInsert(nums2, target2)) # Output: 1

# Example 3:
nums3 = [1, 3, 5, 6]
target3 = 7
print(solution.searchInsert(nums3, target3)) # Output: 4

# Example 4:
nums4 = [1, 3, 5, 6]
target4 = 0
print(solution.searchInsert(nums4, target4)) # Output: 0

# Example 5:
nums5 = [1]
target5 = 0
print(solution.searchInsert(nums5, target5)) # Output: 0
```

This code defines a `Solution` class with a `searchInsert` method to find the insertion position of the target value in the given sorted array. The example usage demonstrates how to create an instance of the `Solution` class and call the `searchInsert` method with different inputs.

## Solution

Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0039_combination_sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ It is **guaranteed** that the number of unique combinations that sum up to `targ
* All elements of `candidates` are **distinct**.
* `1 <= target <= 500`



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ You must implement an algorithm that runs in `O(n)` time and uses constant extra
* <code>1 <= nums.length <= 5 * 10<sup>5</sup></code>
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>



## Solution

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ Given `n` non-negative integers representing an elevation map where the width of
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
* <code>0 <= height[i] <= 10<sup>5</sup></code>



## Solution

```python
Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0045_jump_game_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ You can assume that you can always reach the last index.
* <code>1 <= nums.length <= 10<sup>4</sup></code>
* `0 <= nums[i] <= 1000`



## Solution

```python
Expand Down
2 changes: 0 additions & 2 deletions src/main/python/g0001_0100/s0046_permutations/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ Given an array `nums` of distinct integers, return _all the possible permutation
* `-10 <= nums[i] <= 10`
* All the integers of `nums` are **unique**.



## Solution

```python
Expand Down
Loading