【LeetCode】565. Array Nesting 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/search-a-2d-matrix/description/
题目描述
A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], … } subjected to the rule below.
Suppose the first element in S starts with the selection of element A[i] of index = i, the next element in S should be A[A[i]], and then A[A[A[i]]]… By that analogy, we stop adding right before a duplicate element occurs in S.
Example 1:
Input: A = [5,4,0,3,1,6,2]
Output: 6
Explanation:
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.
One of the longest S[K]:
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
Note:
- N is an integer within the range [1, 20,000].
- The elements of A are all distinct.
- Each element of A is an integer within the range [0, N-1].
题目大意
给出了一个数组,找出从任意位置出发,把该位置的数字当做下一个索引的位置,最后肯定会终止于环路。找出最长的环长。
解题方法
本身思路很简单,就是用一个数组来保存某个位置是否被访问过,如果被访问过说明就是成了一个环,终止并记录最大环长。
直接这么做会超时,一个很机智的做法是,把visited数组放到for循环的外边,这样可以当新的环路计算的时候,如果以前的环访问过该位置的话,就不再计算了。道理是,给出的数组nums的数字范围是0~N-1,也就是说没有重复的数字,那么前面访问过的一个串的长度不会小于后面。
比如题目中给出的例子,在对以index = 0开始的串进行遍历的时候,会对0,5,6,2这几个位置进行标记过已经访问了。当index = 5,6,2时,以index开头的串的长度不会超过以0开头的串的长度。
A = [5,4,0,3,1,6,2]
One of the longest S[K]:
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
代码:
class Solution(object):
def arrayNesting(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# 放在这里
visited = [False] * len(nums)
ans = 0
for i in xrange(len(nums)):
road = 0
while not visited[i]:
road += 1
# 下面两行的顺序不能变
visited[i] = True
i = nums[i]
ans = max(ans, road)
return ans
C++代码如下:
class Solution {
public:
int arrayNesting(vector<int>& nums) {
int res = 0;
const int N = nums.size();
vector<bool> visited(N, false);
for (int i = 0; i < N; i ++) {
int path = 0;
while (!visited[i]) {
visited[i] = true;
path += 1;
i = nums[i];
}
res = max(res, path);
}
return res;
}
};
日期
2018 年 3 月 6 日
2018 年 12 月 15 日 —— 今天四六级
【LeetCode】565. Array Nesting 解题报告(Python & C++)的更多相关文章
- [LeetCode] 565. Array Nesting 数组嵌套
A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- 表格table的宽度问题
首先注意table的一个样式 table { table-layout:fixed; } table-layout有以下取值: automatic 默认.列宽度由单元格内容设定 fixed 列宽由表格 ...
- 单元测试在Unity中的应用
项目描述:简单演示单元测试在Unity中的应用 项目地址:UnityTestRunner_Tutorial - SouthBegonia 项目版本:2020.3.20f1 项目用法:打开就用,代码都放 ...
- linux 常用查看命令
linux 常用查看命令 目录 linux 常用查看命令 linux 查看内存/进程-ps/top linux 查看磁盘存储-df linux 查看io读写-iotop linux 查看端口占用-ne ...
- linux下怎么查看某个命令属于哪个包
# yum whatprovides */ip 或者 # yum provides */ip 即可
- 虚机扩大容量与vm减少所占容量
Linux的虚拟机碎片整理 sudo dd if=/dev/zero of=/free bs=1M sudo rm -f /free 镜像压缩 移动镜像 VBoxManage internalcomm ...
- Redis数据类型内部编码规则及优化方式
Redis的每个键值都是使用一个redisObject结构体保存的,redisObject的定义如下: typedef struct redisObject { unsigned type:4; un ...
- LVS nat模型+dr模型
nat模型 在 rs1 和 rs2 安装httpd 并配置测试页,启动 [root@rs1 ~]# yum install httpd -y[root@rs1 ~]# echo "thi ...
- HTML DOM 对象 - 方法和属性
一些常用的 HTML DOM 方法: getElementById(id) - 获取带有指定 id 的节点(元素) appendChild(node) - 插入新的子节点(元素) removeChil ...
- AOP切入点的配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Nginx+ uWSGI +django进行部署
一:uWSGI的安装 sudo pip install uwsgi 如果安装报错: conda install -c conda-forge uwsgi conda install -c conda- ...