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-9must occur exactly once in each row. - Each of the digits
1-9must occur exactly once in each column. - Each of the the digits
1-9must occur exactly once in each of the 93x3sub-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-9and 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] == c) return false; //check row
if(board[row][i] == c) return false; //check column
if(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] Reverse nodes in k group 每k个一组反转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转
问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...
- LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation
1. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For e ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- python基础之魔法方法
由于hexo自带的markdown渲染引擎对双下划线做了转义,在正文中看到的魔法方法前后都没有双下划线 setattr.getattr.delattr 可以拦截对对象属性的访问 setattr函数是用 ...
- JavaScript截取中英文字符串
有时在显示某段文字的时候,可能会太长,影响我们页面的显示效果.如果仅是英文,那么我们可以用String.substring(start, end)函数就已经够用了.但是通常我们都会遇到既有英文,又有汉 ...
- 页面元素的CSS渲染优先级
样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下:(外部样式)External style ...
- windows下vue项目启动步骤
原创:https://blog.csdn.net/qq_27680317/article/details/71123051?locationNum=10&fps=1 不是ngnix服务器是,忽 ...
- LightOJ 1226 - One Unit Machine Lucas/组合数取模
题意:按要求完成n个任务,每个任务必须进行a[i]次才算完成,且按要求,第i个任务必须在大于i任务完成之前完成,问有多少种完成顺序的组合.(n<=1000 a[i] <= 1e6 mod ...
- 【C++ STL】容器的选择
c++提供了各具特长的容器,那么我们该如何选择最佳的容器? 缺省状态下应该选择vector,因为vector内部结构最简单,并允许随机存取,所以数据的存取十分方便,数据的处理也快. 如果经常要在头部和 ...
- [uva11137]立方数之和·简单dp
小水题再来一发 给定一个正整数n<=1e4,求将n写成若干个正整数立方和的方法数 典型的多阶段模型 f[i][j]表示当前用到1~i的数,累计和为j的方案数. #include<cstdi ...
- 【CF1009F】 Dominant Indices (长链剖分+DP)
题目链接 \(O(n^2)\)的\(DP\)很容易想,\(f[u][i]\)表示在\(u\)的子树中距离\(u\)为\(i\)的点的个数,则\(f[u][i]=\sum f[v][i-1]\) 长链剖 ...
- 有关计数问题的dp
问题一:划分数 问题描述 有n个去区别的物体,将它们划分成不超过m组,求出划分方法数模M的余数. 我们定义dp[i][j],表示j的i划分的总数 将j划分成i个的话,可以先取出k个,然后将剩下的j-k ...
- 【ALB学习笔记】基于多线程方式的串行通信接口数据接收案例
基于多线程方式的串行通信接口数据接收案例 广东职业技术技术学院 欧浩源 1.案例背景 在本博客的<[CC2530入门教程-06]CC2530的ADC工作原理与应用>中实现了电压数据采集的 ...