电话号码的字母组合
class Solution {
string _num[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public:
void Combinations(const string& digits,int di,string numcom,vector<string>& v)
{
if(di == digits.size())//结束条件
{
v.push_back(numcom);
return ;
}
int num = digits[di] - '0';
string str = _num[num];
for(auto ch : str)
{
Combinations(digits,di+1,numcom+ch,v);//递归一定要注意numcom是+
//不是+=;
}
}
vector<string> letterCombinations(string digits) {
vector<string> v;
if(digits.size() == 0)
{
return {};
}
int di = 0;
Combinations(digits,di,"",v);
return v;
}
};
杨辉三角
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> vv;
vv.resize(numRows,vector<int>());//进行初始化
//进行的是每行初始化,因为这里表示的是顺序表里面是个顺序表
for(int i = 0; i < vv.size(); i++)//初始化没列
{
vv[i].resize(i+1,0);
vv[i][0] = vv[i][vv[i].size() - 1] = 1;
}
for(int i = 0 ;i < vv.size(); i++)
{
for(int j = 0; j < vv[i].size(); j++)
{
if(vv[i][j] == 0)
{
vv[i][j] = vv[i-1][j-1] + vv[i-1][j];
}
}
}
return vv;
}
};