diff --git a/src/main/java/g0001_0100/s0001_two_sum/readme.md b/src/main/java/g0001_0100/s0001_two_sum/readme.md index 811079c85..c51a2d424 100644 --- a/src/main/java/g0001_0100/s0001_two_sum/readme.md +++ b/src/main/java/g0001_0100/s0001_two_sum/readme.md @@ -97,4 +97,4 @@ public class Solution { } ``` -This implementation provides a solution to the Two Sum problem with a time complexity of O(n), where n is the number of elements in the input array. \ No newline at end of file +This implementation provides a solution to the Two Sum problem with a time complexity of O(n), where n is the number of elements in the input array. diff --git a/src/main/java/g0001_0100/s0002_add_two_numbers/readme.md b/src/main/java/g0001_0100/s0002_add_two_numbers/readme.md index 77abd6701..82855a16b 100644 --- a/src/main/java/g0001_0100/s0002_add_two_numbers/readme.md +++ b/src/main/java/g0001_0100/s0002_add_two_numbers/readme.md @@ -130,4 +130,4 @@ public class Solution { } ``` -This implementation provides a solution to the Add Two Numbers problem using linked lists in Java. \ No newline at end of file +This implementation provides a solution to the Add Two Numbers problem using linked lists in Java. diff --git a/src/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md b/src/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md index f4856aff1..447d996d9 100644 --- a/src/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md +++ b/src/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md @@ -2,7 +2,7 @@ Medium -Given a string `s`, find the length of the **longest substring** without repeating characters. +Given a string `s`, find the length of the **longest** **substring** without duplicate characters. **Example 1:** @@ -10,7 +10,7 @@ Given a string `s`, find the length of the **longest substring** without repeati **Output:** 3 -**Explanation:** The answer is "abc", with the length of 3. +**Explanation:** The answer is "abc", with the length of 3. Note that `"bca"` and `"cab"` are also correct answers. **Example 2:** @@ -28,12 +28,6 @@ Given a string `s`, find the length of the **longest substring** without repeati **Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. -**Example 4:** - -**Input:** s = "" - -**Output:** 0 - **Constraints:** * 0 <= s.length <= 5 * 104 @@ -100,4 +94,4 @@ public class Solution { } ``` -This implementation provides a solution to the Longest Substring Without Repeating Characters problem in Java. \ No newline at end of file +This implementation provides a solution to the Longest Substring Without Repeating Characters problem in Java. diff --git a/src/main/java/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md b/src/main/java/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md index 09e2fd08c..903c7dde0 100644 --- a/src/main/java/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md +++ b/src/main/java/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md @@ -22,24 +22,6 @@ The overall run time complexity should be `O(log (m+n))`. **Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. -**Example 3:** - -**Input:** nums1 = [0,0], nums2 = [0,0] - -**Output:** 0.00000 - -**Example 4:** - -**Input:** nums1 = [], nums2 = [1] - -**Output:** 1.00000 - -**Example 5:** - -**Input:** nums1 = [2], nums2 = [] - -**Output:** 2.00000 - **Constraints:** * `nums1.length == m` @@ -115,4 +97,4 @@ public class Solution { } ``` -This implementation provides a solution to the Median of Two Sorted Arrays problem in Java with a runtime complexity of O(log(min(m, n))). \ No newline at end of file +This implementation provides a solution to the Median of Two Sorted Arrays problem in Java with a runtime complexity of O(log(min(m, n))). diff --git a/src/main/java/g0001_0100/s0005_longest_palindromic_substring/readme.md b/src/main/java/g0001_0100/s0005_longest_palindromic_substring/readme.md index aa6a60cf4..5e3c1c6f4 100644 --- a/src/main/java/g0001_0100/s0005_longest_palindromic_substring/readme.md +++ b/src/main/java/g0001_0100/s0005_longest_palindromic_substring/readme.md @@ -2,13 +2,15 @@ Medium -Given a string `s`, return _the longest palindromic substring_ in `s`. +Given a string `s`, return _the longest_ _palindromic_ **substring** in `s`. **Example 1:** **Input:** s = "babad" -**Output:** "bab" **Note:** "aba" is also a valid answer. +**Output:** "bab" + +**Explanation:** "aba" is also a valid answer. **Example 2:** @@ -16,18 +18,6 @@ Given a string `s`, return _the longest palindromic substring_ in `s`. **Output:** "bb" -**Example 3:** - -**Input:** s = "a" - -**Output:** "a" - -**Example 4:** - -**Input:** s = "ac" - -**Output:** "a" - **Constraints:** * `1 <= s.length <= 1000` @@ -94,4 +84,4 @@ public class Solution { } ``` -This implementation provides a solution to the Longest Palindromic Substring problem in Java. \ No newline at end of file +This implementation provides a solution to the Longest Palindromic Substring problem in Java. diff --git a/src/main/java/g0001_0100/s0006_zigzag_conversion/readme.md b/src/main/java/g0001_0100/s0006_zigzag_conversion/readme.md index db1939f2a..c0842869e 100644 --- a/src/main/java/g0001_0100/s0006_zigzag_conversion/readme.md +++ b/src/main/java/g0001_0100/s0006_zigzag_conversion/readme.md @@ -104,4 +104,4 @@ public class Solution { } ``` -This implementation provides a solution to the Zigzag Conversion problem in Java. \ No newline at end of file +This implementation provides a solution to the Zigzag Conversion problem in Java. diff --git a/src/main/java/g0001_0100/s0007_reverse_integer/readme.md b/src/main/java/g0001_0100/s0007_reverse_integer/readme.md index a49c7222c..d2a1aae04 100644 --- a/src/main/java/g0001_0100/s0007_reverse_integer/readme.md +++ b/src/main/java/g0001_0100/s0007_reverse_integer/readme.md @@ -24,12 +24,6 @@ Given a signed 32-bit integer `x`, return `x` _with its digits reversed_. If rev **Output:** 21 -**Example 4:** - -**Input:** x = 0 - -**Output:** 0 - **Constraints:** * -231 <= x <= 231 - 1 @@ -93,4 +87,4 @@ public class Solution { } ``` -This implementation provides a solution to the Reverse Integer problem in Java. \ No newline at end of file +This implementation provides a solution to the Reverse Integer problem in Java. diff --git a/src/main/java/g0001_0100/s0008_string_to_integer_atoi/readme.md b/src/main/java/g0001_0100/s0008_string_to_integer_atoi/readme.md index 86bc9f4fd..743eb17cc 100644 --- a/src/main/java/g0001_0100/s0008_string_to_integer_atoi/readme.md +++ b/src/main/java/g0001_0100/s0008_string_to_integer_atoi/readme.md @@ -2,21 +2,16 @@ Medium -Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer (similar to C/C++'s `atoi` function). +Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer. The algorithm for `myAtoi(string s)` is as follows: -1. Read in and ignore any leading whitespace. -2. Check if the next character (if not already at the end of the string) is `'-'` or `'+'`. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present. -3. Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored. -4. Convert these digits into an integer (i.e. `"123" -> 123`, `"0032" -> 32`). If no digits were read, then the integer is `0`. Change the sign as necessary (from step 2). -5. If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1. -6. Return the integer as the final result. +1. **Whitespace**: Ignore any leading whitespace (`" "`). +2. **Signedness**: Determine the sign by checking if the next character is `'-'` or `'+'`, assuming positivity if neither present. +3. **Conversion**: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0. +4. **Rounding**: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1. -**Note:** - -* Only the space character `' '` is considered a whitespace character. -* **Do not ignore** any characters other than the leading whitespace or the rest of the string after the digits. +Return the integer as the final result. **Example 1:** @@ -24,93 +19,70 @@ The algorithm for `myAtoi(string s)` is as follows: **Output:** 42 -**Explanation:** The underlined characters are what is read in, the caret is the current reader position. +**Explanation:** + The underlined characters are what is read in and the caret is the current reader position. Step 1: "42" (no characters read because there is no leading whitespace) - ^ + ^ Step 2: "42" (no characters read because there is neither a '-' nor '+') ^ Step 3: "42" ("42" is read in) - ^ - -The parsed integer is 42. Since 42 is in the range [-231, 231 - 1], the final result is 42. + ^ **Example 2:** -**Input:** s = " -42" +**Input:** s = " -042" -**Output:** -42 +**Output:** \-42 **Explanation:** - Step 1: " -42" (leading whitespace is read and ignored) - ^ - Step 2: " -42" ('-' is read, so the result should be negative) - ^ - Step 3: " -42" ("42" is read in) - ^ - The parsed integer is -42. - -Since -42 is in the range [-231, 231 - 1], the final result is -42. + Step 1: "___-042" (leading whitespace is read and ignored) + ^ + Step 2: " -042" ('-' is read, so the result should be negative) + ^ + Step 3: " -042" ("042" is read in, leading zeros ignored in the result) + ^ **Example 3:** -**Input:** s = "4193 with words" +**Input:** s = "1337c0d3" -**Output:** 4193 +**Output:** 1337 **Explanation:** - Step 1: "4193 with words" (no characters read because there is no leading whitespace) + Step 1: "1337c0d3" (no characters read because there is no leading whitespace) ^ - Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+') + Step 2: "1337c0d3" (no characters read because there is neither a '-' nor '+') ^ - Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit) + Step 3: "1337c0d3" ("1337" is read in; reading stops because the next character is a non-digit) ^ - The parsed integer is 4193. - -Since 4193 is in the range [-231, 231 - 1], the final result is 4193. **Example 4:** -**Input:** s = "words and 987" +**Input:** s = "0-1" **Output:** 0 **Explanation:** - Step 1: "words and 987" (no characters read because there is no leading whitespace) + Step 1: "0-1" (no characters read because there is no leading whitespace) ^ - Step 2: "words and 987" (no characters read because there is neither a '-' nor '+') + Step 2: "0-1" (no characters read because there is neither a '-' nor '+') ^ - Step 3: "words and 987" (reading stops immediately because there is a non-digit 'w') - ^ - The parsed integer is 0 because no digits were read. - -Since 0 is in the range [-231, 231 - 1], the final result is 0. + Step 3: "0-1" ("0" is read in; reading stops because the next character is a non-digit) + ^ **Example 5:** -**Input:** s = "-91283472332" +**Input:** s = "words and 987" -**Output:** -2147483648 +**Output:** 0 **Explanation:** - Step 1: "-91283472332" (no characters read because there is no leading whitespace) - ^ - Step 2: "-91283472332" ('-' is read, so the result should be negative) - ^ - Step 3: "-91283472332" ("91283472332" is read in) - ^ - The parsed integer is -91283472332. - -Since -91283472332 is less than the lower bound of the range [-231, 231 - 1], the final result is clamped to -231 = -2147483648. - -**Constraints:** - -* `0 <= s.length <= 200` -* `s` consists of English letters (lower-case and upper-case), digits (`0-9`), `' '`, `'+'`, `'-'`, and `'.'`. +Reading stops at the first non-digit character 'w'. To solve the String to Integer (atoi) problem in Java using a `Solution` class, we'll follow these steps: @@ -181,4 +153,9 @@ public class Solution { } ``` -This implementation provides a solution to the String to Integer (atoi) problem in Java. \ No newline at end of file +This implementation provides a solution to the String to Integer (atoi) problem in Java. + +**Constraints:** + +* `0 <= s.length <= 200` +* `s` consists of English letters (lower-case and upper-case), digits (`0-9`), `' '`, `'+'`, `'-'`, and `'.'`. diff --git a/src/main/java/g0001_0100/s0009_palindrome_number/readme.md b/src/main/java/g0001_0100/s0009_palindrome_number/readme.md index 464312411..232c883cf 100644 --- a/src/main/java/g0001_0100/s0009_palindrome_number/readme.md +++ b/src/main/java/g0001_0100/s0009_palindrome_number/readme.md @@ -2,15 +2,15 @@ Easy -Given an integer `x`, return `true` if `x` is palindrome integer. - -An integer is a **palindrome** when it reads the same backward as forward. For example, `121` is palindrome while `123` is not. +Given an integer `x`, return `true` _if_ `x` _is a_ _**palindrome**__, and_ `false` _otherwise_. **Example 1:** **Input:** x = 121 -**Output:** true +**Output:** true + +**Explanation:** 121 reads as 121 from left to right and from right to left. **Example 2:** @@ -28,12 +28,6 @@ An integer is a **palindrome** when it reads the same backward as forward. For e **Explanation:** Reads 01 from right to left. Therefore it is not a palindrome. -**Example 4:** - -**Input:** x = -101 - -**Output:** false - **Constraints:** * -231 <= x <= 231 - 1 @@ -97,4 +91,4 @@ public class Solution { } ``` -This implementation provides a solution to the Palindrome Number problem in Java. \ No newline at end of file +This implementation provides a solution to the Palindrome Number problem in Java. diff --git a/src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md b/src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md index f38de36c6..2d99ab985 100644 --- a/src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md +++ b/src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md @@ -33,24 +33,10 @@ The matching should cover the **entire** input string (not partial). **Explanation:** ".\*" means "zero or more (\*) of any character (.)". -**Example 4:** - -**Input:** s = "aab", p = "c\*a\*b" - -**Output:** true - -**Explanation:** c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab". - -**Example 5:** - -**Input:** s = "mississippi", p = "mis\*is\*p\*." - -**Output:** false - **Constraints:** * `1 <= s.length <= 20` -* `1 <= p.length <= 30` +* `1 <= p.length <= 20` * `s` contains only lowercase English letters. * `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. @@ -116,4 +102,4 @@ public class Solution { } ``` -This implementation provides a solution to the Regular Expression Matching problem in Java. \ No newline at end of file +This implementation provides a solution to the Regular Expression Matching problem in Java. diff --git a/src/main/java/g0001_0100/s0011_container_with_most_water/readme.md b/src/main/java/g0001_0100/s0011_container_with_most_water/readme.md index 3f5f57efe..f22e18f7b 100644 --- a/src/main/java/g0001_0100/s0011_container_with_most_water/readme.md +++ b/src/main/java/g0001_0100/s0011_container_with_most_water/readme.md @@ -2,7 +2,11 @@ Medium -Given `n` non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). `n` vertical lines are drawn such that the two endpoints of the line `i` is at (i, ai) and `(i, 0)`. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. +You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the ith line are `(i, 0)` and `(i, height[i])`. + +Find two lines that together with the x-axis form a container, such that the container contains the most water. + +Return _the maximum amount of water a container can store_. **Notice** that you may not slant the container. @@ -22,23 +26,11 @@ Given `n` non-negative integers a1, a2, ..., an **Output:** 1 -**Example 3:** - -**Input:** height = [4,3,2,1,4] - -**Output:** 16 - -**Example 4:** - -**Input:** height = [1,2,1] - -**Output:** 2 - **Constraints:** * `n == height.length` * 2 <= n <= 105 -* 0 <= height[i] <= 104 +* 0 <= height[i] <= 104-105 <= nums[i] <= 105 To solve the 3Sum problem in Java using a `Solution` class, we'll follow these steps: @@ -106,4 +112,4 @@ public class Solution { } ``` -This implementation provides a solution to the 3Sum problem in Java. \ No newline at end of file +This implementation provides a solution to the 3Sum problem in Java. diff --git a/src/main/java/g0001_0100/s0017_letter_combinations_of_a_phone_number/readme.md b/src/main/java/g0001_0100/s0017_letter_combinations_of_a_phone_number/readme.md index ef64b4eaf..7525b8dff 100644 --- a/src/main/java/g0001_0100/s0017_letter_combinations_of_a_phone_number/readme.md +++ b/src/main/java/g0001_0100/s0017_letter_combinations_of_a_phone_number/readme.md @@ -4,9 +4,9 @@ Medium Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent. Return the answer in **any order**. -A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. +A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. -![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png) +![](https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png) **Example 1:** @@ -16,19 +16,13 @@ A mapping of digit to letters (just like on the telephone buttons) is given belo **Example 2:** -**Input:** digits = "" - -**Output:** [] - -**Example 3:** - **Input:** digits = "2" **Output:** ["a","b","c"] **Constraints:** -* `0 <= digits.length <= 4` +* `1 <= digits.length <= 4` * `digits[i]` is a digit in the range `['2', '9']`. To solve the Letter Combinations of a Phone Number problem in Java using a `Solution` class, we'll follow these steps: @@ -102,4 +96,4 @@ public class Solution { } ``` -This implementation provides a solution to the Letter Combinations of a Phone Number problem in Java. \ No newline at end of file +This implementation provides a solution to the Letter Combinations of a Phone Number problem in Java. diff --git a/src/main/java/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md b/src/main/java/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md index 0c5ecdd10..c9562bcbb 100644 --- a/src/main/java/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md +++ b/src/main/java/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md @@ -2,7 +2,7 @@ Medium -Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head. +Given the `head` of a linked list, remove the nth node from the end of the list and return its head. **Example 1:** @@ -122,4 +122,4 @@ public class Solution { } ``` -This implementation provides a solution to the Remove Nth Node From End of List problem in Java. \ No newline at end of file +This implementation provides a solution to the Remove Nth Node From End of List problem in Java. diff --git a/src/main/java/g0001_0100/s0020_valid_parentheses/readme.md b/src/main/java/g0001_0100/s0020_valid_parentheses/readme.md index e5f550055..e0ace3610 100644 --- a/src/main/java/g0001_0100/s0020_valid_parentheses/readme.md +++ b/src/main/java/g0001_0100/s0020_valid_parentheses/readme.md @@ -8,36 +8,37 @@ An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. +3. Every close bracket has a corresponding open bracket of the same type. **Example 1:** **Input:** s = "()" -**Output:** true +**Output:** true **Example 2:** **Input:** s = "()[]{}" -**Output:** true +**Output:** true **Example 3:** **Input:** s = "(]" -**Output:** false +**Output:** false **Example 4:** -**Input:** s = "([)]" +**Input:** s = "([])" -**Output:** false +**Output:** true **Example 5:** -**Input:** s = "{[]}" +**Input:** s = "([)]" -**Output:** true +**Output:** false **Constraints:** @@ -94,4 +95,4 @@ public class Solution { } ``` -This implementation provides a solution to the Valid Parentheses problem in Java using a stack data structure. \ No newline at end of file +This implementation provides a solution to the Valid Parentheses problem in Java using a stack data structure. diff --git a/src/main/java/g0001_0100/s0021_merge_two_sorted_lists/readme.md b/src/main/java/g0001_0100/s0021_merge_two_sorted_lists/readme.md index 591a61c30..699679b01 100644 --- a/src/main/java/g0001_0100/s0021_merge_two_sorted_lists/readme.md +++ b/src/main/java/g0001_0100/s0021_merge_two_sorted_lists/readme.md @@ -2,25 +2,29 @@ Easy -Merge two sorted linked lists and return it as a **sorted** list. The list should be made by splicing together the nodes of the first two lists. +You are given the heads of two sorted linked lists `list1` and `list2`. + +Merge the two lists into one **sorted** list. The list should be made by splicing together the nodes of the first two lists. + +Return _the head of the merged linked list_. **Example 1:** ![](https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg) -**Input:** l1 = [1,2,4], l2 = [1,3,4] +**Input:** list1 = [1,2,4], list2 = [1,3,4] **Output:** [1,1,2,3,4,4] **Example 2:** -**Input:** l1 = [], l2 = [] +**Input:** list1 = [], list2 = [] **Output:** [] **Example 3:** -**Input:** l1 = [], l2 = [0] +**Input:** list1 = [], list2 = [0] **Output:** [0] @@ -28,7 +32,7 @@ Merge two sorted linked lists and return it as a **sorted** list. The list shoul * The number of nodes in both lists is in the range `[0, 50]`. * `-100 <= Node.val <= 100` -* Both `l1` and `l2` are sorted in **non-decreasing** order. +* Both `list1` and `list2` are sorted in **non-decreasing** order. To solve the Merge Two Sorted Lists problem in Java with a `Solution` class, we'll implement a recursive approach. Here are the steps: @@ -95,4 +99,4 @@ public class Solution { } ``` -This implementation provides a solution to the Merge Two Sorted Lists problem in Java using a recursive approach. \ No newline at end of file +This implementation provides a solution to the Merge Two Sorted Lists problem in Java using a recursive approach. diff --git a/src/main/java/g0001_0100/s0022_generate_parentheses/readme.md b/src/main/java/g0001_0100/s0022_generate_parentheses/readme.md index 5c1679e59..82fb8c528 100644 --- a/src/main/java/g0001_0100/s0022_generate_parentheses/readme.md +++ b/src/main/java/g0001_0100/s0022_generate_parentheses/readme.md @@ -75,4 +75,4 @@ public class Solution { } ``` -This implementation provides a solution to the "Generate Parentheses" problem in Java using a backtracking approach. \ No newline at end of file +This implementation provides a solution to the "Generate Parentheses" problem in Java using a backtracking approach.