Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d439d4

Browse files
authored
Added descriptions 31-40.
1 parent 3dfe235 commit 0d439d4

File tree

10 files changed

+427
-0
lines changed

10 files changed

+427
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
31\. Next Permutation
2+
3+
Medium
4+
5+
Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers.
6+
7+
If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).
8+
9+
The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = \[1,2,3\]
14+
15+
**Output:** \[1,3,2\]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = \[3,2,1\]
20+
21+
**Output:** \[1,2,3\]
22+
23+
**Example 3:**
24+
25+
**Input:** nums = \[1,1,5\]
26+
27+
**Output:** \[1,5,1\]
28+
29+
**Example 4:**
30+
31+
**Input:** nums = \[1\]
32+
33+
**Output:** \[1\]
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 100`
38+
* `0 <= nums[i] <= 100`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
32\. Longest Valid Parentheses
2+
3+
Hard
4+
5+
Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "(()"
10+
11+
**Output:** 2
12+
13+
**Explanation:** The longest valid parentheses substring is "()".
14+
15+
**Example 2:**
16+
17+
**Input:** s = ")()())"
18+
19+
**Output:** 4
20+
21+
**Explanation:** The longest valid parentheses substring is "()()".
22+
23+
**Example 3:**
24+
25+
**Input:** s = ""
26+
27+
**Output:** 0
28+
29+
**Constraints:**
30+
31+
* `0 <= s.length <= 3 * 104`
32+
* `s[i]` is `'('`, or `')'`.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
33\. Search in Rotated Sorted Array
2+
3+
Medium
4+
5+
There is an integer array `nums` sorted in ascending order (with **distinct** values).
6+
7+
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]`.
8+
9+
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`.
10+
11+
You must write an algorithm with `O(log n)` runtime complexity.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = \[4,5,6,7,0,1,2\], target = 0
16+
17+
**Output:** 4
18+
19+
**Example 2:**
20+
21+
**Input:** nums = \[4,5,6,7,0,1,2\], target = 3
22+
23+
**Output:** -1
24+
25+
**Example 3:**
26+
27+
**Input:** nums = \[1\], target = 0
28+
29+
**Output:** -1
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 5000`
34+
* `-104 <= nums[i] <= 104`
35+
* All values of `nums` are **unique**.
36+
* `nums` is an ascending array that is possibly rotated.
37+
* `-104 <= target <= 104`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
34\. Find First and Last Position of Element in Sorted Array
2+
3+
Medium
4+
5+
Given an array of integers `nums` sorted in non-decreasing order, find the starting and ending position of a given `target` value.
6+
7+
If `target` is not found in the array, return `[-1, -1]`.
8+
9+
You must write an algorithm with `O(log n)` runtime complexity.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = \[5,7,7,8,8,10\], target = 8
14+
15+
**Output:** \[3,4\]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = \[5,7,7,8,8,10\], target = 6
20+
21+
**Output:** \[-1,-1\]
22+
23+
**Example 3:**
24+
25+
**Input:** nums = \[\], target = 0
26+
27+
**Output:** \[-1,-1\]
28+
29+
**Constraints:**
30+
31+
* `0 <= nums.length <= 105`
32+
* `-109 <= nums[i] <= 109`
33+
* `nums` is a non-decreasing array.
34+
* `-109 <= target <= 109`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
35\. Search Insert Position
2+
3+
Easy
4+
5+
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.
6+
7+
You must write an algorithm with `O(log n)` runtime complexity.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = \[1,3,5,6\], target = 5
12+
13+
**Output:** 2
14+
15+
**Example 2:**
16+
17+
**Input:** nums = \[1,3,5,6\], target = 2
18+
19+
**Output:** 1
20+
21+
**Example 3:**
22+
23+
**Input:** nums = \[1,3,5,6\], target = 7
24+
25+
**Output:** 4
26+
27+
**Example 4:**
28+
29+
**Input:** nums = \[1,3,5,6\], target = 0
30+
31+
**Output:** 0
32+
33+
**Example 5:**
34+
35+
**Input:** nums = \[1\], target = 0
36+
37+
**Output:** 0
38+
39+
**Constraints:**
40+
41+
* `1 <= nums.length <= 104`
42+
* `-104 <= nums[i] <= 104`
43+
* `nums` contains **distinct** values sorted in **ascending** order.
44+
* `-104 <= target <= 104`
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
36\. Valid Sudoku
2+
3+
Medium
4+
5+
Determine if a `9 x 9` Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**:
6+
7+
1. Each row must contain the digits `1-9` without repetition.
8+
2. Each column must contain the digits `1-9` without repetition.
9+
3. Each of the nine `3 x 3` sub-boxes of the grid must contain the digits `1-9` without repetition.
10+
11+
**Note:**
12+
13+
* A Sudoku board (partially filled) could be valid but is not necessarily solvable.
14+
* Only the filled cells need to be validated according to the mentioned rules.
15+
16+
**Example 1:**
17+
18+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
19+
20+
**Input:**
21+
22+
board =
23+
[["5","3",".",".","7",".",".",".","."]
24+
,["6",".",".","1","9","5",".",".","."]
25+
,[".","9","8",".",".",".",".","6","."]
26+
,["8",".",".",".","6",".",".",".","3"]
27+
,["4",".",".","8",".","3",".",".","1"]
28+
,["7",".",".",".","2",".",".",".","6"]
29+
,[".","6",".",".",".",".","2","8","."]
30+
,[".",".",".","4","1","9",".",".","5"]
31+
,[".",".",".",".","8",".",".","7","9"]]
32+
33+
**Output:** true
34+
35+
**Example 2:**
36+
37+
**Input:**
38+
39+
board =
40+
[["8","3",".",".","7",".",".",".","."]
41+
,["6",".",".","1","9","5",".",".","."]
42+
,[".","9","8",".",".",".",".","6","."]
43+
,["8",".",".",".","6",".",".",".","3"]
44+
,["4",".",".","8",".","3",".",".","1"]
45+
,["7",".",".",".","2",".",".",".","6"]
46+
,[".","6",".",".",".",".","2","8","."]
47+
,[".",".",".","4","1","9",".",".","5"]
48+
,[".",".",".",".","8",".",".","7","9"]]
49+
50+
**Output:** false
51+
52+
**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.
53+
54+
**Constraints:**
55+
56+
* `board.length == 9`
57+
* `board[i].length == 9`
58+
* `board[i][j]` is a digit `1-9` or `'.'`.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
37\. Sudoku Solver
2+
3+
Hard
4+
5+
Write a program to solve a Sudoku puzzle by filling the empty cells.
6+
7+
A sudoku solution must satisfy **all of the following rules**:
8+
9+
1. Each of the digits `1-9` must occur exactly once in each row.
10+
2. Each of the digits `1-9` must occur exactly once in each column.
11+
3. Each of the digits `1-9` must occur exactly once in each of the 9 `3x3` sub-boxes of the grid.
12+
13+
The `'.'` character indicates empty cells.
14+
15+
**Example 1:**
16+
17+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
18+
19+
**Input:**
20+
21+
board = [["5","3",".",".","7",".",".",".","."],
22+
["6",".",".","1","9","5",".",".","."],
23+
[".","9","8",".",".",".",".","6","."],
24+
["8",".",".",".","6",".",".",".","3"],
25+
["4",".",".","8",".","3",".",".","1"],
26+
["7",".",".",".","2",".",".",".","6"],
27+
[".","6",".",".",".",".","2","8","."],
28+
[".",".",".","4","1","9",".",".","5"],
29+
[".",".",".",".","8",".",".","7","9"]]
30+
31+
**Output:**
32+
33+
[["5","3","4","6","7","8","9","1","2"],
34+
["6","7","2","1","9","5","3","4","8"],
35+
["1","9","8","3","4","2","5","6","7"],
36+
["8","5","9","7","6","1","4","2","3"],
37+
["4","2","6","8","5","3","7","9","1"],
38+
["7","1","3","9","2","4","8","5","6"],
39+
["9","6","1","5","3","7","2","8","4"],
40+
["2","8","7","4","1","9","6","3","5"],
41+
["3","4","5","2","8","6","1","7","9"]]
42+
43+
**Explanation:** The input board is shown above and the only valid solution is shown below:
44+
45+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png)
46+
47+
**Constraints:**
48+
49+
* `board.length == 9`
50+
* `board[i].length == 9`
51+
* `board[i][j]` is a digit or `'.'`.
52+
* It is **guaranteed** that the input board has only one solution.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
38\. Count and Say
2+
3+
Medium
4+
5+
The **count-and-say** sequence is a sequence of digit strings defined by the recursive formula:
6+
7+
* `countAndSay(1) = "1"`
8+
* `countAndSay(n)` is the way you would "say" the digit string from `countAndSay(n-1)`, which is then converted into a different digit string.
9+
10+
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.
11+
12+
For example, the saying and conversion for digit string `"3322251"`:
13+
14+
![](https://assets.leetcode.com/uploads/2020/10/23/countandsay.jpg)
15+
16+
Given a positive integer `n`, return _the_ `nth` _term of the **count-and-say** sequence_.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 1
21+
22+
**Output:** "1"
23+
24+
**Explanation:** This is the base case.
25+
26+
**Example 2:**
27+
28+
**Input:** n = 4
29+
30+
**Output:** "1211"
31+
32+
**Explanation:**
33+
34+
countAndSay(1) = "1"
35+
countAndSay(2) = say "1" = one 1 = "11"
36+
countAndSay(3) = say "11" = two 1's = "21"
37+
countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"
38+
39+
**Constraints:**
40+
41+
* `1 <= n <= 30`

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /