前言
一个本硕双非的小菜鸡,备战24年秋招,计划刷完hot100和剑指Offer的刷题计划,加油!
根据要求,每一道题都要写出两种以上的解题技巧。
一、15. 三数之和(HOT100)
15. 三数之和
Note:双指针解法。首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。如果nums[i] + nums[left] + nums[right] > 0 就说明 此时三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,这样才能让三数之和小一些。
如果 nums[i] + nums[left] + nums[right] < 0 说明此时三数之和小了,left 就向右移动,才能让三数之和大一些,直到left与right相遇为止。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> res;
for (int i = 0; i < nums.size(); i++) {
if (nums[0] > 0) return res;
if (i > 0 && nums[i] == nums[i - 1]) continue;
int left = i + 1;
int right = nums.size() - 1;
while (left < right) {
if (nums[left] + nums[right] + nums[i] > 0)
right--;
else if (nums[left] + nums[right] + nums[i] < 0)
left++;
else {
res.push_back(vector<int> {nums[left], nums[right], nums[i]});
while (left < right && nums[right] == nums[right - 1])
right--;
while (left < right && nums[left] == nums[left + 1])
left++;
right--;
left++;
}
}
}
return res;
}
};
Note:哈希解法。两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在数组里出现过。但是难点在于去重,并不推荐。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
// 找出a + b + c = 0
// a = nums[i], b = nums[j], c = -(a + b)
for (int i = 0; i < nums.size(); i++) {
// 排序之后如果第一个元素已经大于零,那么不可能凑成三元组
if (nums[i] > 0) {
break;
}
if (i > 0 && nums[i] == nums[i - 1]) { //三元组元素a去重
continue;
}
unordered_set<int> set;
for (int j = i + 1; j < nums.size(); j++) {
if (j > i + 2
&& nums[j] == nums[j-1]
&& nums[j-1] == nums[j-2]) { // 三元组元素b去重
continue;
}
int c = 0 - (nums[i] + nums[j]);
if (set.find(c) != set.end()) {
result.push_back({nums[i], nums[j], c});
set.erase(c);// 三元组元素c去重
} else {
set.insert(nums[j]);
}
}
}
return result;
}
};
二、13. 机器人的运动范围(剑指Offer)
Note:回溯方法,通过使用辅助二维数组记录该数字是否被使用过了
class Solution {
public:
int getSum(pair<int, int> p) {
int s = 0;
while (p.first) {
s += p.first % 10;
p.first /= 10;
}
while (p.second) {
s += p.second % 10;
p.second /= 10;
}
return s;
}
int movingCount(int threshold, int rows, int cols)
{
if (threshold < 0 || rows <= 0 || cols <= 0)
return 0;
queue<pair<int, int>> q;
vector<vector<bool>> st(rows, vector<bool>(cols, false));
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int res = 0;
q.push({0, 0});
while (q.size()) {
auto t = q.front();
q.pop();
if (st[t.first][t.second] || getSum(t) > threshold) continue;
res++;
st[t.first][t.second] = true;
for (int i = 0; i < 4; i++) {
int x = t.first + dx[i], y = t.second + dy[i];
if (x >= 0 && x < rows && y >= 0 && y < cols)
q.push({x, y});
}
}
return res;
}
};
Note:同样是回溯,采用了另外的写法
class Solution {
public:
vector<vector<bool>> st;
int res = 0;
int getSum(pair<int, int> p) {
int s = 0;
while (p.first) {
s += p.first % 10;
p.first /= 10;
}
while (p.second) {
s += p.second % 10;
p.second /= 10;
}
return s;
}
void dfs(int x, int y, int threshold, int rows, int cols) {
if (x < 0 || x > rows - 1 || y < 0 || y > cols - 1 || st[x][y])
return;
if (!st[x][y]) {
st[x][y] = true;
if (getSum({x, y}) <= threshold)
res++;
else
return;
}
dfs(x + 1, y, threshold, rows, cols);
dfs(x - 1, y, threshold, rows, cols);
dfs(x, y + 1, threshold, rows, cols);
dfs(x, y - 1, threshold, rows, cols);
}
int movingCount(int threshold, int rows, int cols)
{
if (threshold < 0 || rows <= 0 || cols <= 0)
return 0;
st = vector<vector<bool>> (rows, vector<bool>(cols, false));
dfs(0, 0, threshold, rows, cols);
return res;
}
};
总结
祝大家都能学有所成,找到一份好工作!
1317

被折叠的 条评论
为什么被折叠?



