Lintcode algorithm--Determining whether Sudoku is legal, planar list, cloned binary tree, sequence automatic arranging

Source: Internet
Author: User
Tags arrays lintcode
Judging whether Sudoku is legal

Please determine if a Sudoku is valid.

The Sudoku may only be populated with partial numbers, where missing numbers are used. Said. Precautions

A valid Sudoku (partial padding only) is not necessarily solvable. We just need to make the padding space valid. Sample Example algorithm

public class Solution {/* * @param board:the Board * @return: Whether the Sudoku is valid */publ

        IC Boolean Isvalidsudoku (char[][] board) {//write your code here boolean[] index = new boolean[9];
            for (int j = 0; j<9; j + +) {Arrays.fill (index, false);
            for (int i = 0; i<9; i++) {if (!checksudoku (index, Board[j][i]) return false;
            }} for (int i = 0; i<9; i++) {Arrays.fill (index, false);
            for (int j = 0; j<9; j + +) {if (!checksudoku (index, BOARD[I][J])) return false; }} for (int i=0, i<9; i=i+3) {for (int j = 0; j<9; j=j+3) {Arrays
                . Fill (index, false); for (int k = 0; k<9; k++) {if (!checksudoku (index, board[i+k/3][j+k%3])) Retu
                RN false; }
            }
        } return true;
            } public boolean Checksudoku (boolean[] index, char num) {if (num = = '. ')
        return true;    
        int _num = num-' 0 '; if (num>9| | num<0| |

        Index[_num-1]) return false;
        Index[_num-1] = true;    
    return true;
 }
}
Flat List

Given a list, each feature in the list is either a list or an integer. Turn it into a simple list that contains only integers. Sample Example

Given [1,2,[1,2]], return [1,2,1,2].

Given [4,[3,[2,[1]]], return [4,3,2,1]. algorithm

Use recursion

 /** *//This is the interface, allows for creating nested lists. *//should not implement it, or speculate on its implementation * public interface Nestedinteger {* *//@
 Return true if this nestedinteger holds a single integer, *//rather than a nested list.
 * Public boolean isinteger (); * *//@return The single integer which this nestedinteger holds, *//if it holds a single integer *//Ret
 Urn NULL if this nestedinteger holds a nested list * public Integer getinteger (); * *//@return The nested list that this nestedinteger holds, *//if it holds a nested list *//Return Nu
 ll if this nestedinteger holds a single integer * Public list<nestedinteger> getList (); * */public class Solution {//@param nestedlist a list of Nestedinteger//@return A list of integer pub Lic list<integer> Flatten (list<nestedinteger> nestedlist) {//Write your code here LIST&LT;integer> list = new arraylist<integer> ();
            for (Nestedinteger ele:nestedlist) {if (Ele.isinteger ()) List.add (Ele.getinteger ());

        else List.addall (Flatten (Ele.getlist ()));
    } return list;
 }

}
Cloning binary Tree

Deep copy a binary tree.

Given a binary tree, return a sample of his clones

Given a two-pronged tree

    1               
   /  \
  2    3     
 /\
4   5
    =
    1
   /  \
  2    3
 /\
4   5
algorithm
/**
 * Definition of TreeNode:
 * public class TreeNode {
 * public     int val;
 * Public     TreeNode left and right;
 * Public     TreeNode (int val) {
 *         this.val = val;
 *         this.left = This.right = null;
 *     }
 *} * * Public
class Solution {
    /**
     * @param root:the root of binary tree
     * @return Root of new Tree
     * * Public
    TreeNode clonetree (TreeNode root) {
        //Write Your code here
        if (root==null)
            return null;
        TreeNode _root = new TreeNode (root.val);
        if (root.left!=null)
            _root.left = Clonetree (root.left);
        if (root.right!=null)
            _root.right = Clonetree (root.right);
        return _root;    
    }
}
ordinal arrangement

Arrange serial numbers

Gives an arrangement that does not contain a repeating number, which is the number of all the permutations of these numbers sorted by dictionary order. Where the numbering starts from 1. Sample Example

For example, the permutation [1,2,4] is the 1th permutation. algorithm

Forgive me for not understanding the meaning of the topic at first.
For a number 321, the three-digit permutation has 3. Species, knowing the first number, arranged with 2. Species, knowing the second number, arranged with 1. Know the third number, arranged with 0. ;
The number of the first bit less than the first digit is x, the second bit is less than the second digit has Y, the third position is 0, so the result of the last permutation is index = x*2!+y*1!.

public class Solution {
 /* * @param a:an array of integers
 * @return: A Long Integer */public
long p Ermutationindex (int[] A) {
    //write your code here
    long index = 0;
    Long _middle = 0;
    Long middle = 1;
    Long num = 0;
    for (int i=0;i<a.length;i++) {for
        (int m=a.length-i-1;m>0;m--)
            middle = middle*m;
        for (int j=i+1;j<a.length;j++) {
            if (A[i]>a[j])
                num=num+1;
        }
        index = index + num*middle;
        num = 0;
        middle = 1;
        _middle = 0;
    }
    return index+1;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.