LeetCode日记2020.2.1
继续链表
817 链表组件(mid)
线性扫描,利用set减少查找时间。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int numComponents(ListNode* head, vector<int>& G) {
set<int> gSet;
for(const auto& g:G)
gSet.insert(g);
int cnt = 0;
bool flag = false;
while(head!=NULL)
{
if(gSet.find(head->val)!=gSet.end())
{
if(!flag)
{
flag = true;
++cnt;
}
}
else
flag = false;
head = head -> next;
}
return cnt;
}
};
86 分隔链表(mid)
注意初始化迭代过程中小于x和大于等于x的两个子链表的末端指针,否则迭代无法正确进行。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* dummyH = new ListNode(-1);
dummyH->next = head;
//初始化两个子链表的末端指针
ListNode* bigEnd = dummyH;
while(bigEnd->next!=NULL&&bigEnd->next->val < x)
bigEnd=bigEnd->next;
ListNode* smallEnd = bigEnd;
ListNode* smallEndNext = bigEnd->next;
while(bigEnd->next != NULL)
{
if(bigEnd->next->val < x)
{
smallEnd->next = bigEnd->next;
bigEnd->next = bigEnd->next->next;
smallEnd->next->next = smallEndNext;
smallEnd = smallEnd -> next;
}
else
bigEnd = bigEnd->next;
}
return dummyH->next;
}
};
143 重排链表(mid)
尝试利用递归解法解决以锻炼思维。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
if(head==NULL)
return;
fast = head;
slow = head;
recursion(head);
End->next = NULL;
}
void recursion(ListNode* head)
{
if(fast!=NULL&&fast->next!=NULL)
{
fast = fast->next->next;
slow = slow->next;
recursion(head->next);
auto temp = head->next;
head->next = slow;
slow=slow->next;
head->next->next = temp;
}
else
{
End = slow;
if(fast!=NULL)
slow=slow->next;
}
}
ListNode *fast, *slow, *End;
};
445 两数相加2(mid)
可以用栈避免反转链表,也可以在一定程度上代替链表递归,编程更简单不容易出错。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> s1, s2;
while(l1!=NULL)
{
s1.push(l1->val);
l1 = l1->next;
}
while(l2!=NULL)
{
s2.push(l2->val);
l2=l2->next;
}
int carry = 0;
ListNode* dummyH = new ListNode(-1);
while(!s1.empty()&&!s2.empty())
{
int sum = s1.top()+s2.top()+carry;
s1.pop();
s2.pop();
carry = sum/10;
sum%=10;
auto temp = dummyH->next;
dummyH->next = new ListNode(sum);
dummyH->next->next = temp;
}
stack<int>& s = s1.empty()?s2:s1;
while(!s.empty())
{
int sum=s.top()+carry;
s.pop();
carry = sum/10;
sum%=10;
auto temp = dummyH->next;
dummyH->next = new ListNode(sum);
dummyH->next->next = temp;
}
if(carry!=0)
{
auto temp = dummyH->next;
dummyH->next = new ListNode(carry);
dummyH->next->next = temp;
}
return dummyH->next;
}
};
725 分割链表(mid)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* root, int k) {
int total =0;
auto tempRoot = root;
while(tempRoot!=NULL)
{
++total;
tempRoot=tempRoot->next;
}
int avr = total / k;
vector<int> sizes(k,avr);
int rem = total%k;
if(rem!=0)
{
for(int i=0;i<rem;++i)
++sizes[i];
}
vector<ListNode*> heads;
for(int i=0;i<k;++i)
{
heads.push_back(root);
ListNode* End=root;
for(int j=0;j<sizes[i];++j)
{
if(j==sizes[i]-1)
End = root;
root = root->next;
}
if(End!=NULL)
End->next = NULL;
}
return heads;
}
};