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:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 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] == 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的更多相关文章

  1. [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 ...

  2. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

  3. 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 ...

  4. 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 算法思想:基本操作就是链表 ...

  5. 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 ...

  6. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  7. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  8. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. java中new一个对象放在循环体里面与外面的区别

    首先说下问题: 这次在做项目的是出现了一个new对象在循环里面与外面造成的不同影响. 大家可以看到这个new的对象放在不同的位置产生的效果是不一样的. 经过多方查询与验证可以得出结论: * EasyU ...

  2. DOM通过ID或NAME获取值

    DOM通过ID或NAME获取值 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...

  3. 35 个你也许不知道的 Google 开源项目

    转载自:http://blog.csdn.net/cnbird2008/article/details/18953113 Google是支持开源运动的最大公司之一,它们现在总共发布有超过500个的开源 ...

  4. maven报错 Failure to transfer org.apache.maven.plugins:maven-compiler-plugin:pom:3.5.0 from

    maven报错误,类似于: Failure to transfer org.apache.maven.plugins:maven-compiler-plugin:pom:3.5.0 from http ...

  5. Linux中 设置apache,mysql 开机启动

    linux开启启动的程序一般放在/etc/rc.d/init.d/里面,/etc/init.d/是其软连接 mysql设为linux服务 cp /usr/local/mysql5/share/mysq ...

  6. 验证组件——FluentValidation

          FluentValidation FluentValidation是与ASP.NET DataAnnotataion Attribute验证实体不同的数据验证组件,提供了将实体与验证分离开 ...

  7. 【hdu1255】线段树求矩形面积交

    题意大概就是上图这个样子.<=100组测试数据,每组<=1000个矩形. 题解: 这个问题怎么解决..做了上一题矩形面积并应该就会了.. 对于每个节点维护3个值: cnt:该节点所代表的这 ...

  8. js_时间戳和时间格式之间的转换。

    关于我的理解,简单明了点: 时间戳:把一个日期使用一个数字表示出来,这个数字就是这个日期的秒数. 日期:就是我们常见的时间表现形式. 时间戳对于一般看时间不够直观明了,可是在程序的世界里作用可大了. ...

  9. Python 对象模型 -- (转)

    面向对象的纯粹性 在很久很久以前,C++还被称为面向对象语言(现在一般称为多范式通用语言),人们就对C++的面向对象的纯粹性提出了质疑,主要有以下几点: 并非所有的对象都是对象(很拗口?),比如指针本 ...

  10. Java 关于微信公众号支付总结附代码

    很多朋友第一次做微信支付的时候都有蒙,但当你完整的做一次就会发现其实并没有那么难 业务流程和应用场景官网有详细的说明:https://pay.weixin.qq.com/wiki/doc/api/js ...