杨辉三角
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> vv;
vv.resize(numRows);
for(size_t i = 0; i<vv.size();++i)
{
/0层开1个空间 1 2
vv[i].resize(i+1,0);
/每一层的头尾初始化成1
vv[i][0]=1;
vv[i][vv[i].size()-1] =1;
}
/ i j 等于上一层 i-1 j-1 + i-1 j
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]+vv[i-1][j-1];
}
}
}
return vv;
}
};
字符串相加
class Solution {
public:
string addStrings(string num1, string num2) {
int end1 = num1.size()-1;
int end2 = num2.size()-1;
int value1 = 0, value2 = 0, next = 0;
string addret;
while(end1 >= 0 || end2 >= 0)
{
/先转成数字
if(end1 >= 0)
value1 = num1[end1--]-'0';
else
value1 = 0;
if(end2 >= 0)
value2 = num2[end2--]-'0';
else
alue2 = 0;
int valueret = value1 + value2 + next; /求和
if(valueret > 9)
{
next = 1;
valueret -= 10;
}
else
{
next = 0;
}
addret += (valueret+'0');/赋值给对象
}
if(next == 1) /处理最后的进位1
{
addret += '1';
}
reverse(addret.begin(), addret.end());/算法逆置
return addret;
};