《左耳听风》-ARTS-打卡记录-第十八周
Algorithm
题目:
合并两个排序的链表
解题历程:
1.一开始,自己首先想到的是循环.尝试用链表1的指针去遍历去指向后一个比它大的元素,发现这样存在问题:如下图,链表1和2都有个元素"1",那链表1的指针p1去指向链表2中的元素"1"时,链表1会断掉,因为没有指针指向链表1后边的元素了.
2.后来考虑新建一个链表,然后将两个链表中的元素去添加到新链表中.这种方法乍一想还挺靠谱的,后来验证了下,发现新的链表最后会多一个值为0的结点,也满足不了要求(具体的实现代码如下). 这是因为需要先建后一个结点,才能把当前结点的next指向后一个结点.
看来这种思路也不正确,后来看了<剑指offer>中的解题思路,在第3点中展开讲述.
// Definition for singly-linked list.
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if((nullptr == l1) && (nullptr == l2))
{
return nullptr;
}
else
{
if(nullptr == l1)
{
return l2;
}
if(nullptr == l2)
{
return l1;
}
//how to sort the list?
ListNode* pListNode1 = l1;
ListNode* pListNode2 = l2;
ListNode *pList1 = new ListNode();
ListNode *pList = pList1;
while((nullptr != pListNode1) && (nullptr != pListNode2))
{
if (pListNode1->val < pListNode2->val)
{
pList->val = pListNode1->val;
pListNode1 = pListNode1->next;
}
else
{
pList->val = pListNode2->val;
pListNode2 = pListNode2->next;
}
pList->next = new ListNode();
pList = pList->next;
}
while(nullptr != pListNode1)
{
pList->val = pListNode1->val;
pListNode1 = pListNode1->next;
pList->next = new ListNode();
pList = pList->next;
}
while(nullptr != pListNode2)
{
pList->val = pListNode2->val;
pListNode2 = pListNode2->next;
pList->next = new ListNode();//新建后一个结点,并把当前结点的next指向它.
pList = pList->next;
}
return pList1;
}
}
};
3.书中采用了递归的思想:
如下图所示,用第三个指针p指向两个链表中最小的,然后把剩余的链表当做一个整体.然后外面的元素指向剩余链表中最小的元素,如此重复,这就是递归的思想了.
代码如下,很简短,这就是递归的魅力.
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if((nullptr == l1) && (nullptr == l2))
{
return nullptr;
}
else
{
if(nullptr == l1)
{
return l2;
}
if(nullptr == l2)
{
return l1;
}
ListNode* pNode;
if (l1->val <= l2->val)
{
pNode = l1;
l1 = l1->next;
}
else
{
pNode = l2;
l2 = l2->next;
}
pNode->next = mergeTwoLists(l1, l2);//当前结点的next指向函数返回的新结点
return pNode;
}
}
};
作者:stephen-uf
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/solution/di-gui-si-xiang-by-stephen-uf-udvc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Review
The XY Problem(XY问题)
See Also: Asking Smart Questions
参见:如何问聪明的问题
What is it?
The XY problem is asking about your attempted solution rather than your actual problem. This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.
-
User wants to do X.
-
User doesn’t know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.
-
User doesn’t know how to do Y either.
-
User asks for help with Y.
-
Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve.
-
After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn’t even a suitable solution for X.
The problem occurs when people get stuck on what they believe is the solution and are unable step back and explain the issue in full.
这是什么?
XY问题是问你打算做的解决方案,而不是你真正的问题.这会导致巨大的时间和精力的浪费,无论是提问的人还是回答问题的人.
- 用户想要做X.
- 用户不知道如何做X,但它们认为它们可以通过做Y来得到一个解决方案.
- 用户也不知如何做Y.
- 用户提问如何做Y.
- 别人帮助用户去做Y,但困惑因为Y看起来像个奇怪的问题.
- 经过多次交互和花费时间,终于搞清楚用户想要在X上寻求帮助,而且Y不是适用于X的的方案.
这个问题发生在当人们被他们深信不疑的解决方案困住的时候,并且他们不能够退一步去完整地解释这个问题.
What to do about it?
- Always include information about a broader picture along with any attempted solution.
- If someone asks for more information, do provide details.
- If there are other solutions you’ve already ruled out, share why you’ve ruled them out. This gives more information about your requirements.
Remember that if your diagnostic theories were accurate, you wouldn’t be asking for help right?
关于这个要做些什么
- 在介绍一个在尝试的解决方案时,总是要包含更大背景信息.
- 如果别人问详情,去提供一些细节.
- 如果一些其他的解决方案被你排除了,分享为什么你把它们给排除在外.这将给你的需求提供更多的信息.
记住如果你的诊断理论是精准的,那么你不正在请求正确的帮助吗?
Examples
Example 1
n00b doesn’t actually want the last 3 chracters in a filename, he wants the file extensions, so why ask for the last 3 characters?
n00b并不是真的想要一个文件的最后三个单词,他想要文件的扩展名,所以为什么问最后3个字符串呢.
<n00b> How can I echo the last three characters in a filename?
<feline> If they're in a variable: echo ${foo: -3}
<feline> Why 3 characters? What do you REALLY want?
<feline> Do you want the extension?
<n00b> Yes.
<feline> There's no guarantee that every filename will have a three-letter extension,
<feline> so blindly grabbing three characters does not solve the problem.
<feline> echo ${foo##*.}
Example 2
If Angela had just started by explaining she wants to prevent others from detecting her OS, this could have been a much shorter and more productive discussion.
如果Angela 一开始就讲清楚她想要防止别人识别到她的OS,这将会有一个更简短更搞笑的交谈.
Angela: 'nmap -O -A 127.0.0.1' returns some lines starting with 'OS:'. How to change it?
Obama: Look in the sourcecode for nmap, find how it figures out the Linux part, then rewrite your TCP/IP stack to not operate in a way nmap can detect.
Angela: Yeah, but I don't know about linux system api at all.
Obama: Well, nmap's fingerprint is based on the way the TCP/IP stack works, there's no real way except to rewrite the appropriate parts of said stack.
Angela: I really need to avoid these messages. Can iptables do this work?
Obama: Well, don't use OS detection or version scanning
Angela: I want to prevent others from knowing the type of my OS
Tips
1.选择珍视自己的价值,每个人都是独一无二的,都是世上的pearl.
2.对于已经下了决定去做的事情,要多想它的好处,做一个Benefit Finder.