Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
判断数独当前状态是否合法
class Solution
{
public:
bool isValidSudoku(vector>& 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 are indicated by the character '.'
.
You may assume that there will be only one unique solution.
A sudoku puzzle...
...and its solution numbers marked in red.
DFS解数独,保证唯一解
class Solution
{
int col[10][10],row[10][10],box[10][10];
public:
void solveSudoku(vector >& 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 > & 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;
}
};
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
1
is read off as "one 1"
or 11
.11
is read off as "two 1s"
or 21
.21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
模拟过程
class Solution
{
public:
string countAndSay(int n)
{
vectormark[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
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 may be chosen from C unlimited number of times.
Note:
For example, given candidate set 2,3,6,7
and target 7
,
A solution set is: [7]
[2, 2, 3]
输出序列中和=target的种类,可重复使用
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 may only be used once in the combination.
Note:
For example, given candidate set 10,1,2,7,6,1,5
and target 8
,
A solution set is: [1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
同上题,加一个条件,出现的数只能使用一次
class Solution
{
vector >ans;
vectornum;
int key;
int len;
private:
void dfs(vectormark,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> combinationSum2(vector& candidates, int target)
{
len=candidates.size();
num=candidates;
sort(num.begin(),num.end());
key=target;
vectormark;
dfs(mark,0,0,1);
return ans;
}
};