C/C++每日一练(20230508) 相交链表、字符数组、排序链表-LMLPHP

目录

1. 相交链表  🌟🌟

2. 字符数组  ※

3. 排序链表  🌟🌟

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 相交链表

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。

图示两个链表在节点 c1 开始相交

C/C++每日一练(20230508) 相交链表、字符数组、排序链表-LMLPHP

题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构 。

自定义评测:

评测系统 的输入如下(你设计的程序 不适用 此输入):

  • intersectVal - 相交的起始节点的值。如果不存在相交节点,这一值为 0
  • listA - 第一个链表
  • listB - 第二个链表
  • skipA - 在 listA 中(从头节点开始)跳到交叉节点的节点数
  • skipB - 在 listB 中(从头节点开始)跳到交叉节点的节点数

评测系统将根据这些输入创建链式数据结构,并将两个头节点 headA 和 headB 传递给你的程序。如果程序能够正确返回相交节点,那么你的解决方案将被 视作正确答案 。

示例 1:

C/C++每日一练(20230508) 相交链表、字符数组、排序链表-LMLPHP

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,6,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

示例 2:

C/C++每日一练(20230508) 相交链表、字符数组、排序链表-LMLPHP

输入:intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [1,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

C/C++每日一练(20230508) 相交链表、字符数组、排序链表-LMLPHP

输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。

提示:

  • listA 中节点数目为 m
  • listB 中节点数目为 n
  • 1 <= m, n <= 3 * 10^4
  • 1 <= Node.val <= 10^5
  • 0 <= skipA <= m
  • 0 <= skipB <= n
  • 如果 listA 和 listB 没有交点,intersectVal 为 0
  • 如果 listA 和 listB 有交点,intersectVal == listA[skipA] == listB[skipB]

进阶:你能否设计一个时间复杂度 O(m + n) 、仅用 O(1) 内存的解决方案?

出处:

https://edu.csdn.net/practice/27308139

代码:

#include <bits/stdc++.h>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution
{
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)
    {
        if (!headA || !headB)
        {
            return NULL;
        }
        ListNode *cur1 = headA;
        ListNode *cur2 = headB;
        while (cur1 != cur2)
        {
            cur1 = cur1 ? cur1->next : headB;
            cur2 = cur2 ? cur2->next : headA;
        }
        return cur1;
    }
};

ListNode* buildNodeList(vector<int> vec) {
    ListNode *head = new ListNode(0);
    ListNode *p = head;
    for (size_t i = 0; i < vec.size(); i++) {
        ListNode *node = new ListNode(vec[i]);
        p->next = node;
        p = p->next;
    }
    return head->next;
}

void testIntersection(ListNode *headA, ListNode *headB, int skipA, int skipB){
    ListNode *p = headA, *q = headB;
    for (int i = 0; i < skipA; i++)
    	p = p->next;
    for (int i = 0; i < skipB; i++)
    	q = q->next;
    if (p != NULL && q->next != NULL)
    	q->next = p; 
    Solution sol;
    ListNode *res = sol.getIntersectionNode(headA, headB);
    if (res != NULL)
    	cout << "Intersected at: " << res->val << endl;
    else
        cout << "null" << endl;
}

int main()
{
    vector<int> listA = {4,1,8,4,5};
    vector<int> listB = {5,0,1,8,4,5};
    int skipA = 2, skipB = 3;
    ListNode *headA = buildNodeList(listA);
    ListNode *headB = buildNodeList(listB);
	testIntersection(headA,headB,skipA, skipB);
	
    listA = {0,9,1,2,4};
    listB = {3,2,4};
    skipA = 3; skipB = 1;
    headA = buildNodeList(listA);
    headB = buildNodeList(listB);
	testIntersection(headA,headB,skipA, skipB);

    listA = {2,6,4};
    listB = {1,5};
    skipA = 3; skipB = 2;
    headA = buildNodeList(listA);
    headB = buildNodeList(listB);
	testIntersection(headA,headB,skipA, skipB);

    return 0;
}

输出:

Intersected at: 8
Intersected at: 2
null


2. 字符数组

编写一个以两个字符数组作为输入的函数。
如果第二个数组包含在第一个数组中,则函数返回第一个数组中第二个数组开始的第一个索引。
如果第二个数组不被包含在第一个数组,然后函数应该return -1
输入 [’c’,’a’,’l’,’l’,’i’,’n’,’g’] 和 [’a’,’l’,’l’] 就 return 1.
输入 [’c’,’a’,’l’,’l’,’i’,’n’,’g’] 和 [’a’,’n’] 就 return -1.

以下程序实现了这一功能,请你补全空白处内容:

