leetcode817 Linked List Components

 1 """
 2 We are given head, the head node of a linked list containing unique integer values.
 3
 4 We are also given the list G, a subset of the values in the linked list.
 5
 6 Return the number of connected components in G, where two values are connected if they appear consecutively in the linked list.
 7
 8 Example 1:
 9
10 Input:
11 head: 0->1->2->3
12 G = [0, 1, 3]
13 Output: 2
14 Explanation:
15 0 and 1 are connected, so [0, 1] and [3] are the two connected components.
16
17 Example 2:
18
19 Input:
20 head: 0->1->2->3->4
21 G = [0, 3, 1, 4]
22 Output: 2
23 Explanation:
24 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.
25
26 """
27 class Solution:
28     def numComponents(self, head, G):
29         set_G = set(G)  #试了用list也能通过,set是为了规范数据集
30         p = head
31         count =0
32         while(p):
33             if p.val in set_G and (p.next == None or p.next!=None and p.next.val not in set_G):
34                 #!!!if条件句关键
35                 #将G中的元素放入set 或者字典中用于查找。
36                 # 遍历链表, 链表中的元素被计数的条件是,
37                 # 如果当前元素在G中且下一个元素不在G中(或者为None),
38                 # 那么当前的元素属于一个component。
39                 count += 1
40             p = p.next
41         return count

原文地址:https://www.cnblogs.com/yawenw/p/12250468.html

时间: 2024-07-30 18:58:47

leetcode817 Linked List Components的相关文章

817. Linked List Components

We are given head, the head node of a linked list containing unique integer values. We are also given the list G, a subset of the values in the linked list. Return the number of connected components in G, where two values are connected if they appear

817. Linked List Components - Medium

We are given head, the head node of a linked list containing unique integer values. We are also given the list G, a subset of the values in the linked list. Return the number of connected components in G, where two values are connected if they appear

#Leetcode# 817. Linked List Components

https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked list containing unique integer values. We are also given the list G, a subset of the values in the linked list. Return the number of connected componen

LeetCode 817. Linked List Components (链表组件)

题目标签:Linked List 题目给了我们一组 linked list, 和一组 G, 让我们找到 G 在 linked list 里有多少组相连的部分. 把G 存入 hashset,遍历 linked list, 利用 hashset 来检查目前的点 和 下一个点 是否在G 里面. 如果目前的点在G里面,下一个点不在,说明这里断开了.具体看code. Java Solution: Runtime:  7 ms, faster than 81 % Memory Usage: 40 MB, l

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

Learning OpenCV Lecture 6 (Extracting Lines,Contours, and Components)

In this chapter, we will cover: Detecting image contours with the Canny operator Detecting lines in images with the Hough transform Fitting a line to a set of points Extracting the components' contours Computing components' shape descriptors Detectin

Facebook React 和 Web Components(Polymer)对比优势和劣势

目录结构 译者前言 Native vs. Compiled 原生语言对决预编译语言 Internal vs. External DSLs 内部与外部 DSLs 的对决 Types of DSLs - explanation DSLs 的种类 - 解释 Data binding 数据绑定 Native vs. VM 原生对决 VM(虚拟机) 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 React 相比 WebComponents 有一些"先天不足"之处,列举

【转】Facebook React 和 Web Components(Polymer)对比优势和劣势

原文转自:http://segmentfault.com/blog/nightire/1190000000753400 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 React 相比 WebComponents有一些“先天不足”之处,列举如下: 原生浏览器支持 原生语法支持(意即不把样式和结构混杂在 JS 中) 使用 Shadow DOM 封装样式 数据的双向绑定 这些都是确然的.不过他还是希望听听大家的看法,于是就有了这篇精彩的回答. 需要说明的是,这篇回答并没有讨

Cannot obtain the required interface ("IID_IDBCreateCommand") from OLE DB provider "OraOLEDB.Oracle" for linked server xxxx

  今天遇到了一个关于LINKED SERVER查询报错的案例,链接服务器链接ORACLE数据库,测试没有错误,但是执行脚本的时候,报如下错误: Msg 7399, Level 16, State 1, Line 1 The OLE DB provider "OraOLEDB.Oracle" for linked server "xxxx" reported an error. Access denied. Msg 7301, Level 16, State 2,