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. NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解

    肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...

  2. JavaScript中Unicode值转字符

    在JavaScript中,将Unicode值转字符的方法: <!DOCTYPE html> <html> <head> <meta charset=" ...

  3. iOS AFNetworking 2.6.0框架导入报错解决方法

    最近手动导入AFNetworking 2.6.0框架时发现Xcode报如下3个错误: 1. Use of undeclared identifier ‘kSecFormatUnknown‘ 2. Us ...

  4. Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)

    C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...

  5. 题解【luoguP1525 NOIp提高组2010 关押罪犯】

    题目链接 题解 算法: 一个经典的并查集 但是需要用一点贪心的思想 做法: 先将给的冲突们按冲突值从大到小进行排序(这很显然) 然后一个一个的遍历它们 如果发现其中的一个冲突里的两个人在同一个集合里, ...

  6. Leetcode 445. 两数相加 II

    1.题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. ...

  7. Ubuntu修改系统语言为英文可支持中文

    简单来说,就行修改/etc/default/locale文件,设置语言位UTF-8,如果没有这个语言,就执行命令locale-gen en_US.UTF-8进行安装,没有即时生效的话就重启. 查看当前 ...

  8. mac命令行配置网络

    mac命令行配置网络今天终于找到了Mac OS X通过命令行修改ip的方式了,记录如下: 修改mac地址,重启后失效sudo ifconfig en0 lladdr d0:67:e5:2e:07:f1 ...

  9. MongoDB入门(3)- MongoDB备份与恢复

    1. 备份 MongoDB提供了备份工具,mongodump.exe,在bin目录下,其用法如下: mongodump.exe -h localhost -d database_name -o d:\ ...

  10. LightOJ 1129 - Consistency Checker Trie树模板

    题意:给出n条串判断是否存在一个串为另一个串的前缀. 思路:套Trie树的模板,先全部插入,再查找每个字串,如果查找字串完毕,但还存在下一个节点,说明存在前缀. /** @Date : 2016-11 ...