标签:链表 val etc pre href while contain c++ for
(LeetCode 817. 链表组件)[https://leetcode-cn.com/problems/linked-list-components/]
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int numComponents(ListNode* head, vector<int>& G) {
set<int> se;
for(auto g:G)
se.insert(g);
int res=0;
ListNode* cur=head;
while(cur)
{
// 判断当前结点的值是否存在于set中
// 若存在,则判断下一个值是否存在,不存在说明存在组件
if(se.find(cur->val)!=se.end()&&(cur->next==NULL||(se.find(cur->next->val)==se.end())))
res++;
cur=cur->next;
}
return res;
}
};
标签:链表 val etc pre href while contain c++ for
原文地址:https://www.cnblogs.com/HurryXin/p/12928192.html