leetcode817 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 consecutively in the linked list. Example 1: Input:
head: 0->1->2->3
G = [0, 1, 3]
Output: 2
Explanation:
0 and 1 are connected, so [0, 1] and [3] are the two connected components. Example 2: Input:
head: 0->1->2->3->4
G = [0, 3, 1, 4]
Output: 2
Explanation:
0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components. """
class Solution:
def numComponents(self, head, G):
set_G = set(G) #试了用list也能通过,set是为了规范数据集
p = head
count =0
while(p):
if p.val in set_G and (p.next == None or p.next!=None and p.next.val not in set_G):
#!!!if条件句关键
#将G中的元素放入set 或者字典中用于查找。
# 遍历链表, 链表中的元素被计数的条件是,
# 如果当前元素在G中且下一个元素不在G中(或者为None),
# 那么当前的元素属于一个component。
count += 1
p = p.next
return count
leetcode817 Linked List Components的更多相关文章
- 817. Linked List Components - LeetCode
Question 817. Linked List Components Solution 题目大意:给一个链表和该链表元素组成的一个子数组,求子数组在链表中组成多少个片段,每个片段中可有多个连续的元 ...
- [Swift]LeetCode817. 链表组件 | Linked List Components
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- [LeetCode] Linked List Components 链表组件
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- (链表 set) leetcode 817. Linked List Components
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- 817. Linked List Components
1. 原始题目 We are given head, the head node of a linked list containing unique integer values. We are a ...
- #Leetcode# 817. Linked List Components
https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 817. Linked List Components (链表组件)
题目标签:Linked List 题目给了我们一组 linked list, 和一组 G, 让我们找到 G 在 linked list 里有多少组相连的部分. 把G 存入 hashset,遍历 lin ...
- 算法与数据结构基础 - 链表(Linked List)
链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...
随机推荐
- 从零构建以太坊(Ethereum)智能合约到项目实战——学习笔记10
P57 .1-Solidity Types - 玩转 Solidity 数组 (Arrays) 学习目标 1.掌握Arrays的可变不可变的创建 2.深度理解可变数组和不可变数组之间的区别 3.二维数 ...
- LeetCode 345. Reverse Vowels of a String(双指针)
题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...
- 「CF1303C Perfect Keyboard」
前置芝士 图的遍历:通过DFS或者BFS遍历全图. 前向星:用来存边,但是在本题用也可以用一个二维数组解决. 具体做法 先从判断YES和NO开始,可以发现如果一个字母与三个及以上不同的字母相邻时肯定是 ...
- Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群
一.集群介绍 根据功能划分为两大类:高可用和负载均衡 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 实现高可用的开源软件有:heartbeat. ...
- redis 之 redis主从复制
Redis集群中的数据库复制是通过主从同步来实现的 主节点(Master)把数据分发给从节点(Slave) 主从同步的好处在于高可用,Redis节点有冗余设计 主从复制的原理:1. 从服务器向主服务器 ...
- 十 用栈解决LeetCode20题括号的匹配
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiIAAACWCAYAAADjcONgAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw
- 吴裕雄--天生自然PYTHON爬虫:爬虫攻防战
我们在开发者模式下不仅可以找到URL.Form Data,还可以在Request headers 中构造浏览器的请求头,封装自己.服务器识别浏览器访问的方法就是判断keywor是否为Request h ...
- python join 和setDaemon 简介
Python多线程编程时,经常会用到join()和setDaemon()方法 1.join ()方法:主线程A中,创建了子线程B,并且在主线程A中调用了B.join(),那么,主线程A会在调用的地方等 ...
- JavaScript图片
<ul> <li> <a href="images/666.jpg" title="frist img">frist< ...
- VUE 父子组件之间通信传值 props和 $emit
1.父组件传值给子组件 $props,子组件传值给父组件 $emit 父组件 <div id="app" > <tr ...