Skip to content

Commit 25760b4

Browse files
add 2191
1 parent 4ab2386 commit 25760b4

File tree

3 files changed

+66
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+66
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
| 2196 | [Create Binary Tree From Descriptions](https://leetcode.com/problems/create-binary-tree-from-descriptions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2196.java) || Medium | HashTable, Tree
122122
| 2194 | [Cells in a Range on an Excel Sheet](https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2194.java) || Easy ||
123123
| 2192 | [All Ancestors of a Node in a Directed Acyclic Graph](https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2192.java) || Medium | Topological Sort, Graph
124+
| 2191 | [Sort the Jumbled Numbers](https://leetcode.com/problems/sort-the-jumbled-numbers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2191.java) || Medium | Array, Soring
124125
| 2190 | [Most Frequent Number Following Key In an Array](https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2190.java) || Easy ||
125126
| 2186 | [Minimum Number of Steps to Make Two Strings Anagram II](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2186.java) || Medium ||
126127
| 2185 | [Counting Words With a Given Prefix](https://leetcode.com/problems/counting-words-with-a-given-prefix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2185.java) || Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.Arrays;
4+
5+
public class _2191 {
6+
public static class Solution1 {
7+
public int[] sortJumbled(int[] mapping, int[] nums) {
8+
int[][] result = new int[nums.length][2];
9+
for (int i = 0; i < nums.length; i++) {
10+
result[i][0] = convert(nums[i], mapping);
11+
result[i][1] = i;
12+
}
13+
Arrays.sort(result, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
14+
int[] list = new int[nums.length];
15+
for (int i = 0; i < nums.length; i++) {
16+
list[i] = nums[result[i][1]];
17+
}
18+
return list;
19+
}
20+
21+
private int convert(int num, int[] mapping) {
22+
char[] charArray = String.valueOf(num).toCharArray();
23+
StringBuilder sb = new StringBuilder();
24+
for (char c : charArray) {
25+
sb.append(mapping[Character.getNumericValue(c)]);
26+
}
27+
int i = 0;
28+
while (i < sb.length() && sb.charAt(i) == '0') {
29+
i++;
30+
}
31+
if (i >= sb.length()) {
32+
return 0;
33+
}
34+
return Integer.parseInt(sb.substring(i));
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2191;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
public class _2191Test {
10+
private static _2191.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2191.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{338, 38, 991}, solution1.sortJumbled(new int[]{8, 9, 4, 0, 2, 1, 3, 5, 7, 6}, new int[]{991, 338, 38}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new int[]{0, 999999999}, solution1.sortJumbled(
25+
new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
26+
new int[]{0, 999999999}));
27+
}
28+
}

0 commit comments

Comments
 (0)