Skip to content

Commit 705b877

Browse files
authored
Improved tasks 27, 28, 36
1 parent e060cec commit 705b877

File tree

3 files changed

+99
-51
lines changed
  • LeetCodeNet/G0001_0100

3 files changed

+99
-51
lines changed

LeetCodeNet/G0001_0100/S0027_remove_element/readme.md

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,54 @@
1-
# 27. Remove Element
1+
27\. Remove Element
22

3-
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` in-place. The order of the elements may be changed. Then return the number of elements in `nums` which are not equal to `val`.
3+
Easy
44

5-
Consider the number of elements in `nums` which are not equal to `val` be `k`, to get accepted, you need to do the following things:
5+
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm). The relative order of the elements may be changed.
66

7-
- Change the array `nums` such that the first `k` elements of `nums` contain the elements which are not equal to `val`. The remaining elements of `nums` are not important as well as the size of `nums`.
8-
- Return `k`.
7+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
98

10-
## Example 1:
9+
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
10+
11+
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
12+
13+
**Custom Judge:**
14+
15+
The judge will test your solution with the following code:
16+
17+
int[] nums = [...]; // Input array
18+
19+
int val = ...; // Value to remove
20+
21+
int[] expectedNums = [...]; // The expected answer with correct length.
22+
// It is sorted with no values equaling val.
23+
24+
int k = removeElement(nums, val); // Calls your implementation
25+
26+
assert k == expectedNums.length;
27+
28+
sort(nums, 0, k); // Sort the first k elements of nums
29+
30+
for (int i = 0; i < actualLength; i++){
31+
32+
assert nums[i] == expectedNums[i];
33+
34+
}
35+
36+
If all assertions pass, then your solution will be **accepted**.
37+
38+
**Example 1:**
39+
40+
**Input:** nums = [3,2,2,3], val = 3
41+
42+
**Output:** 2, nums = [2,2,\_,\_]
1143

12-
**Input:** nums = `[3,2,2,3]`, val = 3
13-
**Output:** 2, nums = `[2,2,_,_]`
1444
**Explanation:** Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the returned k (hence they are underscores).
1545

16-
## Example 2:
46+
**Example 2:**
47+
48+
**Input:** nums = [0,1,2,2,3,0,4,2], val = 2
49+
50+
**Output:** 5, nums = [0,1,4,0,3,\_,\_,\_]
1751

18-
**Input:** nums = `[0,1,2,2,3,0,4,2]`, val = 2
19-
**Output:** 5, nums = `[0,1,4,0,3,_,_,_]`
2052
**Explanation:** Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned k (hence they are underscores).
2153

2254
**Constraints:**
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
# 28. Find the Index of the First Occurrence in a String
1+
28\. Find the Index of the First Occurrence in a String
2+
3+
Medium
24

35
Given two strings `needle` and `haystack`, return the index of the first occurrence of `needle` in `haystack`, or `-1` if `needle` is not part of `haystack`.
46

5-
## Example 1:
7+
**Example 1:**
68

79
**Input:** haystack = "sadbutsad", needle = "sad"
10+
811
**Output:** 0
12+
913
**Explanation:** "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0.
1014

11-
## Example 2:
15+
**Example 2:**
1216

1317
**Input:** haystack = "leetcode", needle = "leeto"
14-
**Output:** -1
15-
**Explanation:** "leeto" did not occur in "leetcode", so we return -1.
16-
17-
**Clarification:**
1818

19-
What should we return when `needle` is an empty string? This is a great question to ask during an interview.
19+
**Output:** -1
2020

21-
For the purpose of this problem, we will return 0 when `needle` is an empty string. This is consistent to C's [strstr()](http://www.cplusplus.com/reference/cstring/strstr/) and Java's [indexOf()](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)).
21+
**Explanation:** "leeto" did not occur in "leetcode", so we return -1.
2222

2323
**Constraints:**
2424

25-
* <code>0 <= haystack.length, needle.length <= 5 * 10<sup>4</sup></code>
26-
* `haystack` and `needle` consist of only lower-case English characters.
25+
* <code>1 <= haystack.length, needle.length <= 10<sup>4</sup></code>
26+
* `haystack` and `needle` consist of only lowercase English characters.

LeetCodeNet/G0001_0100/S0036_valid_sudoku/readme.md

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# 36. Valid Sudoku
1+
36\. Valid Sudoku
22

3-
## Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
3+
Medium
44

5-
1. Each row must contain the digits 1-9 without repetition.
6-
2. Each column must contain the digits 1-9 without repetition.
7-
3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
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.
810

911
**Note:**
1012

@@ -15,39 +17,53 @@
1517

1618
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
1719

18-
**Input:**
20+
**Input:** board =
21+
22+
[["5","3",".",".","7",".",".",".","."] ,
23+
24+
["6",".",".","1","9","5",".",".","."] ,
25+
26+
[".","9","8",".",".",".",".","6","."] ,
27+
28+
["8",".",".",".","6",".",".",".","3"] ,
29+
30+
["4",".",".","8",".","3",".",".","1"] ,
31+
32+
["7",".",".",".","2",".",".",".","6"] ,
33+
34+
[".","6",".",".",".",".","2","8","."] ,
35+
36+
[".",".",".","4","1","9",".",".","5"] ,
1937

20-
board =
21-
[["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"]]
38+
[".",".",".",".","8",".",".","7","9"]]
3039

31-
**Output:** true
40+
**Output:** true
3241

3342
**Example 2:**
3443

35-
**Input:**
44+
**Input:** board =
45+
46+
[["8","3",".",".","7",".",".",".","."] ,
47+
48+
["6",".",".","1","9","5",".",".","."] ,
49+
50+
[".","9","8",".",".",".",".","6","."] ,
51+
52+
["8",".",".",".","6",".",".",".","3"] ,
53+
54+
["4",".",".","8",".","3",".",".","1"] ,
55+
56+
["7",".",".",".","2",".",".",".","6"] ,
57+
58+
[".","6",".",".",".",".","2","8","."] ,
59+
60+
[".",".",".","4","1","9",".",".","5"] ,
3661

37-
board =
38-
[["8","3",".",".","7",".",".",".","."]
39-
,["6",".",".","1","9","5",".",".","."]
40-
,[".","9","8",".",".",".",".","6","."]
41-
,["8",".",".",".","6",".",".",".","3"]
42-
,["4",".",".","8",".","3",".",".","1"]
43-
,["7",".",".",".","2",".",".",".","6"]
44-
,[".","6",".",".",".",".","2","8","."]
45-
,[".",".",".","4","1","9",".",".","5"]
46-
,[".",".",".",".","8",".",".","7","9"]]
62+
[".",".",".",".","8",".",".","7","9"]]
4763

4864
**Output:** false
4965

50-
**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.
66+
**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.
5167

5268
**Constraints:**
5369

0 commit comments

Comments
 (0)