微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

365天挑战LeetCode1000题——Day 067 通过翻转子数组使两个数组相等 环形链表

1460. 通过翻转子数组使两个数组相等

在这里插入图片描述

代码实现(自解)

class Solution {
public:
    bool canBeEqual(vector<int>& target, vector<int>& arr) {
        sort(target.begin(), target.end());
        sort(arr.begin(), arr.end());
        return target == arr;
    }
};

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) {
        ListNode* slow_ptr = head, *fast_ptr = head;
        while (fast_ptr && fast_ptr->next) {
            slow_ptr = slow_ptr->next;
            fast_ptr = fast_ptr->next->next;
            if (slow_ptr != NULL && slow_ptr == fast_ptr) return true;
        }
        return false;
    }
};

142. 环形链表 II

在这里插入图片描述

代码实现(自解)

/**
 * 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) {
        set<ListNode*> st;
        while (head) {
            if (st.count(head)) return head;
            st.emplace(head);
            head = head->next;
        }
        return NULL;
    }
};

原文地址:https://www.jb51.cc/wenti/3285968.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