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 c0a937c

Browse files
Added tasks 566, 567, 572.
1 parent b3a89cb commit c0a937c

File tree

9 files changed

+260
-0
lines changed

9 files changed

+260
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0501_0600.s0566_reshape_the_matrix;
2+
3+
// #Easy #Array #Matrix #Simulation
4+
5+
public class Solution {
6+
public int[][] matrixReshape(int[][] mat, int r, int c) {
7+
if ((mat.length * mat[0].length) != r * c) {
8+
return mat;
9+
}
10+
int p = 0;
11+
int[] flatArr = new int[mat.length * mat[0].length];
12+
for (int[] ints : mat) {
13+
for (int anInt : ints) {
14+
flatArr[p++] = anInt;
15+
}
16+
}
17+
int[][] ansMat = new int[r][c];
18+
int k = 0;
19+
for (int i = 0; i < ansMat.length; i++) {
20+
for (int j = 0; j < ansMat[i].length; j++) {
21+
ansMat[i][j] = flatArr[k++];
22+
}
23+
}
24+
return ansMat;
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
566\. Reshape the Matrix
2+
3+
Easy
4+
5+
In MATLAB, there is a handy function called `reshape` which can reshape an `m x n` matrix into a new one with a different size `r x c` keeping its original data.
6+
7+
You are given an `m x n` matrix `mat` and two integers `r` and `c` representing the number of rows and the number of columns of the wanted reshaped matrix.
8+
9+
The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.
10+
11+
If the `reshape` operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/04/24/reshape1-grid.jpg)
16+
17+
**Input:** mat = [[1,2],[3,4]], r = 1, c = 4
18+
19+
**Output:** [[1,2,3,4]]
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/04/24/reshape2-grid.jpg)
24+
25+
**Input:** mat = [[1,2],[3,4]], r = 2, c = 4
26+
27+
**Output:** [[1,2],[3,4]]
28+
29+
**Constraints:**
30+
31+
* `m == mat.length`
32+
* `n == mat[i].length`
33+
* `1 <= m, n <= 100`
34+
* `-1000 <= mat[i][j] <= 1000`
35+
* `1 <= r, c <= 300`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0501_0600.s0567_permutation_in_string;
2+
3+
// #Medium #String #Hash_Table #Two_Pointers #Sliding_Window
4+
5+
public class Solution {
6+
public boolean checkInclusion(String s1, String s2) {
7+
int n = s1.length();
8+
int m = s2.length();
9+
if (n > m) {
10+
return false;
11+
}
12+
int[] cntS1 = new int[26];
13+
int[] cntS2 = new int[26];
14+
for (int i = 0; i < n; i++) {
15+
cntS1[s1.charAt(i) - 'a']++;
16+
}
17+
for (int i = 0; i < n; i++) {
18+
cntS2[s2.charAt(i) - 'a']++;
19+
}
20+
if (check(cntS1, cntS2)) {
21+
return true;
22+
}
23+
for (int i = n; i < m; i++) {
24+
cntS2[s2.charAt(i - n) - 'a']--;
25+
cntS2[s2.charAt(i) - 'a']++;
26+
if (check(cntS1, cntS2)) {
27+
return true;
28+
}
29+
}
30+
return false;
31+
}
32+
33+
private boolean check(int[] cnt1, int[] cnt2) {
34+
for (int i = 0; i < 26; i++) {
35+
if (cnt1[i] != cnt2[i]) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
567\. Permutation in String
2+
3+
Medium
4+
5+
Given two strings `s1` and `s2`, return `true` _if_ `s2` _contains a permutation of_ `s1`_, or_ `false` _otherwise_.
6+
7+
In other words, return `true` if one of `s1`'s permutations is the substring of `s2`.
8+
9+
**Example 1:**
10+
11+
**Input:** s1 = "ab", s2 = "eidbaooo"
12+
13+
**Output:** true
14+
15+
**Explanation:** s2 contains one permutation of s1 ("ba").
16+
17+
**Example 2:**
18+
19+
**Input:** s1 = "ab", s2 = "eidboaoo"
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* <code>1 <= s1.length, s2.length <= 10<sup>4</sup></code>
26+
* `s1` and `s2` consist of lowercase English letters.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0501_0600.s0572_subtree_of_another_tree;
2+
3+
// #Easy #Depth_First_Search #Tree #Binary_Tree #Hash_Function #String_Matching
4+
5+
import com_github_leetcode.TreeNode;
6+
7+
public class Solution {
8+
public boolean isSubtreeFound(TreeNode root, TreeNode subRoot) {
9+
if (root == null && subRoot == null) {
10+
return true;
11+
}
12+
if (root == null || subRoot == null) {
13+
return false;
14+
}
15+
if (root.val == subRoot.val) {
16+
return isSubtreeFound(root.left, subRoot.left) && isSubtree(root.right, subRoot.right);
17+
} else {
18+
return false;
19+
}
20+
}
21+
22+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
23+
if (root == null && subRoot == null) {
24+
return true;
25+
}
26+
if (root == null || subRoot == null) {
27+
return false;
28+
}
29+
return isSubtreeFound(root, subRoot)
30+
|| isSubtree(root.left, subRoot)
31+
|| isSubtree(root.right, subRoot);
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
572\. Subtree of Another Tree
2+
3+
Easy
4+
5+
Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.
6+
7+
A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this node's descendants. The tree `tree` could also be considered as a subtree of itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/28/subtree1-tree.jpg)
12+
13+
**Input:** root = [3,4,5,1,2], subRoot = [4,1,2]
14+
15+
**Output:** true
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/04/28/subtree2-tree.jpg)
20+
21+
**Input:** root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
22+
23+
**Output:** false
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the `root` tree is in the range `[1, 2000]`.
28+
* The number of nodes in the `subRoot` tree is in the range `[1, 1000]`.
29+
* <code>-10<sup>4</sup> <= root.val <= 10<sup>4</sup></code>
30+
* <code>-10<sup>4</sup> <= subRoot.val <= 10<sup>4</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0501_0600.s0566_reshape_the_matrix;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void matrixReshape() {
11+
assertThat(
12+
new Solution().matrixReshape(new int[][] {{1, 2}, {3, 4}}, 1, 4),
13+
equalTo(new int[][] {{1, 2, 3, 4}}));
14+
}
15+
16+
@Test
17+
void matrixReshape2() {
18+
assertThat(
19+
new Solution().matrixReshape(new int[][] {{1, 2}, {3, 4}}, 2, 4),
20+
equalTo(new int[][] {{1, 2}, {3, 4}}));
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0501_0600.s0567_permutation_in_string;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void checkInclusion() {
11+
assertThat(new Solution().checkInclusion("ab", "eidbaooo"), equalTo(true));
12+
}
13+
14+
@Test
15+
void checkInclusion2() {
16+
assertThat(new Solution().checkInclusion("ab", "eidboaoo"), equalTo(false));
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0501_0600.s0572_subtree_of_another_tree;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import com_github_leetcode.TreeUtils;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import org.junit.jupiter.api.Test;
11+
12+
class SolutionTest {
13+
@Test
14+
void isSubtreeFound() {
15+
TreeNode treeNode =
16+
TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(3, 4, 5, 1, 2)));
17+
TreeNode subTree = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(4, 1, 2)));
18+
assertThat(new Solution().isSubtreeFound(treeNode, subTree), equalTo(false));
19+
}
20+
21+
@Test
22+
void isSubtreeFound2() {
23+
TreeNode treeNode =
24+
TreeUtils.constructBinaryTree(
25+
new ArrayList<>(Arrays.asList(3, 4, 5, 1, 2, null, null, null, null, 0)));
26+
TreeNode subTree = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(4, 1, 2)));
27+
assertThat(new Solution().isSubtreeFound(treeNode, subTree), equalTo(false));
28+
}
29+
}

0 commit comments

Comments
(0)

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