从0开始的Leetcode 做题记录
丑陋的代码
第一天
两数之和
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int i = 0;
int j = 0;
while (1) {
int sum;
if (i==j)
{
j=i+1;
}
sum = nums[i] + nums[j];
if (sum == target) {
vector<int> result;
result.push_back(i);
result.push_back(j);
return result;
break;
} else {
if(j==(nums.size()-1)){
i++;
j=0;
}else{
j++;
}
}
}
}
};
394ms 12.61MB 难绷🤣
回文数
class Solution {
public:
bool isPalindrome(int x) {
auto str = std::to_string(x);
bool result = false;
for (int i = 0, j = 0; i < (str.size() + 1) / 2; i++) {
if (str[i] == str[str.size() - i - 1]) // 12341234 8
{
result = true;
} else {
result = false;
break;
}
if (i == (str.size() + 1) / 2) {
break;
}
}
return result;
}
};
11ms 10.25MB 又是难绷的代码