# 2055. Plates between candles / 54 Spiral matrix / 59 Spiral matrix II

Posted by Schlo_50 on Tue, 08 Mar 2022 10:11:25 +0100

2055. Plates between candles [medium] [daily]

Idea:

1. Record the number of plates in front of each position with cnt; Use left to record the position of the first candle on the left of the current position; Use right to record the position of the first candle on the right of the current position.
2. Traverse each query, and define x as the position of the first candle on the right of the starting point of the current query (if there is no candle, set it to - 1), and y as the position of the first candle on the left of the ending point of the current query (if there is no candle, set it to - 1). If there is a candle interval that meets the meaning of the question, update the current query result to
The difference between the number of plates in front of the right candle and the number of plates in front of the left candle, otherwise, set to 0.

code:

```class Solution {
public int[] platesBetweenCandles(String s, int[][] queries) {
int lens = s.length(),lenq = queries.length;
int[] ans = new int[lenq];
int[] cnt = new int[lens];
int count = 0;
for (int i = 0; i < lens; i++) {//Record the current position. How many plates are ahead
if (s.charAt(i) == '*'){
count++;
}
cnt[i] = count;
}
int[] left = new int[lens];
for (int i = 0,l = -1; i < lens; i++) {//Record the position of the first candle on the left of the current position. If not, set it to - 1
if (s.charAt(i) == '|'){
l = i;
}
left[i] = l;
}
int[] right = new int[lens];
for (int i = lens-1,r = -1; i >= 0 ; i--) {//Record the position of the first candle on the right of the current position. If not, set it to - 1
if (s.charAt(i) == '|'){
r = i;
}
right[i] = r;
}
for (int i = 0; i < lenq; i++) {//Traverse each query
int[] query = queries[i];
int x =  right[query], y = left[query];//Define x as the position of the first candle on the right of the starting point of the current query, and y as the position of the first candle on the left of the ending point of the current query
//If there is a candle range that matches the meaning of the question, update the current query result to the difference between the number of plates in front of the right candle and the number of plates in front of the left candle. Otherwise, set it to 0
ans[i] = x == -1 || y == -1 || x>=y  ? 0 : cnt[y]-cnt[x];
}
return ans;
}
}
```

54. Spiral matrix [medium]

1. Let the number of rows and columns of the matrix be row and col, then the number of elements of the matrix is n=row*col. define the row subscript of the elements traversed in the matrix as r and the column subscript as c. define the direction of adding elements to the list as direction, and the initial value is 0. Set four values of direction, 0 right, 1 down, 2 left and 3 up.
2. When defining the start spiral, the start row, start column, end row and end column are start respectively_ r,start_c,end_r,start_c. The initial values are set to 0, 0, row and col respectively.
3. Start traversing the matrix, traversing a total of n times.
4. Add the current matrix element to the list, and then judge the direction of the next element. To the right, c + +; Down, then r + +; Left, then c –; Up, then r –;
5. In the process of judging the direction, if you encounter a terminating row or column, change the direction and update the boundary. Specifically, it is to the right. If you encounter a boundary, change it to the down and start the row + +; If you encounter a boundary down, change to the left and terminate the column –; It turns out to be left. If it encounters a boundary, it changes to up and terminates the line –; It turns out to be upward. When it meets the boundary, it changes to the right and starts with the line + +;

code:

```class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
int row = matrix.length,col = matrix.length;
int n = row*col,r = 0,c = 0;
int direction = 0;//The direction flag bit indicates 0 right, 1 lower, 2 left and 3 upper in turn. Each time the direction is changed, the flag bit is + +. When it is equal to 4, 4 is subtracted
int start_r = 0,start_c = 0,end_r = row,end_c = col;
while (n>0){
switch (direction){
case 0:
if (c < end_c-1){
c++;
}else {
direction++;
r++;
start_r++;
}
break;
case 1:
if (r < end_r-1){
r++;
}else {
direction++;
c--;
end_c--;
}
break;
case 2:
if (c > start_c){
c--;
}else {
direction++;
r--;
end_r--;
}
break;
case 3:
if (r > start_r){
r--;
}else {
direction = 0;
c++;
start_c++;
}
break;
}
n--;
}
return list;
}
}
```

Time: 59. Spiral matrix II [medium]

Idea:

Keep up with the questions, change the soup without changing the dressing, and the ideas are the same. It's just that the number in the previous question is taken out according to the spiral order, and this question is put in according to the spiral order.

code:

```class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int direction = 0;//The direction flag bit indicates 0 right, 1 lower, 2 left and 3 upper in turn. Each time the direction is changed, the flag bit is + +. When it is equal to 4, 4 is subtracted
int start_r = 0,start_c = 0,end_r = n,end_c = n,num = 1,end = n*n,r = 0,c = 0;
while (num<=end){
matrix[r][c] = num;
switch (direction){
case 0:
if (c < end_c-1){
c++;
}else {
direction++;
r++;
start_r++;
}
break;
case 1:
if (r < end_r-1){
r++;
}else {
direction++;
c--;
end_c--;
}
break;
case 2:
if (c > start_c){
c--;
}else {
direction++;
r--;
end_r--;
}
break;
case 3:
if (r > start_r){
r--;
}else {
direction = 0;
c++;
start_c++;
}
break;
}
num++;
}
return matrix;
}
}
```

Time: Topics: Java leetcode