Leetcode题解(25)
77. Combinations
题目

分析:求给定数字n,k的组合数,方法是采用深度搜索算法,代码如下(copy网上代码)
class Solution {
public:
void dfs77(vector<vector<int > > &ans, vector<int> subans, int start, int n, int k)
{
if (subans.size() == k)
{
ans.push_back(subans); return ;
}
for (int i = start; i <= n; i++)
{
subans.push_back(i);
dfs77(ans, subans, i + , n, k);
subans.pop_back(); // 满足一个条件或者该分支递归完后要删除最后一个
}
}
vector<vector<int> > combine(int n, int k) {
vector<vector<int > > ans;
if (n < k || k == ) return ans;
vector<int> subans;
dfs77(ans, subans, , n, k);
return ans;
}
};
--------------------------------------------------------------------------------分割线------------------------------------------------------------------
78. Subsets
题目

分析:求一个集合的所有子集,代码如下(copy网上代码)
class Solution {
public:
vector<vector<int>>res;
vector<int>ans;
vector<vector<int>> subsets(vector<int>& nums) {
if(nums.empty()) return res;
sort(nums.begin(), nums.end());
dfs(, ans, nums);
return res;
}
void dfs(int k, vector<int>ans, vector<int> nums){
res.push_back(ans);
for(int i = k; i < nums.size(); i++){
ans.push_back(nums[i]);
dfs(i + , ans, nums);
ans.pop_back();
}
}
};
--------------------------------------------------------------------------------------分割线------------------------------------------------------------
79. Word Search
题目

分析:看到这类题目,首先就应该想到“回溯法”,代码如下,具体思路见注释:
class Solution {
public:
int m,n;
public:
bool exist(vector<vector<char>>& board, string word) {
if(""==word)
return true;
m=board.size();
n=board[].size();
vector<vector<bool>> flag(m,vector<bool>(n,false));
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
if(board[i][j]==word[])
{
flag[i][j]=true;
if(HuiSu(board,i,j,word,,flag))
return true;
else
flag[i][j]=false;
}
}
}
return false;
}
bool HuiSu(const vector<vector<char>>& board,int i,int j,const string& word,int index,vector<vector<bool>>& flag)
{
if(word[index+] == '\0')
return true;
int ii,jj;
ii=i;
jj=j+;
bool temp;
if(isTrue(ii,jj))//向右
{
if(flag[ii][jj] == false && board[ii][jj] == word[index+])
{
flag[ii][jj] = true;
temp = HuiSu(board,ii,jj,word,index+,flag);
if(temp)
return true;
flag[ii][jj] = false;//这一步很重要
}
}
ii=i;
jj=j-;
if(isTrue(ii,jj))//向左
{
if(flag[ii][jj] == false && board[ii][jj] == word[index+])
{
flag[ii][jj] = true;
temp = HuiSu(board,ii,jj,word,index+,flag);
if(temp)
return true;
flag[ii][jj] = false;
}
}
ii=i-;
jj=j;
if(isTrue(ii,jj))//向上
{
if(flag[ii][jj] == false && board[ii][jj] == word[index+])
{
flag[ii][jj] = true;
temp = HuiSu(board,ii,jj,word,index+,flag);
if(temp)
return true;
flag[ii][jj] = false;
}
}
ii=i+;
jj=j;
if(isTrue(ii,jj))//向下
{
if(flag[ii][jj] == false && board[ii][jj] == word[index+])
{
flag[ii][jj] = true;
temp = HuiSu(board,ii,jj,word,index+,flag);
if(temp)
return true;
flag[ii][jj] = false;
}
}
return false;
}
bool isTrue(int i,int j)
{
if(i<)
return false;
else if(i>=m)
return false;
else if(j<)
return false;
else if(j>=n)
return false;
return true;
}
};
Leetcode题解(25)的更多相关文章
- [LeetCode题解]: Sort Colors
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- [LeetCode 题解]: Maximum Subarray
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Find the c ...
- [LeetCode 题解]:Gas Station
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 There are ...
- [LeetCode 题解]: plusOne
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a no ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
随机推荐
- win7系统Myeclipse下切换SVN用户
Eclipse的SVN插件Subclipse做得很好,在svn操作方面提供了很强大丰富的功能.但到目前为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,而且一旦用户的帐号.密码保存之后 ...
- Safe Area Layout Guide
原文:Safe Area Layout Guide Apple在iOS 7中引入了topLayoutGuide和bottomLayoutGuide作为UIViewController属性.它们允许您创 ...
- C++11获取线程的返回值
C++11 std::future and std::promise 在许多时候,我们会有这样的需求--即我们想要得到线程返回的值. 但是在C++11 多线程中我们注意到,std::thread对象会 ...
- [SDOI2009]HH的项链解题报告
原题目:洛谷P1972 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此 ...
- Java 数组扩容
在添加数据到达数组的上限的时候数组进行扩容: public void resizeArrayCaptcity(){ if(size>=arr.length){ Emp [] arr2=new ...
- Linux Bash Shell字符串截取
#!/bin/bash#定义变量赋值时等号两边不能有空格,否则会报命令不存在 # 运行shell脚本两种方式 # 1.作为解释参数 /bin/sh test.sh ; 2.作为可执行文件 chmo ...
- [bzoj1059] [ZJOI2007] 矩阵游戏 (二分图匹配)
小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏--矩阵游戏.矩阵游戏在一个N *N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换操作:选 ...
- Springmvc+Spring+Mybatis实现员工登录注册功能
ssm实现用户注册以及登录功能..转载请标明出处 http://www.cnblogs.com/smfx1314/p/smfx1314.html 前端bootstrap 所使用的IDE是eclips ...
- 易语言关于使用CURL,网页_访问,网页_访问S,网页_访问_对象,鱼刺(winHttpW)发送Get性能测试
易语言关于使用 CURL,网页_访问,网页_访问S,网页_访问_对象,鱼刺(winHttpW)发送Get性能测试 测试模块情况: |-精易模块5.8 |-鱼刺类Http |-libCURL +++ ...
- 使用svn与maven管理的项目导入Eclipse,但是与本地svn客户端关联不上?
因为这个问题,导致我的项目导了删,删了导.现在终于弄明白了. 首先,需求场景是: 1.使用svn进行版本控制; 2.使用maven进行项目管理. 3.使用Tortoise svn将项 ...