1.有序数组问题
1.1 leetcode 15. 三数之和
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int len = nums.size();
vector< vector<int> > res;
sort(nums.begin(),nums.end());
for(int i = 0;i < len;i++)
{
int opp = -nums[i];
if(i==0 || nums[i] != nums[i-1])
{
int left = i+1,right = len-1;
while(left < right)
{
int sum = nums[left] + nums[right];
if(sum == opp)
{
res.push_back({nums[i],nums[left],nums[right]});
while(left < right && nums[left] == nums[left+1])++left;++left;
while(left < right && nums[right] == nums[right-1])--right;--right;
}
else if(sum < opp)
{
left++;
}
else right--;
}
}
}
return res;
}
};
1.2 16. 最接近的三数之和
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int _min = 0x3f3f3f3f;
int ans;
sort(nums.begin(),nums.end());
int len = nums.size();
for(int i = 0;i<len;i++)
{
if(i > 0 && nums[i] == nums[i-1])continue;
int left = i+1,right = len-1;
while(left < right)
{
if(nums[i] + nums[left] + nums[right] == target)
{
return target;
}
else if (nums[i] + nums[left] + nums[right] < target)
{
if(abs(nums[i] + nums[left] + nums[right] - target) < _min)
ans = nums[i] + nums[left] + nums[right] ,_min = abs(nums[i] + nums[left] + nums[right] - target);
left++;
}
else {
if(abs(nums[i] + nums[left] + nums[right] - target) < _min)
ans = nums[i] + nums[left] + nums[right] ,_min = abs(nums[i] + nums[left] + nums[right] - target);
right--;
}
}
}
return ans;
}
};
1.3 18. 四数之和
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int len = nums.size();
vector < vector<int> >res;
sort(nums.begin(),nums.end());
for(int i = 0;i<len;i++)
{
if(i>0 && nums[i] == nums[i-1])continue;
for(int j = i+1;j<len;j++)
{
if(j > i+1 && nums[j] == nums[j-1])continue;
int left = j+1,right = len-1;
while(left < right)
{
if(nums[i] + nums[j] + nums[left] + nums[right] == target)
{
res.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--;
}
else if(nums[i] + nums[j] + nums[left] + nums[right] < target)
{
left++;
}
else right--;
}
}
}
return res;
}
};
1.4 11. 盛最多水的容器
class Solution {
public:
int maxArea(vector<int>& height) {
int l = 0, r = height.size()-1,_max = -1;
while(l<r)
{
_max = max(_max,min(height[l],height[r])*(r-l));
if(height[l] < height[r])++l;
else --r;
}
return _max;
}
};
2.链表问题
141. 环形链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(!head || !head->next)return false;
auto fast = head->next->next,slow = head->next;
while(fast != slow)
{
if(!fast || !slow) return false;
slow = slow->next;
if(!fast->next)return false;
fast = fast->next->next;
}
return true;
}
};
这道题还有一个就是需要找到环的入口,可以设一下变量,轻松推出来做法。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(!head || !head->next)return NULL;
auto slow = head->next,fast = head->next->next;
while(slow != fast)
{
if(!slow || !fast || !fast->next)
return NULL;
fast = fast->next->next;
slow = slow->next;
}
fast = head;
while(slow!=fast)
{
slow = slow->next;
fast = fast->next;
}
return fast;
}
};
19. 删除链表的倒数第N个节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(head==nullptr)return nullptr;
ListNode *i = head, *ii = head;
while(n && ii->next){
ii = ii->next;
n--;
}
if(n==1)return head->next;
while(ii->next)
{
i = i->next;
ii = ii->next;
}
i->next = i->next-> next;
return head;
}
};
两个链表的第一个公共结点
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
int n = 0, m = 0;
auto p = pHead1;
while(p){
n++;
p = p->next;
}
p = pHead2;
while(p){
m++,p = p->next;
}
auto p1 = pHead1,p2 = pHead2;
while(n!=m){
if(n>m){
p1 = p1->next;n--;
}
else{
p2 = p2->next;m--;
}
}
for(int i = 0;i<n;i++){
if(p1->val == p2->val)return p1;
p1 = p1->next,p2 = p2->next;
}
return nullptr;
}
};