马上要面试了,把前些天在LeetCode上刷的题再看一遍。
写上注释,算复习了,也方便以后复习。
带 * 号的为忘记怎么做的。
31. 88. 合并两个有序数组(简单数组)
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
// 需要一个临时数组
vector<int> temp(m + n);
int i = 0, j = 0, k = 0;
while (j < m && k < n) {
if (nums1[j] < nums2[k]) {
temp[i++] = nums1[j++];
}
else {
temp[i++] = nums2[k++];
}
}
while (j < m) {
temp[i++] = nums1[j++];
}
while (k < n) {
temp[i++] = nums2[k++];
}
for (int p = 0; p < m + n; p++) {
nums1[p] = temp[p];
}
}
};
32. 414. 第三大的数(top K简单版)
class Solution {
public:
int thirdMax(vector<int>& nums) {
int n = nums.size();
int min = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] < min) min = nums[i];
}
// 初始化3个值为最小
int a = min, b = min, c = min;
for (int i = 0; i < n; i++) {
// 循环一遍后,保证a存最大
if (nums[i] > a) {
c = b;
b = a;
a = nums[i];
}
// b存次大
else if (nums[i] > b && nums[i] != a) {
c = b;
b = nums[i];
}
// c存答案
else if (nums[i] > c && nums[i] != a && nums[i] != b) {
c = nums[i];
}
}
if (c == b || c == a) return a;
else return c;
}
};
33. 167. 两数之和 II - 输入有序数组(哈希表 / 对撞指针)
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int n = numbers.size();
int l = 0, r = n - 1;
vector<int> v;
// 对撞指针
while (l < r) {
if (numbers[l] + numbers[r] == target) {
v.push_back(l + 1);
v.push_back(r + 1);
break;
}
else if (numbers[l] + numbers[r] > target) r--;
else l++;
}
return v;
}
};
/*
class Solution {
public:
// hash表解法
vector<int> twoSum(vector<int>& numbers, int target) {
int n = numbers.size();
map<int, int> mp;
vector<int> a;
for (int i = 0; i < n; i++) {
int key = target - numbers[i];
if (mp.find(key) != mp.end()) {
a.push_back(mp[key]+1);
a.push_back(i+1);
break;
}
else mp[numbers[i]] = i;
}
return a;
}
};
*/
34. 125. 验证回文串(对撞指针)
class Solution {
public:
bool isOk(char ch) {
// 是否是需要考虑的字符
if (ch >= '0' && ch <= '9') return true;
if (ch >= 'a' && ch <= 'z') return true;
if (ch >= 'A' && ch <= 'Z') return true;
return false;
}
bool isEqu(char ch1, char ch2) {
// 是否相等
if (ch1 >= '0' && ch1 <= '9') return ch1 == ch2;
if (ch1 >= 'a' && ch1 <= 'z') ch1 = ch1 + 'A' - 'a';
if (ch2 >= 'a' && ch2 <= 'z') ch2 = ch2 + 'A' - 'a';
return ch1 == ch2;
}
bool isPalindrome(string s) {
int n = s.length();
int left = 0, right = n - 1;
while (left < right) {
// 找到有效字符
while (left < right && !isOk(s[left])) left ++;
while (left < right && !isOk(s[right])) right --;
// 成功碰撞
if (left == right) break;
if (!isEqu(s[left], s[right])) return false;
// 缩小
else left++, right--;
}
return true;
}
};
35. 345. 反转字符串中的元音字母(对撞指针)
class Solution {
public:
bool isYuan(char ch) {
// 是否是元音字母
if (ch == 'a' || ch == 'A') return true;
if (ch == 'e' || ch == 'E') return true;
if (ch == 'i' || ch == 'I') return true;
if (ch == 'o' || ch == 'O') return true;
if (ch == 'u' || ch == 'U') return true;
return false;
}
string reverseVowels(string s) {
int len = s.length();
int left = 0, right = len - 1;
// 对撞指针
while (left < right) {
// 找到有效字符
while (left < right && !isYuan(s[left])) left++;
while (left < right && !isYuan(s[right])) right--;
// 成功对撞
if (left == right) break;
char temp = s[left];
s[left] = s[right];
s[right] = temp;
// 缩小
left++, right--;
}
return s;
}
};
来源:CSDN
作者:dmxjhg
链接:https://blog.csdn.net/liujh_990807/article/details/104079897