🌈C++专栏: 南桥谈C++
🌈C语言专栏: C语言学习系列
🌈Linux学习专栏: 南桥谈Linux
🌈数据结构学习专栏: 数据结构杂谈
🌈数据库学习专栏: 南桥谈MySQL
🌈Qt学习专栏: 南桥谈Qt
🌈菜鸡代码练习: 练习随想记录
🌈git学习: 南桥谈Git
283.移动零
283.移动零
class Solution {
public:void moveZeroes(vector<int>& nums) {int n = nums.size();int cur = 0;int dest = 0; // 已经处理区间内非0元素的位置// [0,dest](全是非0元素) [dest+1,cur-1](全是0元素) [cur,n-1](待处理元素)while (cur < n) {if (nums[cur]) {swap(nums[dest], nums[cur]);dest++;cur++;} else {cur++;}}}
};
1089.复写零
1089.复写零
class Solution {
public:void duplicateZeros(vector<int>& arr) {int n = arr.size();int dist = -1, cur = 0;// 先判断cur的位置while (cur < n) {if (arr[cur]) {dist++;} else {dist += 2;}if (dist >= n - 1)break;cur++;}if (dist == n) {arr[n - 1] = 0;cur--;dist -= 2;}while (cur >= 0) {if (arr[cur]) {arr[dist] = arr[cur];cur--;dist--;} else {arr[dist--] = 0;arr[dist--] = 0;cur--;}}}
};
202.快乐数
202.快乐数
class Solution {
public:int solve(int n) {int sum = 0;while (n) {int t = n % 10;sum += t * t;n /= 10;}return sum;}bool isHappy(int n) {int slow = n, fast = solve(n);while (slow != fast) {slow = solve(slow);fast = solve(solve(fast));}return slow == 1;}
};
11.盛最多水的容器
11.盛最多水的容器
class Solution {
public:int maxArea(vector<int>& height) {int n=height.size();int left=0,right=n-1;int ans=0,ret=0;while(left!=right){ans=min(height[left],height[right])*(right-left);ret=max(ans,ret);if(height[left]>=height[right]) right--;else left++;}return ret;}
};
611.有效三角形的个数
611.有效三角形的个数
判断三角形方法:a+b>c&&a+c>b&&b+c>a
但是这种判断方法需要判断三次
更加优化的方法:三个数是排好序的,a<b<c,只需要判断a+b>c成立与否
class Solution {
public:int triangleNumber(vector<int>& nums) {int n = nums.size();sort(nums.begin(), nums.end());int ans = 0;for (int i = n - 1; i >= 2; i--) {int left = 0, right = i - 1;while (left != right) {if (nums[left] + nums[right] > nums[i]) {ans += right - left;right--;} else {left++;}}}return ans;}
};
15.三数之和
15.三数之和
class Solution {
public:vector<vector<int>> threeSum(vector<int>& nums) {int n = nums.size();sort(nums.begin(), nums.end());vector<vector<int>> ans;for (int i = 0; i < n; i++) {if (nums[i] > 0)return ans;if (i > 0 && nums[i] == nums[i - 1])continue;int left = i + 1, right = n - 1;int t = -nums[i];while (left < right) {if (nums[left] + nums[right] < t)left++;else if (nums[left] + nums[right] > t)right--;else {ans.push_back({nums[i], nums[left], nums[right]});while (right > left && nums[right - 1] == nums[right])right--;while (right > left && nums[left + 1] == nums[left])left++;left++;right--;}}}return ans;}
};
18.四数之和
18.四数之和
class Solution {typedef long long LL;public:vector<vector<int>> fourSum(vector<int>& nums, int target) {int n = nums.size();sort(nums.begin(), nums.end());vector<vector<int>> ans;for (int i = 0; i < n; i++) {if (i > 0 && nums[i] == nums[i - 1])continue;for (int j = i + 1; j < n; j++) {if (j > i + 1 && nums[j] == nums[j - 1])continue;int left = j + 1, right = n - 1;LL t = (LL)target - nums[i] - nums[j];while (left < right) {if (nums[left] + nums[right] < t)left++;else if (nums[left] + nums[right] > t)right--;else {ans.push_back({nums[i], nums[j], nums[left], nums[right]});while (left < right && nums[left] == nums[left + 1])left++;while (left < right && nums[right] == nums[right - 1])right--;left++;right--;}}}}return ans;}
};