Leetcode 36-40 Questions of solving code

Source: Internet
Author: User

Valid Sudoku

Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.

The Sudoku board could be partially filled, where empty cells is filled with the character ‘.‘ .


A partially filled sudoku which is valid.

Determine if the current state of Sudoku is legal


Class Solution {public:    bool Isvalidsudoku (vector<vector<char>>& board)     {        int col[10][ 10],ROW[10][10],BOX[10][10];        int i,j,x,a;        memset (col,0,sizeof (col));        memset (row,0,sizeof (Row));        memset (box,0,sizeof (box));        for (i=0;i<9;i++) for            (j=0;j<9;j++)                if (board[i][j]!= '. ')                {                    x=board[i][j]-' 0 ';                    A=I/3*3+J/3;                    if (row[i][x]==1 | | col[j][x]==1 | | box[a][x]==1) return false;                    else row[i][x]=col[j][x]=box[a][x]=1;                }                        return true;    }};

Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells is indicated by the character ‘.‘ .

Assume that there would be is only one unique solution.


A Sudoku Puzzle ...


... and its solution numbers marked in red.

DFS coefficient alone, guaranteeing the unique solution

Class Solution{int col[10][10],row[10][10],box[10][10];p ublic:void solvesudoku (vector<vector<char> >        & board) {memset (col,0,sizeof (col));        memset (row,0,sizeof (row));        memset (box,0,sizeof (box));            for (int i=0;i<9;i++) for (int j=0;j<9;j++) if (board[i][j]!= '. ')                {col[j][board[i][j]-' 0 ']=1;                row[i][board[i][j]-' 0 ']=1;            box[i/3*3+j/3][board[i][j]-' 0 ']=1;    } dfs (board,0);        } bool Dfs (vector<vector<char> > & board,int key) {int i,j;        if (key==81) return true;        int X=KEY/9;        int y=key%9;            if (board[x][y]!= '. ')        Return Dfs (BOARD,KEY+1);            else {int A=X/3*3+Y/3;                for (int i=1;i<=9;i++) if (col[y][i]==0 && row[x][i]==0 && box[a][i]==0) {                Col[y][i]=row[x][i]=box[a][i]=1; board[x][y]=i+ ' 0 ';                if (Dfs (board,key+1)) return true;                col[y][i]=row[x][i]=box[a][i]=0;            Board[x][y]= '. ';    }} return false; }};

Count and Say 

The Count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1is read off as"one 1"Or11.
11is read off as"two 1s"Or21.
21is read off as"one 2, thenone 1"Or1211.

Given an integer n, generate the nth sequence.

Note:the sequence of integers would be represented as a string.

Simulation process

Class Solution {public:    string Countandsay (int n)    {        vector<int>mark[n+1];        int temp;        char ch;        Mark[1].push_back (1);        string ans;        int i,sum,j;        for (i=2;i<=n;i++)        {            temp=mark[i-1][0];            Sum=1;            For (J=1;j<mark[i-1].size (); j + +)            if (mark[i-1][j]!=temp)            {                mark[i].push_back (sum);                Mark[i].push_back (temp);                TEMP=MARK[I-1][J];                sum=1;            }            else    sum++;            Mark[i].push_back (sum);            Mark[i].push_back (temp);        }        Ans= "";        For (I=0;i<mark[n].size (); i++)        {            ch=mark[n][i]+ ' 0 ';            ans=ans+ch;        }        return ans;    };

Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C Where the candidate numbers sums to T.

The same repeated number is chosen from C unlimited number of times.

Note:

  • All numbers (including target) would be positive integers.
  • elements in a combination (a 1 ,  a 2 , ...,  a k ) must is in non-descending order. (Ie, a 1  ≤ a 2  ≤ ... ≤ a k ).
  • The solution set must not contain duplicate combinations.

For example, given candidate set2,3,6,7and target7,
A Solution set is:
[7]
[2, 2, 3]

Types of output sequences and =target, reusable


Combination Sum II 

Given A collection of candidate numbers (C) and a target number (T), find all unique combinations in c where the candidate numbers sums to T.

Each number in C is used once in the combination.

Note:

  • All numbers (including target) would be positive integers.
  • elements in a combination (a 1 ,  a 2 , ...,  a k ) must is in non-descending order. (Ie, a 1  ≤ a 2  ≤ ... ≤ a k ).
  • The solution set must not contain duplicate combinations.

For example, given candidate set10,1,2,7,6,1,5and target8,
A Solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]

Above, add a condition, the number of occurrences can only be used once

Class solution {vector<vector<int> >ans;vector<int>num;int key;int len;private:void dfs (vector< Int>mark,int n,int Sum,int used) {    if (Sum==key)    {        ans.push_back (mark);        return;    }    if (N==len) return;    if (Sum+num[n]>key) return;    DFS (mark,n+1,sum,0);    if (num[n]==num[n-1] && used==0) return;    Mark.push_back (Num[n]);    DFS (mark,n+1,sum+num[n],1);    Mark.pop_back ();} Public:vector<vector<int>> combinationSum2 (vector<int>& candidates, int target) {    len= Candidates.size ();    Num=candidates;    Sort (Num.begin (), Num.end ());    Key=target;        vector<int>mark;    DFS (mark,0,0,1);    return ans;};




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 36-40 Questions of solving code

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.