diff --git a/src/main/java/g0001_0100/s0031_next_permutation/readme.md b/src/main/java/g0001_0100/s0031_next_permutation/readme.md new file mode 100644 index 000000000..bca539d67 --- /dev/null +++ b/src/main/java/g0001_0100/s0031_next_permutation/readme.md @@ -0,0 +1,38 @@ +31\. Next Permutation + +Medium + +Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers. + +If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). + +The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory. + +**Example 1:** + +**Input:** nums = \[1,2,3\] + +**Output:** \[1,3,2\] + +**Example 2:** + +**Input:** nums = \[3,2,1\] + +**Output:** \[1,2,3\] + +**Example 3:** + +**Input:** nums = \[1,1,5\] + +**Output:** \[1,5,1\] + +**Example 4:** + +**Input:** nums = \[1\] + +**Output:** \[1\] + +**Constraints:** + +* `1 <= nums.length <= 100` +* `0 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0032_longest_valid_parentheses/readme.md b/src/main/java/g0001_0100/s0032_longest_valid_parentheses/readme.md new file mode 100644 index 000000000..8861dac66 --- /dev/null +++ b/src/main/java/g0001_0100/s0032_longest_valid_parentheses/readme.md @@ -0,0 +1,32 @@ +32\. Longest Valid Parentheses + +Hard + +Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring. + +**Example 1:** + +**Input:** s = "(()" + +**Output:** 2 + +**Explanation:** The longest valid parentheses substring is "()". + +**Example 2:** + +**Input:** s = ")()())" + +**Output:** 4 + +**Explanation:** The longest valid parentheses substring is "()()". + +**Example 3:** + +**Input:** s = "" + +**Output:** 0 + +**Constraints:** + +* `0 <= s.length <= 3 * 104` +* `s[i]` is `'('`, or `')'`. \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md b/src/main/java/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md new file mode 100644 index 000000000..92e27eaf8 --- /dev/null +++ b/src/main/java/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md @@ -0,0 +1,37 @@ +33\. Search in Rotated Sorted Array + +Medium + +There is an integer array `nums` sorted in ascending order (with **distinct** values). + +Prior to being passed to your function, `nums` is **possibly rotated** at an unknown pivot index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,5,6,7]` might be rotated at pivot index `3` and become `[4,5,6,7,0,1,2]`. + +Given the array `nums` **after** the possible rotation and an integer `target`, return _the index of_ `target` _if it is in_ `nums`_, or_ `-1` _if it is not in_ `nums`. + +You must write an algorithm with `O(log n)` runtime complexity. + +**Example 1:** + +**Input:** nums = \[4,5,6,7,0,1,2\], target = 0 + +**Output:** 4 + +**Example 2:** + +**Input:** nums = \[4,5,6,7,0,1,2\], target = 3 + +**Output:** -1 + +**Example 3:** + +**Input:** nums = \[1\], target = 0 + +**Output:** -1 + +**Constraints:** + +* `1 <= nums.length <= 5000` +* `-104 <= nums[i] <= 104` +* All values of `nums` are **unique**. +* `nums` is an ascending array that is possibly rotated. +* `-104 <= target <= 104` \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md b/src/main/java/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md new file mode 100644 index 000000000..07deb3767 --- /dev/null +++ b/src/main/java/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md @@ -0,0 +1,34 @@ +34\. Find First and Last Position of Element in Sorted Array + +Medium + +Given an array of integers `nums` sorted in non-decreasing order, find the starting and ending position of a given `target` value. + +If `target` is not found in the array, return `[-1, -1]`. + +You must write an algorithm with `O(log n)` runtime complexity. + +**Example 1:** + +**Input:** nums = \[5,7,7,8,8,10\], target = 8 + +**Output:** \[3,4\] + +**Example 2:** + +**Input:** nums = \[5,7,7,8,8,10\], target = 6 + +**Output:** \[-1,-1\] + +**Example 3:** + +**Input:** nums = \[\], target = 0 + +**Output:** \[-1,-1\] + +**Constraints:** + +* `0 <= nums.length <= 105` +* `-109 <= nums[i] <= 109` +* `nums` is a non-decreasing array. +* `-109 <= target <= 109` \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0035_search_insert_position/readme.md b/src/main/java/g0001_0100/s0035_search_insert_position/readme.md new file mode 100644 index 000000000..daf79666f --- /dev/null +++ b/src/main/java/g0001_0100/s0035_search_insert_position/readme.md @@ -0,0 +1,44 @@ +35\. Search Insert Position + +Easy + +Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. + +You must write an algorithm with `O(log n)` runtime complexity. + +**Example 1:** + +**Input:** nums = \[1,3,5,6\], target = 5 + +**Output:** 2 + +**Example 2:** + +**Input:** nums = \[1,3,5,6\], target = 2 + +**Output:** 1 + +**Example 3:** + +**Input:** nums = \[1,3,5,6\], target = 7 + +**Output:** 4 + +**Example 4:** + +**Input:** nums = \[1,3,5,6\], target = 0 + +**Output:** 0 + +**Example 5:** + +**Input:** nums = \[1\], target = 0 + +**Output:** 0 + +**Constraints:** + +* `1 <= nums.length <= 104` +* `-104 <= nums[i] <= 104` +* `nums` contains **distinct** values sorted in **ascending** order. +* `-104 <= target <= 104` \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0036_valid_sudoku/readme.md b/src/main/java/g0001_0100/s0036_valid_sudoku/readme.md new file mode 100644 index 000000000..2098acf48 --- /dev/null +++ b/src/main/java/g0001_0100/s0036_valid_sudoku/readme.md @@ -0,0 +1,58 @@ +36\. Valid Sudoku + +Medium + +Determine if a `9 x 9` Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**: + +1. Each row must contain the digits `1-9` without repetition. +2. Each column must contain the digits `1-9` without repetition. +3. Each of the nine `3 x 3` sub-boxes of the grid must contain the digits `1-9` without repetition. + +**Note:** + +* A Sudoku board (partially filled) could be valid but is not necessarily solvable. +* Only the filled cells need to be validated according to the mentioned rules. + +**Example 1:** + + + +**Input:** + + board = + [["5","3",".",".","7",".",".",".","."] + ,["6",".",".","1","9","5",".",".","."] + ,[".","9","8",".",".",".",".","6","."] + ,["8",".",".",".","6",".",".",".","3"] + ,["4",".",".","8",".","3",".",".","1"] + ,["7",".",".",".","2",".",".",".","6"] + ,[".","6",".",".",".",".","2","8","."] + ,[".",".",".","4","1","9",".",".","5"] + ,[".",".",".",".","8",".",".","7","9"]] + +**Output:** true + +**Example 2:** + +**Input:** + + board = + [["8","3",".",".","7",".",".",".","."] + ,["6",".",".","1","9","5",".",".","."] + ,[".","9","8",".",".",".",".","6","."] + ,["8",".",".",".","6",".",".",".","3"] + ,["4",".",".","8",".","3",".",".","1"] + ,["7",".",".",".","2",".",".",".","6"] + ,[".","6",".",".",".",".","2","8","."] + ,[".",".",".","4","1","9",".",".","5"] + ,[".",".",".",".","8",".",".","7","9"]] + +**Output:** false + +**Explanation:** Same as Example 1, except with the **5** in the top left corner being modified to **8**. Since there are two 8's in the top left 3x3 sub-box, it is invalid. + +**Constraints:** + +* `board.length == 9` +* `board[i].length == 9` +* `board[i][j]` is a digit `1-9` or `'.'`. \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0037_sudoku_solver/readme.md b/src/main/java/g0001_0100/s0037_sudoku_solver/readme.md new file mode 100644 index 000000000..ca744931b --- /dev/null +++ b/src/main/java/g0001_0100/s0037_sudoku_solver/readme.md @@ -0,0 +1,52 @@ +37\. Sudoku Solver + +Hard + +Write a program to solve a Sudoku puzzle by filling the empty cells. + +A sudoku solution must satisfy **all of the following rules**: + +1. Each of the digits `1-9` must occur exactly once in each row. +2. Each of the digits `1-9` must occur exactly once in each column. +3. Each of the digits `1-9` must occur exactly once in each of the 9 `3x3` sub-boxes of the grid. + +The `'.'` character indicates empty cells. + +**Example 1:** + + + +**Input:** + + board = [["5","3",".",".","7",".",".",".","."], + ["6",".",".","1","9","5",".",".","."], + [".","9","8",".",".",".",".","6","."], + ["8",".",".",".","6",".",".",".","3"], + ["4",".",".","8",".","3",".",".","1"], + ["7",".",".",".","2",".",".",".","6"], + [".","6",".",".",".",".","2","8","."], + [".",".",".","4","1","9",".",".","5"], + [".",".",".",".","8",".",".","7","9"]] + +**Output:** + + [["5","3","4","6","7","8","9","1","2"], + ["6","7","2","1","9","5","3","4","8"], + ["1","9","8","3","4","2","5","6","7"], + ["8","5","9","7","6","1","4","2","3"], + ["4","2","6","8","5","3","7","9","1"], + ["7","1","3","9","2","4","8","5","6"], + ["9","6","1","5","3","7","2","8","4"], + ["2","8","7","4","1","9","6","3","5"], + ["3","4","5","2","8","6","1","7","9"]] + +**Explanation:** The input board is shown above and the only valid solution is shown below: + + + +**Constraints:** + +* `board.length == 9` +* `board[i].length == 9` +* `board[i][j]` is a digit or `'.'`. +* It is **guaranteed** that the input board has only one solution. \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0038_count_and_say/readme.md b/src/main/java/g0001_0100/s0038_count_and_say/readme.md new file mode 100644 index 000000000..53386af59 --- /dev/null +++ b/src/main/java/g0001_0100/s0038_count_and_say/readme.md @@ -0,0 +1,41 @@ +38\. Count and Say + +Medium + +The **count-and-say** sequence is a sequence of digit strings defined by the recursive formula: + +* `countAndSay(1) = "1"` +* `countAndSay(n)` is the way you would "say" the digit string from `countAndSay(n-1)`, which is then converted into a different digit string. + +To determine how you "say" a digit string, split it into the **minimal** number of groups so that each group is a contiguous section all of the **same character.** Then for each group, say the number of characters, then say the character. To convert the saying into a digit string, replace the counts with a number and concatenate every saying. + +For example, the saying and conversion for digit string `"3322251"`: + + + +Given a positive integer `n`, return _the_ `nth` _term of the **count-and-say** sequence_. + +**Example 1:** + +**Input:** n = 1 + +**Output:** "1" + +**Explanation:** This is the base case. + +**Example 2:** + +**Input:** n = 4 + +**Output:** "1211" + +**Explanation:** + + countAndSay(1) = "1" + countAndSay(2) = say "1" = one 1 = "11" + countAndSay(3) = say "11" = two 1's = "21" + countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211" + +**Constraints:** + +* `1 <= n <= 30` \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0039_combination_sum/readme.md b/src/main/java/g0001_0100/s0039_combination_sum/readme.md new file mode 100644 index 000000000..64da13032 --- /dev/null +++ b/src/main/java/g0001_0100/s0039_combination_sum/readme.md @@ -0,0 +1,52 @@ +39\. Combination Sum + +Medium + +Given an array of **distinct** integers `candidates` and a target integer `target`, return _a list of all **unique combinations** of_ `candidates` _where the chosen numbers sum to_ `target`_._ You may return the combinations in **any order**. + +The **same** number may be chosen from `candidates` an **unlimited number of times**. Two combinations are unique if the frequency of at least one of the chosen numbers is different. + +It is **guaranteed** that the number of unique combinations that sum up to `target` is less than `150` combinations for the given input. + +**Example 1:** + +**Input:** candidates = \[2,3,6,7\], target = 7 + +**Output:** \[\[2,2,3\],\[7\]\] + +**Explanation:** + + 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. + 7 is a candidate, and 7 = 7. + These are the only two combinations. + +**Example 2:** + +**Input:** candidates = \[2,3,5\], target = 8 + +**Output:** \[\[2,2,2,2\],\[2,3,3\],\[3,5\]\] + +**Example 3:** + +**Input:** candidates = \[2\], target = 1 + +**Output:** \[\] + +**Example 4:** + +**Input:** candidates = \[1\], target = 1 + +**Output:** \[\[1\]\] + +**Example 5:** + +**Input:** candidates = \[1\], target = 2 + +**Output:** \[\[1,1\]\] + +**Constraints:** + +* `1 <= candidates.length <= 30` +* `1 <= candidates[i] <= 200` +* All elements of `candidates` are **distinct**. +* `1 <= target <= 500` \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0040_combination_sum_ii/readme.md b/src/main/java/g0001_0100/s0040_combination_sum_ii/readme.md new file mode 100644 index 000000000..974e1d907 --- /dev/null +++ b/src/main/java/g0001_0100/s0040_combination_sum_ii/readme.md @@ -0,0 +1,39 @@ +40\. Combination Sum II + +Medium + +Given a collection of candidate numbers (`candidates`) and a target number (`target`), find all unique combinations in `candidates` where the candidate numbers sum to `target`. + +Each number in `candidates` may only be used **once** in the combination. + +**Note:** The solution set must not contain duplicate combinations. + +**Example 1:** + +**Input:** candidates = \[10,1,2,7,6,1,5\], target = 8 + +**Output:** + + [ + [1,1,6], + [1,2,5], + [1,7], + [2,6] + ] + +**Example 2:** + +**Input:** candidates = \[2,5,2,1,2\], target = 5 + +**Output:** + + [ + [1,2,2], + [5] + ] + +**Constraints:** + +* `1 <= candidates.length <= 100` +* `1 <= candidates[i] <= 50` +* `1 <= target <= 30` \ No newline at end of file