Skip to content

Commit e145100

Browse files
committed
Added Sequential Digits.java & modified 2 solutions
1 parent e95d5d0 commit e145100

File tree

3 files changed

+66
-84
lines changed

3 files changed

+66
-84
lines changed

Easy/Rotting Oranges.java

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,39 @@
11
class Solution {
2-
int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
3-
public int orangesRotting(int[][] grid) {
4-
Queue<Integer> queue = new LinkedList<>();
5-
Map<Integer, Integer> map = new HashMap<>();
6-
int numRows = grid.length;
7-
int numCols = grid[0].length;
8-
9-
for (int i = 0; i < numRows; i++) {
10-
for (int j = 0; j < numCols; j++) {
11-
if (grid[i][j] == 2) {
12-
int key = numCols * i + j;
13-
queue.add(key);
14-
map.put(key, 0);
15-
}
16-
}
2+
int[][] dirs = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
3+
public final int VISITED = -1;
4+
public int orangesRotting(int[][] grid) {
5+
int minutes = 0;
6+
Queue<int[]> queue = new LinkedList<>();
7+
for (int i = 0; i < grid.length; i++) {
8+
for (int j = 0; j < grid[i].length; j++) {
9+
if (grid[i][j] == 2) {
10+
queue.add(new int[]{i, j});
1711
}
18-
19-
int ans = 0;
20-
while (!queue.isEmpty()) {
21-
int key = queue.remove();
22-
int x = key / numCols;
23-
int y = key % numCols;
24-
for (int[] dir : dirs) {
25-
int newX = x + dir[0];
26-
int newY = y + dir[1];
27-
if (newX < 0 || newX >= numRows || newY < 0 || newY >= numCols || grid[newX][newY] != 1) {
28-
continue;
29-
}
30-
grid[newX][newY] = 2;
31-
int newKey = newX * numCols + newY;
32-
queue.add(newKey);
33-
map.put(newKey, map.get(key) + 1);
34-
ans = map.get(newKey);
35-
}
12+
}
13+
}
14+
while (!queue.isEmpty()) {
15+
int size = queue.size();
16+
while (size-- > 0) {
17+
int[] removed = queue.remove();
18+
for (int[] dir : dirs) {
19+
int newX = removed[0] + dir[0];
20+
int newY = removed[1] + dir[1];
21+
if (newX >= 0 && newX < grid.length && newY >= 0 && newY < grid[0].length && grid[newX][newY] == 1) {
22+
grid[newX][newY] = 2;
23+
queue.add(new int[]{newX, newY});
24+
}
3625
}
37-
38-
for (int i = 0; i < numRows; i++) {
39-
for (int j = 0; j < numCols; j++) {
40-
if (grid[i][j] == 1) {
41-
return -1;
42-
}
43-
}
26+
grid[removed[0]][removed[1]] = VISITED;
27+
}
28+
minutes++;
29+
}
30+
for (int i = 0; i < grid.length; i++) {
31+
for (int j = 0; j < grid[i].length; j++) {
32+
if (grid[i][j] == 1) {
33+
return -1;
4434
}
45-
46-
return ans;
35+
}
4736
}
37+
return minutes > 0 ? minutes - 1 : 0;
38+
}
4839
}

Medium/Sequential Digits.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public List<Integer> sequentialDigits(int low, int high) {
3+
String bucket = "123456789";
4+
int n = 10;
5+
List<Integer> list = new ArrayList<>();
6+
int lowLength = String.valueOf(low).length();
7+
int highLength = String.valueOf(high).length();
8+
for (int length = lowLength; length < highLength + 1; length++) {
9+
for (int start = 0; start < n - length; start++) {
10+
int num = Integer.parseInt(bucket.substring(start, start + length));
11+
if (num >= low && num <= high) {
12+
list.add(num);
13+
}
14+
}
15+
}
16+
return list;
17+
}
18+
}

Medium/Summary Ranges.java

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,18 @@
11
class Solution {
2-
public List<String> summaryRanges(int[] nums) {
3-
List<String> ranges = new ArrayList<>();
4-
5-
if (nums.length == 0) {
6-
return ranges;
7-
}
8-
9-
int count = 1;
10-
int i = 0;
11-
StringBuilder sb = new StringBuilder();
12-
sb.append(nums[0]);
13-
int n = nums.length-1;
14-
15-
while (i < n) {
16-
if (nums[i+1] - nums[i] == 1) {
17-
count++;
18-
}
19-
else {
20-
if (count > 1) {
21-
sb.append("->").append(nums[i]);
22-
ranges.add(sb.toString());
23-
count = 1;
24-
}
25-
else {
26-
ranges.add(sb.toString());
27-
}
28-
sb = new StringBuilder();
29-
sb.append(nums[i+1]);
30-
}
31-
32-
i++;
33-
}
34-
35-
if (count > 1) {
36-
sb.append("->").append(nums[i]);
37-
ranges.add(sb.toString());
38-
}
39-
else {
40-
ranges.add(sb.toString());
41-
}
42-
43-
return ranges;
2+
public List<String> summaryRanges(int[] nums) {
3+
List<String> list = new ArrayList<>();
4+
int idx = 0;
5+
int n = nums.length;
6+
while (idx < n) {
7+
int start = nums[idx];
8+
int prev = start;
9+
idx++;
10+
while (idx < n && nums[idx] - prev == 1) {
11+
prev = nums[idx];
12+
idx++;
13+
}
14+
list.add(start == prev ? String.valueOf(start) : String.valueOf(start + "->" + prev));
4415
}
16+
return list;
17+
}
4518
}

0 commit comments

Comments
 (0)