```c++
#include <iostream>
#include <string>
using namespace std;
int main()
{
    char a[128], b[128];
    int numA, numB;
    cout << "请输入第一个数组元素个数:";
    cin >> numA;
    cout << "请输入第一个数组元素:";
    for (int i = 0; i < numA; ++i)
        cin >> a[i];
    cin.clear();
    cin.sync();
    cout << "请输入第二个数组元素个数:";
    cin >> numB;
    cout << "请输入第二个数组元素:";
    for (int i = 0; i < numB; ++i)
        cin >> b[i];
    int num = 0;
    string index;
    for (int j = 0; j < numB; j++)
    {
        for (int k = 0; k < numA; k++)
        {
            if (b[j] == a[k])
            {
                __________________;
            }
        }
    }
    if (num == numB)
    {
        cout << "第二个数组包含在第一个数组中" << endl;
        cout << "第一个数组中第二个数组开始的第一个索引为:" << index.substr(0, 1) << endl;
    }
    else
        cout << "第二个数组不被包含在第一个数组";
    system("pause");
    return 0;
}
```

出处:

https://edu.csdn.net/practice/27308140

代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    char a[128], b[128];
    int numA, numB;
    cout << "请输入第一个数组元素个数:";
    cin >> numA;
    cout << "请输入第一个数组元素:";
    for (int i = 0; i < numA; ++i)
        cin >> a[i];
    cin.clear();
    cin.sync();
    cout << "请输入第二个数组元素个数:";
    cin >> numB;
    cout << "请输入第二个数组元素:";
    for (int i = 0; i < numB; ++i)
        cin >> b[i];
    int num = 0;
    string index;
    for (int j = 0; j < numB; j++)
    {
        for (int k = 0; k < numA; k++)
        {
            if (b[j] == a[k])
            {
                index += to_string(k);
                num++;
                break;
            }
        }
    }
    if (num == numB)
    {
        cout << "第二个数组包含在第一个数组中" << endl;
        cout << "第一个数组中第二个数组开始的第一个索引为:" << index.substr(0, 1) << endl;
    }
    else
        cout << "第二个数组不被包含在第一个数组";
    system("pause");
    return 0;
}

输出:


3. 排序链表

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。

进阶:

  • 你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?

示例 1:

C/C++每日一练(20230508) 相交链表、字符数组、排序链表-LMLPHP

输入:head = [4,2,1,3]
输出:[1,2,3,4]

示例 2:

C/C++每日一练(20230508) 相交链表、字符数组、排序链表-LMLPHP

输入:head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]

示例 3:

输入:head = []
输出:[]

提示:

  • 链表中节点的数目在范围 [0, 5 * 10^4] 内
  • -10^5 <= Node.val <= 10^5

出处:

https://edu.csdn.net/practice/27308141

代码:

#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
    ListNode *sortList(ListNode *head)
    {
        return mergesort(head);
    }
    ListNode *mergesort(ListNode *node)
    {
        if (!node || !node->next)
            return node;
        ListNode *fast = node;
        ListNode *slow = node;
        ListNode *brek = node;
        while (fast && fast->next)
        {
            fast = fast->next->next;
            brek = slow;
            slow = slow->next;
        }
        brek->next = nullptr;
        ListNode *l1 = mergesort(node);
        ListNode *l2 = mergesort(slow);
        return merge(l1, l2);
    }
    ListNode *merge(ListNode *l1, ListNode *l2)
    {
        if (l1 == NULL)
        {
            return l2;
        }
        if (l2 == NULL)
        {
            return l1;
        }
        if (l1->val < l2->val)
        {
            l1->next = merge(l1->next, l2);
            return l1;
        }
        else
        {
            l2->next = merge(l2->next, l1);
            return l2;
        }
    }
};

ListNode* buildNodeList(vector<int> vec) {
    ListNode *head = new ListNode(0);
    ListNode *p = head;
    for (size_t i = 0; i < vec.size(); i++) {
        ListNode *node = new ListNode(vec[i]);
        p->next = node;
        p = p->next;
    }
    return head->next;
}

string NodeList2String(ListNode *head) {
	if (head==nullptr) return "[]";
    ListNode *p = head;
	string res;
    while (p != nullptr) {
        res.append(to_string(p->val));
        res.append("->");
        p = p->next;
    }
    res.append("null");
    return res;
}

int main()
{
	Solution s;
    vector<int> nums = {4,2,1,3};
    ListNode *head = buildNodeList(nums);
	cout << NodeList2String(head) << endl;
	head = s.sortList(head);
	cout << NodeList2String(head) << endl;
	
    nums = {-1,5,3,4,0};
    head = buildNodeList(nums);
	cout << NodeList2String(head) << endl;
	head = s.sortList(head);
	cout << NodeList2String(head) << endl;
	
	return 0;
}

输出:

4->2->1->3->null
1->2->3->4->null
-1->5->3->4->0->null
-1->0->3->4->5->null


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/

05-09 02:17