21. Merge Two Sorted Lists
题目描述: 合并两个有序链表
解题思路: 水题
AC代码:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
ans = None
new = None
while l1 or l2:
if not l1:
if not new:
new = ListNode(0)
ans = new
else:
new.next = ListNode(0)
new = new.next
new.val = l2.val
l2 = l2.next
elif not l2:
if not new:
new = ListNode(0)
ans = new
else:
new.next = ListNode(0)
new = new.next
new.val = l1.val
l1 = l1.next
elif not l1 and not l2: return new
elif l1.val >= l2.val:
if not new:
new = ListNode(0)
ans = new
else:
new.next = ListNode(0)
new = new.next
new.val = l2.val
l2 = l2.next
elif l1.val < l2.val:
if not new:
new = ListNode(0)
ans = new
else:
new.next = ListNode(0)
new = new.next
new.val = l1.val
l1 = l1.next
return ans
23. Merge k Sorted Lists
题目描述: 合并K个有序链表
解题思路: 比较难,刚开始用合并两个有序数组的方法来做,果断超时,做了太多的重复计算。后来看到有人说优先队列,于是只要知道优先队列就很简单了,然后去看了一下STL的priority_queue,代码就很好写了,用优先队列保存每个链表的当前位置,然后就出队进队就行了。
AC代码:
class Solution {
public:
struct cmp{
bool operator ()(ListNode *a,ListNode *b){
return a->val >= b->val; //最小值优先
}
};
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<ListNode*,vector<ListNode*>,cmp>que;
for (int i = 0; i < lists.size() ; i ++ )
if(lists[i])
que.push(lists[i]);
ListNode* head = NULL,*tail = NULL,*tmp = NULL;
while(!que.empty())
{
tmp = que.top();
que.pop();
if (head == NULL)
{
head = new ListNode(tmp->val);
tail = head;
}
else
{
tail->next = new ListNode(tmp->val);
tail = tail->next;
}
if(tmp->next)
que.push(tmp->next);
}
return head;
}
};
24. Swap Nodes in Pairs
题目描述: 把链表的每两个元素调换个位置
解题思路: 水题,指针注意好就行了
AC代码:
class Solution {
public:
struct cmp{
bool operator ()(ListNode *a,ListNode *b){
return a->val >= b->val; //最小值优先
}
};
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<ListNode*,vector<ListNode*>,cmp>que;
for (int i = 0; i < lists.size() ; i ++ )
if(lists[i])
que.push(lists[i]);
ListNode* head = NULL,*tail = NULL,*tmp = NULL;
while(!que.empty())
{
tmp = que.top();
que.pop();
if (head == NULL)
{
head = new ListNode(tmp->val);
tail = head;
}
else
{
tail->next = new ListNode(tmp->val);
tail = tail->next;
}
if(tmp->next)
que.push(tmp->next);
}
return head;
}
};
25. Reverse Nodes in k-Group
题目描述: 把链表的每K个元素的位置颠倒,上一题的升级版
解题思路: 难点就在于掌握好每个指针,每组的队首队尾都要记录,还要记录上一组的队尾,然后就按照倒置一条链表的思路,用头插法倒置K个节点,再把当前组和上一组串起来。只要注意好每个指针,思路其实很简单,花了一个两个小时,才顺利写好修改指针的代码。
AC代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode*grp = head,*sta = head,*fst = head,*mid = head ,*end = head;
int cnt=1;
if (k == 1) return head;
while(end && cnt < k)
{
end = end->next;
cnt ++;
}
if(cnt == k && end)
{
cnt = 1;
end = head->next;
}
else
return head;
while(end)
{
mid = end;
if (end != NULL)
end = end->next;
if (cnt == 1)
sta = fst;
mid->next = fst;
fst = mid;
cnt ++;
if (cnt == k)
{
cnt = 1;
if (sta == head)
head = fst;
else
{
grp->next = fst;
grp = sta;
}
fst = end;
mid = end;
int tmp = 1;
while(end && tmp < k)
{
end = end->next;
tmp ++;
}
if (tmp == k && end)
end = mid->next;
else
{grp->next = fst;return head;}
}
}
if (grp == head)
head = fst;
else
{
grp->next = fst;
sta->next = NULL;
}
return head;
}
};
26. Remove Duplicates from Sorted Array
题目描述: 移除数组重复元素
解题思路: 水题
AC代码:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() == 0) return 0;
int i = 0 ,j;
for (j = 0; j < nums.size() ; j ++)
{
if (nums[i] != nums[j])
{
nums[++i] = nums[j];
}
}
return i+1;
}
};
27. Remove Element
题目描述: 给定一个数组和一个值K,移除数组中所有的K
解题思路: 水题
AC代码:
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
length = len(nums)-nums.count(val)
while val in nums:
nums.remove(val)
return length
28. Implement strStr()
题目描述: 定位模式串在正文中的位置
解题思路: KMP入门题
AC代码:
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle: return 0
next_ = [0,0]
p = 0
for i in range(1,len(needle)):
p = next_[i]
while p and needle[p] != needle[i]: p = next_[p]
if needle[p] == needle[i] : next_.append(p+1)
else: next_.append(0)
# print(next_)
p = 0
for i in range(len(haystack)):
while p and needle[p] != haystack[i]: p = next_[p]
if needle[p] == haystack[i]: p += 1
if p == len(needle): return i-p+1
return -1