-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
703 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
1\. Two Sum | ||
|
||
Easy | ||
|
||
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_. | ||
|
||
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice. | ||
|
||
You can return the answer in any order. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [2,7,11,15], target = 9 | ||
|
||
**Output:** [0,1] | ||
|
||
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1]. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [3,2,4], target = 6 | ||
|
||
**Output:** [1,2] | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [3,3], target = 6 | ||
|
||
**Output:** [0,1] | ||
|
||
**Constraints:** | ||
|
||
* <code>2 <= nums.length <= 10<sup>4</sup></code> | ||
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code> | ||
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code> | ||
* **Only one valid answer exists.** | ||
|
||
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity? |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/g0001_0100/s0002_add_two_numbers/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
2\. Add Two Numbers | ||
|
||
Medium | ||
|
||
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. | ||
|
||
You may assume the two numbers do not contain any leading zero, except the number 0 itself. | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg) | ||
|
||
**Input:** l1 = [2,4,3], l2 = [5,6,4] | ||
|
||
**Output:** [7,0,8] | ||
|
||
**Explanation:** 342 + 465 = 807. | ||
|
||
**Example 2:** | ||
|
||
**Input:** l1 = [0], l2 = [0] | ||
|
||
**Output:** [0] | ||
|
||
**Example 3:** | ||
|
||
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] | ||
|
||
**Output:** [8,9,9,9,0,0,0,1] | ||
|
||
**Constraints:** | ||
|
||
* The number of nodes in each linked list is in the range `[1, 100]`. | ||
* `0 <= Node.val <= 9` | ||
* It is guaranteed that the list represents a number that does not have leading zeros. |
40 changes: 40 additions & 0 deletions
40
...otlin/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
3\. Longest Substring Without Repeating Characters | ||
|
||
Medium | ||
|
||
Given a string `s`, find the length of the **longest substring** without repeating characters. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "abcabcbb" | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** The answer is "abc", with the length of 3. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "bbbbb" | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** The answer is "b", with the length of 1. | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "pwwkew" | ||
|
||
**Output:** 3 | ||
|
||
**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:** | ||
|
||
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code> | ||
* `s` consists of English letters, digits, symbols and spaces. |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
4\. Median of Two Sorted Arrays | ||
|
||
Hard | ||
|
||
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays. | ||
|
||
The overall run time complexity should be `O(log (m+n))`. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums1 = [1,3], nums2 = [2] | ||
|
||
**Output:** 2.00000 | ||
|
||
**Explanation:** merged array = [1,2,3] and median is 2. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums1 = [1,2], nums2 = [3,4] | ||
|
||
**Output:** 2.50000 | ||
|
||
**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` | ||
* `nums2.length == n` | ||
* `0 <= m <= 1000` | ||
* `0 <= n <= 1000` | ||
* `1 <= m + n <= 2000` | ||
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code> |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
5\. Longest Palindromic Substring | ||
|
||
Medium | ||
|
||
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. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "cbbd" | ||
|
||
**Output:** "bb" | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "a" | ||
|
||
**Output:** "a" | ||
|
||
**Example 4:** | ||
|
||
**Input:** s = "ac" | ||
|
||
**Output:** "a" | ||
|
||
**Constraints:** | ||
|
||
* `1 <= s.length <= 1000` | ||
* `s` consist of only digits and English letters. |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/g0001_0100/s0006_zigzag_conversion/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
6\. Zigzag Conversion | ||
|
||
Medium | ||
|
||
The string `"PAYPALISHIRING"` is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) | ||
|
||
P A H N A P L S I I G Y I R | ||
|
||
And then read line by line: `"PAHNAPLSIIGYIR"` | ||
|
||
Write the code that will take a string and make this conversion given a number of rows: | ||
|
||
string convert(string s, int numRows); | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "PAYPALISHIRING", numRows = 3 | ||
|
||
**Output:** "PAHNAPLSIIGYIR" | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "PAYPALISHIRING", numRows = 4 | ||
|
||
**Output:** "PINALSIGYAHRPI" | ||
|
||
**Explanation:** P I N A L S I G Y A H R P I | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "A", numRows = 1 | ||
|
||
**Output:** "A" | ||
|
||
**Constraints:** | ||
|
||
* `1 <= s.length <= 1000` | ||
* `s` consists of English letters (lower-case and upper-case), `','` and `'.'`. | ||
* `1 <= numRows <= 1000` |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/g0001_0100/s0007_reverse_integer/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
7\. Reverse Integer | ||
|
||
Medium | ||
|
||
Given a signed 32-bit integer `x`, return `x` _with its digits reversed_. If reversing `x` causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return `0`. | ||
|
||
**Assume the environment does not allow you to store 64-bit integers (signed or unsigned).** | ||
|
||
**Example 1:** | ||
|
||
**Input:** x = 123 | ||
|
||
**Output:** 321 | ||
|
||
**Example 2:** | ||
|
||
**Input:** x = -123 | ||
|
||
**Output:** -321 | ||
|
||
**Example 3:** | ||
|
||
**Input:** x = 120 | ||
|
||
**Output:** 21 | ||
|
||
**Example 4:** | ||
|
||
**Input:** x = 0 | ||
|
||
**Output:** 0 | ||
|
||
**Constraints:** | ||
|
||
* <code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code> |
113 changes: 113 additions & 0 deletions
113
src/main/kotlin/g0001_0100/s0008_string_to_integer_atoi/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
8\. String to Integer (atoi) | ||
|
||
Medium | ||
|
||
Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer (similar to C/C++'s `atoi` function). | ||
|
||
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 <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then clamp the integer so that it remains in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be clamped to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be clamped to <code>2<sup>31</sup> - 1</code>. | ||
6. Return the integer as the final result. | ||
|
||
**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. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "42" | ||
|
||
**Output:** 42 | ||
|
||
**Explanation:** The underlined characters are what is read in, 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 [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 42. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = " -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 [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is -42. | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "4193 with words" | ||
|
||
**Output:** 4193 | ||
|
||
**Explanation:** | ||
|
||
Step 1: "4193 with words" (no characters read because there is no leading whitespace) | ||
^ | ||
Step 2: "4193 with words" (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) | ||
^ | ||
The parsed integer is 4193. | ||
|
||
Since 4193 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 4193. | ||
|
||
**Example 4:** | ||
|
||
**Input:** s = "words and 987" | ||
|
||
**Output:** 0 | ||
|
||
**Explanation:** | ||
|
||
Step 1: "words and 987" (no characters read because there is no leading whitespace) | ||
^ | ||
Step 2: "words and 987" (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 [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 0. | ||
|
||
**Example 5:** | ||
|
||
**Input:** s = "-91283472332" | ||
|
||
**Output:** -2147483648 | ||
|
||
**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 [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is clamped to -2<sup>31</sup> = -2147483648. | ||
|
||
**Constraints:** | ||
|
||
* `0 <= s.length <= 200` | ||
* `s` consists of English letters (lower-case and upper-case), digits (`0-9`), `' '`, `'+'`, `'-'`, and `'.'`. |
Oops, something went wrong.