字符串转换完成

三数之和待优化
This commit is contained in:
carry 2024-12-28 18:47:03 +08:00
commit 759c5b09ce
3 changed files with 111 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
#仅保留代码文件
*
!.gitignore
!*.java
!*.py
!*.cpp
!*.c
!*.go
!*.js

57
myAtoi.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#include <math.h>
#include <memory>
#include <string>
using namespace std;
string test = "-91283472332";
class Solution
{
public:
int myAtoi(string s)
{
auto i = s.begin();
long long toReturn = 0;
while (*i == ' ') {
i++;
}
auto head = i;
i++;
while ((('0' <= *i) and (*i <= '9'))) {
i++;
}
auto end = i++;
i = head;
if ((('0' <= *i) and (*i <= '9')) or (*i == '-')) {
while ('0' <= *i <= '9') {
i++;
if (i == end) {
break;
}
}
i--;
long long carry = 0;
while (i != head) {
toReturn += pow(10, carry) * ((*i) - 48);
i--;
carry++;
}
if (*i == '-') {
return -toReturn;
} else {
return toReturn + pow(10, carry) * ((*i) - 48);
}
} else {
return 0;
}
}
};
int main()
{
Solution s;
cout << s.myAtoi(test) << endl;
return 0;
}

45
threeSum.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
class Solution
{
public:
vector<vector<int>> threeSum(vector<int> &nums)
{
sort(nums.begin(), nums.end());
set<vector<int>> res;
for(int i = 0; i < nums.size(); i++) {
for(int j = i + 1; j < nums.size(); j++) {
for(int k = nums.size() - 1; k > j ; k--) {
if(nums[i] + nums[j] + nums[k] == 0) {
vector<int> tmp = {nums[i], nums[j], nums[k]};
res.insert(tmp);
}
}
}
}
return vector<vector<int>>(res.begin(), res.end());
}
};
int main()
{
Solution s;
vector<int> nums = {-1, 0, 1, 2, -1, -4};
vector<vector<int>> res = s.threeSum(nums);
for(auto &v : res) {
for(auto &i : v) {
cout << i << " ";
}
cout << endl;
}
return 0;
}