LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver
Posted f91og
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver相关的知识,希望对你有一定的参考价值。
1. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
Example:
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
Note:
- Only constant extra memory is allowed.
- You may not alter the values in the list‘s nodes, only nodes itself may be changed.
思路:尝试遍历链表,根据索引关系来逆置指定部分,结果发现复杂无比,gg。还是看了下解析,置顶的方法是用递归来解题。其基本想法是对于找到的k个节点逆置,然后对剩下的节点作相同处理。虽然不是很高效但是却非常通俗易懂。
代码:
public ListNode reverseKGroup(ListNode head, int k){
ListNode cur=head;
int count=0;
while(cur!=null && count<k){
cur=cur.next;
count++;
}
if(count==k){
cur=reverseKGroup(cur, k); //标记1
//这里的逆转逻辑是依次将第一个移动到相应后面的位置,cur始终指向这个位置的下一个
while(count-- > 0){
ListNode temp=head.next; //将当前头结点的下一个赋给temp
head.next=cur; //cur指向下一组逆转置后的头结点(第一个几点),将head移动到逆置后的位置,也就是cur前面
cur=head; //因为head已经移动好了,接下来移动temp的话,应该是移动到head的前面,所以需要将cur指向head
head=temp;//将原先第二个节点temp置为新的head,循环往后移,直至移动了k个
}
//逆置完成后cur应该指向当前k个节点中的第一个,
//需要将其赋给head返回,标记1处的cur才能指向其后面k个节点逆置后的第一个节点
head=cur;
}
return head;
}
2. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
- Each of the digits
1-9
must occur exactly once in each row. - Each of the digits
1-9
must occur exactly once in each column. - Each of the the digits
1-9
must occur exactly once in each of the 93x3
sub-boxes of the grid.
Empty cells are indicated by the character ‘.‘
.
A sudoku puzzle...
...and its solution numbers marked in red.
Note:
- The given board contain only digits
1-9
and the character‘.‘
. - You may assume that the given Sudoku puzzle will have a single unique solution.
- The given board size is always
9x9
.
思路:看了下discussion,发现有多种解题方法,比如DFS或者Backtracing,也有算法大神给出了很厉害的解法。还是选择较为简单的Backtracing方法来考虑。其基本思路是:
1. 遍历整个数独,对于空缺的部分,尝试用1到9填充。
2. 填充前先判断当前位置所对应的行和列,以及所在的3*3矩阵有没有整个1~9数字,如果有则跳过判断1~9中的下一个。
3. 填充好了后,将当前矩阵作为新的输入重复步骤1,2,3,这样递归,如果有一层如果1~9都填不进去则表示数独不合法,上层填的有误,需返回false到上层重置上层填的数字。如果顺利递归到最底层,也就是填好了整个数独,因为每次的赋值标准是数独合法,所以填好的数独也是合法的,故返回true。
代码:
public class Solution {
public void solveSudoku(char[][] board) {
if(board == null || board.length == 0)
return;
solve(board);
}
public boolean solve(char[][] board){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == ‘.‘){
for(char c = ‘1‘; c <= ‘9‘; c++){//trial. Try 1 through 9,注意这里直接c++就可以取到字符数字的下一个
if(isValid(board, i, j, c)){
board[i][j] = c; //Put c for this cell
if(solve(board))
return true; //If it‘s the solution return true
else
board[i][j] = ‘.‘; //Otherwise go back
}
}
return false; // 如果在某一层遍历了1~9后还是没有返回true则表示无解,上层填充的数字有问题,返回false到上层将相应的字符数字改回为‘.‘
}
}
}
return true;// 如果可以顺利通过所有遍历,因为每次的赋值标准是数独合法,所以顺利遍历之后数独也是合法的,故返回true
}
private boolean isValid(char[][] board, int row, int col, char c){
for(int i = 0; i < 9; i++) {
if(board[i][col] != ‘.‘ && board[i][col] == c) return false; //check row
if(board[row][i] != ‘.‘ && board[row][i] == c) return false; //check column
if(board[3 * (row / 3) + i / 3][ 3 * (col / 3) + i % 3] != ‘.‘ &&
board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) return false; //check 3*3 block,这里的check 3*3 block还是很有意思的
}
return true;
}
}
以上是关于LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 206 Reverse Linked List 解题报告
LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver
LeetCode --- 1317. Convert Integer to the Sum of Two No-Zero Integers 解题报告
LeetCode --- 1317. Convert Integer to the Sum of Two No-Zero Integers 解题报告