-
Notifications
You must be signed in to change notification settings - Fork 89
Added tasks 566, 567, 572 #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/g0501_0600/s0566_reshape_the_matrix/Solution.java
This file contains hidden or 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,26 @@ | ||
package g0501_0600.s0566_reshape_the_matrix; | ||
|
||
// #Easy #Array #Matrix #Simulation | ||
|
||
public class Solution { | ||
public int[][] matrixReshape(int[][] mat, int r, int c) { | ||
if ((mat.length * mat[0].length) != r * c) { | ||
return mat; | ||
} | ||
int p = 0; | ||
int[] flatArr = new int[mat.length * mat[0].length]; | ||
for (int[] ints : mat) { | ||
for (int anInt : ints) { | ||
flatArr[p++] = anInt; | ||
} | ||
} | ||
int[][] ansMat = new int[r][c]; | ||
int k = 0; | ||
for (int i = 0; i < ansMat.length; i++) { | ||
for (int j = 0; j < ansMat[i].length; j++) { | ||
ansMat[i][j] = flatArr[k++]; | ||
} | ||
} | ||
return ansMat; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/g0501_0600/s0566_reshape_the_matrix/readme.md
This file contains hidden or 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 @@ | ||
566\. Reshape the Matrix | ||
|
||
Easy | ||
|
||
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. | ||
|
||
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. | ||
|
||
The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were. | ||
|
||
If the `reshape` operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix. | ||
|
||
**Example 1:** | ||
|
||
 | ||
|
||
**Input:** mat = [[1,2],[3,4]], r = 1, c = 4 | ||
|
||
**Output:** [[1,2,3,4]] | ||
|
||
**Example 2:** | ||
|
||
 | ||
|
||
**Input:** mat = [[1,2],[3,4]], r = 2, c = 4 | ||
|
||
**Output:** [[1,2],[3,4]] | ||
|
||
**Constraints:** | ||
|
||
* `m == mat.length` | ||
* `n == mat[i].length` | ||
* `1 <= m, n <= 100` | ||
* `-1000 <= mat[i][j] <= 1000` | ||
* `1 <= r, c <= 300` |
41 changes: 41 additions & 0 deletions
41
src/main/java/g0501_0600/s0567_permutation_in_string/Solution.java
This file contains hidden or 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,41 @@ | ||
package g0501_0600.s0567_permutation_in_string; | ||
|
||
// #Medium #String #Hash_Table #Two_Pointers #Sliding_Window | ||
|
||
public class Solution { | ||
public boolean checkInclusion(String s1, String s2) { | ||
int n = s1.length(); | ||
int m = s2.length(); | ||
if (n > m) { | ||
return false; | ||
} | ||
int[] cntS1 = new int[26]; | ||
int[] cntS2 = new int[26]; | ||
for (int i = 0; i < n; i++) { | ||
cntS1[s1.charAt(i) - 'a']++; | ||
} | ||
for (int i = 0; i < n; i++) { | ||
cntS2[s2.charAt(i) - 'a']++; | ||
} | ||
if (check(cntS1, cntS2)) { | ||
return true; | ||
} | ||
for (int i = n; i < m; i++) { | ||
cntS2[s2.charAt(i - n) - 'a']--; | ||
cntS2[s2.charAt(i) - 'a']++; | ||
if (check(cntS1, cntS2)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean check(int[] cnt1, int[] cnt2) { | ||
for (int i = 0; i < 26; i++) { | ||
if (cnt1[i] != cnt2[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/g0501_0600/s0567_permutation_in_string/readme.md
This file contains hidden or 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,26 @@ | ||
567\. Permutation in String | ||
|
||
Medium | ||
|
||
Given two strings `s1` and `s2`, return `true` _if_ `s2` _contains a permutation of_ `s1`_, or_ `false` _otherwise_. | ||
|
||
In other words, return `true` if one of `s1`'s permutations is the substring of `s2`. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s1 = "ab", s2 = "eidbaooo" | ||
|
||
**Output:** true | ||
|
||
**Explanation:** s2 contains one permutation of s1 ("ba"). | ||
|
||
**Example 2:** | ||
|
||
**Input:** s1 = "ab", s2 = "eidboaoo" | ||
|
||
**Output:** false | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= s1.length, s2.length <= 10<sup>4</sup></code> | ||
* `s1` and `s2` consist of lowercase English letters. |
33 changes: 33 additions & 0 deletions
33
src/main/java/g0501_0600/s0572_subtree_of_another_tree/Solution.java
This file contains hidden or 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,33 @@ | ||
package g0501_0600.s0572_subtree_of_another_tree; | ||
|
||
// #Easy #Depth_First_Search #Tree #Binary_Tree #Hash_Function #String_Matching | ||
|
||
import com_github_leetcode.TreeNode; | ||
|
||
public class Solution { | ||
public boolean isSubtreeFound(TreeNode root, TreeNode subRoot) { | ||
if (root == null && subRoot == null) { | ||
return true; | ||
} | ||
if (root == null || subRoot == null) { | ||
return false; | ||
} | ||
if (root.val == subRoot.val) { | ||
return isSubtreeFound(root.left, subRoot.left) && isSubtree(root.right, subRoot.right); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean isSubtree(TreeNode root, TreeNode subRoot) { | ||
if (root == null && subRoot == null) { | ||
return true; | ||
} | ||
if (root == null || subRoot == null) { | ||
return false; | ||
} | ||
return isSubtreeFound(root, subRoot) | ||
|| isSubtree(root.left, subRoot) | ||
|| isSubtree(root.right, subRoot); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/g0501_0600/s0572_subtree_of_another_tree/readme.md
This file contains hidden or 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,30 @@ | ||
572\. Subtree of Another Tree | ||
|
||
Easy | ||
|
||
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. | ||
|
||
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. | ||
|
||
**Example 1:** | ||
|
||
 | ||
|
||
**Input:** root = [3,4,5,1,2], subRoot = [4,1,2] | ||
|
||
**Output:** true | ||
|
||
**Example 2:** | ||
|
||
 | ||
|
||
**Input:** root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2] | ||
|
||
**Output:** false | ||
|
||
**Constraints:** | ||
|
||
* The number of nodes in the `root` tree is in the range `[1, 2000]`. | ||
* The number of nodes in the `subRoot` tree is in the range `[1, 1000]`. | ||
* <code>-10<sup>4</sup> <= root.val <= 10<sup>4</sup></code> | ||
* <code>-10<sup>4</sup> <= subRoot.val <= 10<sup>4</sup></code> |
22 changes: 22 additions & 0 deletions
22
src/test/java/g0501_0600/s0566_reshape_the_matrix/SolutionTest.java
This file contains hidden or 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,22 @@ | ||
package g0501_0600.s0566_reshape_the_matrix; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SolutionTest { | ||
@Test | ||
void matrixReshape() { | ||
assertThat( | ||
new Solution().matrixReshape(new int[][] {{1, 2}, {3, 4}}, 1, 4), | ||
equalTo(new int[][] {{1, 2, 3, 4}})); | ||
} | ||
|
||
@Test | ||
void matrixReshape2() { | ||
assertThat( | ||
new Solution().matrixReshape(new int[][] {{1, 2}, {3, 4}}, 2, 4), | ||
equalTo(new int[][] {{1, 2}, {3, 4}})); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/g0501_0600/s0567_permutation_in_string/SolutionTest.java
This file contains hidden or 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,18 @@ | ||
package g0501_0600.s0567_permutation_in_string; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SolutionTest { | ||
@Test | ||
void checkInclusion() { | ||
assertThat(new Solution().checkInclusion("ab", "eidbaooo"), equalTo(true)); | ||
} | ||
|
||
@Test | ||
void checkInclusion2() { | ||
assertThat(new Solution().checkInclusion("ab", "eidboaoo"), equalTo(false)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/g0501_0600/s0572_subtree_of_another_tree/SolutionTest.java
This file contains hidden or 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,29 @@ | ||
package g0501_0600.s0572_subtree_of_another_tree; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import com_github_leetcode.TreeNode; | ||
import com_github_leetcode.TreeUtils; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class SolutionTest { | ||
@Test | ||
void isSubtreeFound() { | ||
TreeNode treeNode = | ||
TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(3, 4, 5, 1, 2))); | ||
TreeNode subTree = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(4, 1, 2))); | ||
assertThat(new Solution().isSubtreeFound(treeNode, subTree), equalTo(false)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
@Test | ||
void isSubtreeFound2() { | ||
TreeNode treeNode = | ||
TreeUtils.constructBinaryTree( | ||
new ArrayList<>(Arrays.asList(3, 4, 5, 1, 2, null, null, null, null, 0))); | ||
TreeNode subTree = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(4, 1, 2))); | ||
assertThat(new Solution().isSubtreeFound(treeNode, subTree), equalTo(false)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need
new ArrayList<>(
wrapper.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prepared a fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks