1460. 通过翻转子数组使两个数组相等![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/78940e851e41daf91f648d4d7e50d5b5.png)
代码实现(自解)
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. 环形链表
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/823f620ea419f643cbde83530226aedc.png)
代码实现(自解)
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
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8c2a84b8b0bd4063a08d3b9b65ddf20f.png)
代码实现(自解)
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;
}
};