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:

  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] != ‘.‘ && 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;
    }
}

原文地址:https://www.cnblogs.com/f91og/p/9411057.html

时间: 2024-11-06 23:34:17

LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver的相关文章

LeetCode解题报告--Swap Nodes in Pairs

题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list

leetCode解题报告5道题(九)

题目一:Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 分析: 题意给我们一个数字n, 和一个数字k,让我们求出从 1~~n中取出k个数所能得到的组合数 所

LeetCode解题报告:Reorder List

Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 1.利用快慢两个指针将链表一分为二: 2.针对第二个子链表求倒序

leetCode解题报告5道题(十)

Disk Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2368    Accepted Submission(s): 333 Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景.磁

leetCode解题报告5道题(六)

题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the

leetCode解题报告5道题(八)

题目一: Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the

LeetCode解题报告:Linked List Cycle &amp;&amp; Linked List Cycle II

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo

LeetCode解题报告:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

leetCode解题报告5道题(十一)

题目一:Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2]