Skip to content

Commit c2d27c5

Browse files
committed
Update code with contain and add code with insert contain checking
1 parent 6992348 commit c2d27c5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

141/with_set.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Solution {
55
set<ListNode*> reached;
66
ListNode* current = head;
77
while (current) {
8-
if (reached.count(current)) {
8+
if (reached.contains(current)) {
99
return true;
1010
}
1111
reached.insert(current);

141/with_set_2.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// insertの返り値を利用して存在確認をする方法
2+
class Solution {
3+
public:
4+
bool hasCycle(ListNode *head) {
5+
set<ListNode*> reached;
6+
ListNode* current = head;
7+
while (current) {
8+
auto [it, inserted] = reached.insert(current);
9+
if (!inserted) { return true; }
10+
current = current->next;
11+
}
12+
return false;
13+
}
14+
};

0 commit comments

Comments
 (0)