1、两数之和#include <iostream>#include <vector>using namespace std;class Solution { public: vector<int> twoSum(vector<int> &nums, int target) { vector<int> a; for (int i = 0; i < nums.size() - 1; i++) { for (int j = i + 1; j < nums.size(); j++) { if (nums[i] + nums[j] == target) { a.push_back(i); a.push_back(j); return a; } } } return a; //! if之外也要返回vector类型 }}; 7、整数反转#include <math.h>#define INT_MIN -2147483648#define INT_MAX 2147483647class Solution { public: int reverse(int x) { int ans = 0; while (x != 0) { int pop = x % 10; x /= 10; if (ans > INT_MAX / 10 || (ans == INT_MAX / 10 && pop > 7)) return 0; if (ans < INT_MIN / 10 || (ans == INT_MIN / 10 && pop < -8)) return 0; ans = ans * 10 + pop; } return ans; //!!!Solution!!! //!还是看答案看得 //*需要设一个pop来考虑个位数的值,这样才能处理max一个数量级的数据 }}; #include <math.h>#define INT_MIN -2147483648#define INT_MAX 2147483647class Solution { public: int reverse(int x) { int ans = 0; while (x != 0) { int temp = ans * 10 + x % 10; if (temp / 10 != ans) { return 0; } ans = temp; x /= 10; } return ans; //?本地可以得出结果,到了LeetCode就不能了,不知道为何 }}; #include <math.h>#define INT_MIN -2147483648#define INT_MAX 2147483647class Solution { public: int reverse(int x) { int ans = 0; while (x != 0) { int temp = ans * 10 + x % 10; if (temp > (INT_MAX / 10) || temp < (INT_MIN / 10)) { return 0; } ans = temp; x /= 10; } return ans; /* ?虽然可以运行,但是这里的INT_MAX和INT_MIN不能换成-pow(2,31)、pow(2,31)-1,不知道为何 !对于和MAX一个数量级的就不行 */ //现在知道了是要对个位数进行考虑 }}; #include <math.h>#define INT_MIN -2147483648#define INT_MAX 2147483647class Solution { public: int reverse(int x) { int ans = 0; while (x != 0) { ans = ans * 10 + x % 10; x /= 10; } if (-pow(2, 31) < x && x < pow(2, 31) - 1) { return ans; } return 0; //?这个甚至都不能成功运行,似乎是因为pow的原因 }}; 9、 回文数#include <iostream>#include <vector>using namespace std;#include <math.h>#define true 1#define false 0class Solution { public: bool isPalindrome(int x) { /*if (x < 0) { return false; } while (x > 9) { int end = x % 10; int length = log10(x); int temp = x; int digits = 1; for (int i = 0; i < length; i++) { temp /= 10; digits *= 10; } if (temp != end) { return false; } x -= temp * digits; //!这里错了奥,没有考虑第一位之后就是0的情况 x /= 10; } return true;*/ if (x < 0) { return false; } vector<int> a; while (x) { a.push_back(x % 10); x /= 10; //*妈的原来是这里少了一个"="号woc,就nm离谱 } int len = a.size(); for (int i = 0; i < len / 2; i++) { if (a.at(i) != a.at(len - 1 - i)) { a.clear(); vector<int>(a).swap(a); return false; } } a.clear(); vector<int>(a).swap(a); return true; //*速度打败了6.29 %,空间打败了9.1 %,淦,这方法太拉了 //todo: 搞一个更快的方法 }}; 13、罗马数字转整数#include <iostream>#include <string.h>using namespace std;class Solution { public: int tran(char ch) { int t; switch (ch) { case 'I': t = 1; break; case 'V': t = 5; break; case 'X': t = 10; break; case 'L': t = 50; break; case 'C': t = 100; break; case 'D': t = 500; break; case 'M': t = 1000; break; } return t; } int romanToInt(string s) { int ans = 0; for (int i = 0; i < s.length(); i++) { if (i < s.length() - 1 && tran(s.at(i)) < tran(s.at(i + 1))) { ans += tran(s.at(i + 1)) - tran(s.at(i)); i++; } else { ans += tran(s.at(i)); } } return ans; } //*时间打败了95.07 %,空间打败了89.19 %的人,这是目前为止成绩最好的了!};