【leetcode】1257. Smallest Common Region
题目如下:
You are given some lists of
regionswhere the first region of each list includes all other regions in that list.Naturally, if a region
Xcontains another regionYthenXis bigger thanY.Given two regions
region1,region2, find out the smallest region that contains both of them.If you are given regions
r1,r2andr3such thatr1includesr3, it is guaranteed there is nor2such thatr2includesr3.It's guaranteed the smallest region exists.
Example 1:
Input:
regions = [["Earth","North America","South America"],
["North America","United States","Canada"],
["United States","New York","Boston"],
["Canada","Ontario","Quebec"],
["South America","Brazil"]],
region1 = "Quebec",
region2 = "New York"
Output: "North America"Constraints:
2 <= regions.length <= 10^4region1 != region2- All strings consist of English letters and spaces with at most 20 letters.
解题思路:首先递归找出region1所属的regions链,并保持结果;然后再递归查找region2所属的regions链,找到第一个region在region1所属的regions链即可。
代码如下:
class Solution(object):
def findSmallestRegion(self, regions, region1, region2):
"""
:type regions: List[List[str]]
:type region1: str
:type region2: str
:rtype: str
"""
dic = {}
for region in regions:
parent = region[0]
for i in range(1,len(region)):
dic[region[i]] = parent dic_region1 = {}
while region1 in dic:
dic_region1[region1] = 1
region1 = dic[region1] while region2 in dic:
if region2 in dic_region1:
return region2
region2 = dic[region2]
return regions[0][0]
【leetcode】1257. Smallest Common Region的更多相关文章
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 1257. Smallest Common Region
原题链接在这里:https://leetcode.com/problems/smallest-common-region/ 题目: You are given some lists of region ...
- 【leetcode】302.Smallest Rectangle Enclosing Black Pixels
原题 An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The bl ...
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】910. Smallest Range II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
随机推荐
- 【并行计算-CUDA开发】Windows下opencl环境配置
首先声明我这篇主要是根据下面网站的介绍, 加以修改和详细描述,一步一步在我自己的电脑上实现的, http://www.cmnsoft.com/wordpress/?tag=opencl&pag ...
- python笔记之元祖
元祖创建使用圆括号括起来,中间元素使用逗号隔开 如:tuple1 = (1,2,3,4) tuple2 = () 空元祖 #!/usr/bin/env python #-*-coding:utf-8- ...
- 第六次java实验报告
Java实验报告 班级 计科二班 学号20188437 姓名 何磊 完成时间 2019/10/17 评分等级 实验四 类的继承 实验目的 理解异常的基本概念: 掌握异常处理方法及熟悉常见异常的捕获方法 ...
- [转帖]全方位掌握OpenStack技术知识
全方位掌握OpenStack技术知识 http://www.itpub.net/2019/06/17/2206/ 架构师技术联盟的文章 相当好呢. 大家好,我是小枣君.最近几年,OpenStack这个 ...
- predis操作redis方法大全
//连接redis $redis = new Redis(); $redis->connect("127.0.0.1", 6379); //普通set/get操作 $redi ...
- 使用php过滤emoji表情
/** * 过滤字符串中表情 * @param $str string 昵称 * @return string */ public function filterEmoji($str) { $str ...
- C#派生类的构造函数
构造函数的调用顺序是先调用System.Object,再按照层次结构由上向下(基类=>派生类)进行,直到到达编译器要实例化的类为止.在此过程中,每个构造函数都初始化自己类中的字段.编译器先自下而 ...
- Linux系统定时备份网站文件到七牛云存储脚本
1.七牛云账号注册. 有些朋友可能会问为什么要备份到七牛云?很简单,七牛云免费注册就能获取1G的存储空间,只要简单几步操作完成实名认证即可获取10G的存储空间,对于一般个人站点来说已经足够备份使用了. ...
- linux查看端口被占用情况
Linux 查看端口占用情况可以使用 lsof 和 netstat 命令. 如果linux中没有这两个命令,则yum安装一下 yum install -y lsof yum install -y ne ...
- Action注入错误
2016-07-13 13:52:09,584 [ERROR]-[com.opensymphony.xwork2.util.logging.commons.CommonsLogger:38] Exce ...