leetcode 题解代码整理 36-40题
2015-08-19 23:01
645 查看
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
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
A partially filled sudoku which is valid.
判断数独当前状态是否合法
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.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
A sudoku puzzle...
![](http://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png)
...and its solution numbers marked in red.
DFS解数独,保证唯一解
The count-and-say sequence is the sequence of integers beginning as follows:
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
模拟过程
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:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2,
… , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤
… ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set
A solution set is:
输出序列中和=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:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2,
… , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤
… ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set
A solution set is:
同上题,加一个条件,出现的数只能使用一次
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
'.'.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
A partially filled sudoku which is valid.
判断数独当前状态是否合法
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 are indicated by the character
'.'.
You may assume that there will be only one unique solution.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
A sudoku puzzle...
![](http://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png)
...and its solution numbers marked in red.
DFS解数独,保证唯一解
class Solution { int col[10][10],row[10][10],box[10][10]; public: 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"or
11.
11is read off as
"two 1s"or
21.
21is 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) { 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 .size();i++) { ch=mark [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 may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2,
… , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤
… ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set
2,3,6,7and target
7,
A solution set is:
[7]
[2, 2, 3]
输出序列中和=target的种类,可重复使用
Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C wherethe candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2,
… , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤
… ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set
10,1,2,7,6,1,5and target
8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
同上题,加一个条件,出现的数只能使用一次
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 >key) return ; dfs(mark,n+1,sum,0); if (num ==num[n-1] && used==0) return ; mark.push_back(num ); dfs(mark,n+1,sum+num ,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; } };
相关文章推荐
- uva 11090 Going in Cycle!!
- Java泛型总结
- Eclipse插件使用links目录的用法
- 直接在sublime test3中编译Java源程序
- Java的内存回收机制
- Spring & 配置
- 关于编程语言未来的12个预测(仁者见,仁智者见智,不喜勿喷谢谢)
- 使用 Python 为 KVM 编写脚本,第 1 部分: libvirt
- Java集合的遍历方法
- C#相关知识
- Ubuntu安装Sun JDK及如何设置默认java JDK
- Java中标识接口的作用
- C语言:内存地址分析 & sizeof和strlen用法总结
- C++11 第二章
- maven学习
- Ubuntu下python3 安装 pip 和 Django
- 转:java中include动作和include指令的区别
- 最短路径弗洛伊德算法C语言实现__Floyd
- Java基础再进阶学习总结2015(一)
- 设计模式实例(Lua)笔记之二(AbstractFactory抽象工厂模式)