Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
KJ-Falloutlast committed Jan 26, 2023
1 parent 641d963 commit 451e9b8
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cpp/Algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
- [12-7.电话号码的字母组合](#12-7电话号码的字母组合)
- [12-8.组合问题](#12-8组合问题)
- [12-9.分割回文串](#12-9分割回文串)
- [12-10.N皇后问题](#12-10n皇后问题)
- [12-10.解数独问题](#12-10解数独问题)

# 1.时间复杂度和空间复杂度
## 1-1.概念
Expand Down
120 changes: 120 additions & 0 deletions cpp/cpp_learn.md
Original file line number Diff line number Diff line change
Expand Up @@ -15254,6 +15254,7 @@ void print(const T& firstArg, const Types&... args) {
*/


<<<<<<< Updated upstream
template <typename... Types>
void print(const Types&... args) {
std::cout << "print(...)" << std::endl;
Expand Down Expand Up @@ -15307,3 +15308,122 @@ int main(int argc, char *argv[]) {
2. queue = [0, 1, 2, 3] (head = 0, tail = 3, 插入在3, 删除在0)
3. stack = [0, 1, 2, 3] (head = 0, tail = 3, 插入和删除在3)

=======
```

## 21.字符串的加减法
```cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

string addString(string s1, string s2) {
int i = s1.length() - 1;
int j = s2.length() - 1;
int add = 0;
string ss = "";
while (i >= 0 || j >= 0|| add != 0) {
if (i >= 0)
add = add + s1[i--] - '0';
if (j >= 0)
add = add + s2[j--] - '0';
ss = ss + to_string(add % 10);
add = add / 10;
}
reverse(ss.begin(), ss.end());
return ss;
}
//大数减法
string subString(string s1,string s2){
bool minus = false;
if( s1.size() < s2.size() || ( s1.size() == s2.size() && s1 < s2 ) ){
swap(s1,s2);
minus = true;
}
int i = s1.size() - 1;
int j = s2.size() - 1;
int flag = 0;
string ans = "";
while( i >= 0 && j >= 0 ){
int tmp = 0;
if( s1[i] >= s2[j] ){
tmp = s1[i] - s2[j];
tmp = tmp - flag;
ans += to_string(tmp);
flag = 0;
}
else{
tmp = s1[i] - s2[j] + 10;
tmp = tmp - flag;
ans += to_string(tmp);
flag = 1;
}
i--;
j--;
}
// 处理较大数的剩余部分
while( i >= 0 ){
if(flag == 0){
ans += s1[i];
}
else{
int tmp = s1[i] - '0' - flag;
ans += to_string(tmp);
flag = 0;
}
i--;
}
// 翻转
reverse(ans.begin(),ans.end());
//去除前导 0
int k = 0;
while( k < ans.size() && ans[k] == '0') k++;
if( k == ans.size() ){
ans = "0";
}
else ans = ans.substr(k);
// 结果是否为负
return minus?"-" + ans:ans;
}
//使用大小为 n + m 的数组,num1的第i位乘以num2的第j位,结果
// 对应存放在数组的i+j+1的位置。
string multiplyString(string num1, string num2) {
int n = num1.size();
int m = num2.size();
vector<int> result(n + m,0);
for(int i = n - 1; i >= 0; i--)
{
for(int j = m - 1;j >= 0; j--)
{
int tmp = (num1[i] - '0') * (num2[j] - '0');
tmp += result[i + j + 1];
result[i + j] += tmp / 10; //注意 这里是 +=
result[i + j + 1] = tmp % 10;
}
}
int i = 0;
while( i < n + m && result[i] == 0 )
{
i++;
}
string ans;
for(; i < n + m; i++){
ans.push_back(result[i] + '0');
}
return ans.size() == 0 ? "0" : ans;
}
int main()
{
string s1 = "abc";
string s2 = "a";
string s_add = addString(s1, s2);
string s_sub = subString(s1, s2);
string s_mul = multiplyString(s1, s2);
cout << s_add << endl;
cout << s_sub << endl;
cout << s_mul << endl;
}
```
>>>>>>> Stashed changes

0 comments on commit 451e9b8

Please sign in to comment.