Skip to content

Commit 8b6ec46

Browse files
add 867
1 parent 3684b68 commit 8b6ec46

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Your ideas/fixes/algorithms are more than welcome!
3434
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Easy|
3535
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
3636
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
37+
|867|[Transpose Matrix](https://leetcode.com/problems/transpose-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_867.java) | O(r*c) | O(r*c) | |Easy|
3738
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | O(n) | O(1) | |Easy|
3839
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
3940
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | O(n) | O(1) | |Easy|

Diff for: src/main/java/com/fishercoder/solutions/_867.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _867 {
4+
public static class Solution1 {
5+
public int[][] transpose(int[][] A) {
6+
int[][] result = new int[A[0].length][A.length];
7+
for (int i = 0; i < A.length; i++) {
8+
for (int j = 0; j < A[0].length; j++) {
9+
result[j][i] = A[i][j];
10+
}
11+
}
12+
return result;
13+
}
14+
}
15+
}

Diff for: src/test/java/com/fishercoder/_867Test.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._867;
4+
import com.fishercoder.solutions._9;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _867Test {
12+
private static _867.Solution1 solution1;
13+
private static int[][] A;
14+
private static int[][] result;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _867.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
A = new int[][]{
24+
{1, 2, 3},
25+
{4, 5, 6},
26+
{7, 8, 9}
27+
};
28+
result = new int[][]{
29+
{1, 4, 7},
30+
{2, 5, 8},
31+
{3, 6, 9}
32+
};
33+
assertArrayEquals(result, solution1.transpose(A));
34+
}
35+
36+
@Test
37+
public void test2() {
38+
A = new int[][]{
39+
{1, 2, 3}
40+
};
41+
result = new int[][]{
42+
{1},
43+
{2},
44+
{3}
45+
};
46+
assertArrayEquals(result, solution1.transpose(A));
47+
}
48+
49+
@Test
50+
public void test3() {
51+
A = new int[][]{
52+
{1},
53+
{2},
54+
{3}
55+
};
56+
result = new int[][]{
57+
{1, 2, 3}
58+
};
59+
assertArrayEquals(result, solution1.transpose(A));
60+
}
61+
62+
@Test
63+
public void test4() {
64+
A = new int[][]{
65+
{1, 2, 3, 4},
66+
{5, 6, 7, 8},
67+
{9, 10, 11, 12}
68+
};
69+
result = new int[][]{
70+
{1, 5, 9},
71+
{2, 6, 10},
72+
{3, 7, 11},
73+
{4, 8, 12}
74+
};
75+
assertArrayEquals(result, solution1.transpose(A));
76+
}
77+
78+
}

0 commit comments

Comments
 (0)