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. 环形链表
代码实现(自解)
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
代码实现(自解)
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;
}
};