Table of contents
Question meaning:
Please judge
9 x 9
whether a Sudoku is valid. Just follow the following rules to verify whether the numbers you have filled in are valid.
- Numbers
1-9
can appear only once per line.- Numbers
1-9
can appear only once in each column.- Numbers can appear only once
1-9
in each3x3
palace separated by a thick solid line. (Please refer to the example picture)Notice:
- A valid Sudoku (partially filled in) is not necessarily solvable.
- You only need to verify whether the numbers you have filled in are valid according to the above rules.
- Blank spaces are
'.'
represented by .
【Input sample】
board = [["5","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]][Output sample] true
Problem-solving ideas:
1. Both row and column are relatively simple. Use a two-dimensional array a[i][num] to store the number of occurrences of num in the i-th row/column. The value range of num is 1~9;
2. Each 3×3 small nine-square grid is implemented with a three-dimensional array. Matrix[i][j][num] represents the number of times num appears in the nine-square grid in the i-th row and j-th column in the three-dimensional array.
3. Observe the coordinate relationship of the nine small nine-square grid. The row and column coordinate range of the small nine-square grid in the upper left corner is (0~2,0~2). If you divide it by 3, the coordinates are (0,0), and the second The coordinates of the nine-square grid (viewed horizontally) are (0~2,3~5), and the result obtained by dividing 3 is (0,1). Therefore, by dividing the row range and column range by 3 at the same time, each small nine-square grid can be placed in The coordinates in the three-dimensional array matrix are determined as (0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1),(2,2)
4. OK, start the enumeration. Every time you find a non-"." character, add it to the three statistical arrays. After adding it, you need to judge. After adding it, you will get the value of a[i][num]. Will it be greater than 1? Greater than 1 means that it does not comply with the rules and the columns are consistent.
class Solution {
public boolean isValidSudoku(char[][] board) {
int[][] row = new int[10][9];//行row[i][num]:第i行num值有多少个
int[][] col = new int[10][9];//列
int[][][] matrix = new int[10][10][9];//小矩阵
for(int i=0;i<9;++i){
for(int j=0;j<9;++j){
//先填充行的矩阵
char temp = board[i][j];
if(temp!='.'){
int num = temp - '0'-1;
++row[i][num];
++col[j][num];
++matrix[i/3][j/3][num];
if(row[i][num]>1||col[j][num]>1||matrix[i/3][j/3][num]>1){
return false;
}
}
}
}
return true;
}
}
Time: Defeated 18.26%
Memory: Beaten by 5.14%
54. Spiral matrix
Question meaning:
Given a matrix of
m
rows and columns , please return all elements in the matrix in clockwise spiral order .n
matrix
【Input sample】
matrix=[[1,2,3],[4,5,6],[7,8,9]]
[Output sample][1,2,3,6,9,8,7,4,5]
Problem-solving ideas:
1. First, there must be a corresponding label matrix isChoose[i][j], 0 means that the values in matrix[i][j] have not been traversed yet;
2. Define the row and column access pointers i and j; according to the rule of the question, the matrix access rule is: go right --> go down --> go left --> go up --> go right
3. Define the variable count to count how many numbers have been accessed to facilitate the end of the loop;
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
//在有些题中也叫做蛇形矩阵
int m =matrix.length;//m行
int n = matrix[0].length;//n列
List<Integer> ans = new ArrayList<>();
if(matrix == null || m == 0 || n== 0){
return ans;
}
int i=0,j=0;
int[][] isChoose = new int[m][n];
isChoose[0][0] = 1;//从第一位开始,第一位先走
ans.add(matrix[0][0]);
int count = 1;
while(count < m*n){
//向右走,是列在变化,行不变
while(j+1 < n && isChoose[i][j+1] == 0){
//下一位没有越界,并且没有被访问过的时候,可以进行访问
ans.add(matrix[i][j+1]);
isChoose[i][j+1] = 1;
//统计个数
++count;
++j;
}
//向下走,行在变化
while(i+1 < m && isChoose[i+1][j] == 0){
//下一位没有越界,并且没有被访问过的时候,可以进行访问
ans.add(matrix[i+1][j]);
isChoose[i+1][j] = 1;
//统计个数
++count;
++i;
}
//向左走
while(j-1 >= 0 && isChoose[i][j-1] == 0){
//下一位没有越界,并且没有被访问过的时候,可以进行访问
ans.add(matrix[i][j-1]);
isChoose[i][j-1] = 1;
//统计个数
++count;
--j;
}
//向上走
while(i-1>= 0 && isChoose[i-1][j] == 0){
ans.add(matrix[i-1][j]);
isChoose[i-1][j] =1;
++count;
--i;
}
}
return ans;
}
}
Time: Beat 100.00%
Memory: Beaten by 66.09%
48. Rotate images
Question meaning:
Given an n × n two-dimensional matrix
matrix
representing an image. Please rotate the image 90 degrees clockwise.You have to rotate the image in place , which means you need to modify the input 2D matrix directly. Please do not use another matrix to rotate the image.
【Input sample】
matrix=[[1,2,3],[4,5,6],[7,8,9]
【Output sample】
[[7,4,1],[8,5,2],[9,6,3]
Problem-solving ideas:
1. Find the pattern. The j-th element in the first row is flipped to the j-th element in the penultimate column; the j-th element in the second row is flipped to the j-th element in the penultimate column;
2. Let i represent the row number and j represent the column number. The rule is: (i, j) --> (j, n-1-i)
3. Just do the replacement. The replacement here is because you need to flip the array in place, and I thought of the rotation array that I practiced before . ok give it a try.
4. The difference between this and the rotating array is that a two-dimensional array will form a ring after rotating 4 times.
5.Number of cycles
In each round of replacement, 4 elements can be placed at the specified position. Therefore, n*n elements require a total of n*n/4 replacements.
How do you know how many times the rows and columns have been traversed?
Hehe, it is divided into 4 equal parts, which means that we traverse the rows n/2 times and the columns n/2 times. Considering that n is an odd number, there will be an extra column in the middle. At this time, the columns need to be traversed (n+ 1)/2, which means rounding up is guaranteed.
class Solution {
public void rotate(int[][] matrix) {
int temp;//存储临时元素
int n = matrix.length;
for(int i=0;i<n/2;++i){
for(int j= 0;j< (n+1)/2; ++j){
temp = matrix[i][j];
matrix[i][j] = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
matrix[j][n-1-i] = temp;
}
}
}
}
Time: Beat 100.00%
Memory: Beaten by 31.83%
73.Matrix zeroing
Question meaning:
Given a matrix, if an element is 0 , set all elements in its row and column to 0 . Please use the in-place algorithm .
m x n
【Input sample】
matrix=[[1,1,1],[1,0,1],[1,1,1]]
[Output sample][[1,0,1],[0,0,0],[1,0,1]]
Problem-solving ideas:
When I got the question, I thought of a very simple and crude method;
Define two arrays a[i] and b[j], which are used to determine whether there are elements of 0 in the i-th row and j-th column respectively;
Traverse the elements and assign values to arrays a and b
Traverse the array again and modify the elements
class Solution {
public void setZeroes(int[][] matrix) {
//两个数组a和b,用来判断那一些列有0,哪一些没有0
int[] a = new int[matrix.length];//行
int[] b = new int[matrix[0].length];//列
for(int i=0;i<matrix.length;++i){
for(int j=0;j<matrix[0].length;++j){
if(matrix[i][j] == 0){
a[i] = 1;//所有i行,j列的数据都要为0
b[j] = 1;
}
}
}
for(int i=0;i<matrix.length;++i){
if(a[i] == 1){
for(int j=0;j<matrix[0].length;++j){
matrix[i][j] =0;
}
}
}
for(int j=0;j<matrix[0].length;++j){
if(b[j] == 1){
for(int i=0;i<matrix.length;++i){
matrix[i][j] =0;
}
}
}
}
}
Time: Defeated 100.00%, my double cycle can still be 100%, I didn’t expect it haha
Memory: Beaten by 59.17%