【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 ...
随机推荐
- Mysql的delimiter
告诉MySQL解释器,该段命令是否已经结束了,mysql是否可以执行了.默认情况下,delimiter是分号;.在命令行客户端中,如果有一行命令以分号结束,那么回车后,mysql将会执行该命令. 有时 ...
- EXCEl-数据透视表按照自定义序列排序
用着感觉挺神奇,也有点奇怪,可能不是很懂里边的原理吧.最后,需要排序的列,应该在数据透视表首列才有效. 参考:https://jingyan.baidu.com/article/bea41d43a53 ...
- jQuery ajax常用示例
总结一下jQuery ajax常用示例 $.ajax({ type: "post", //类型get,post url: urls, //链接地址 data:{"id&q ...
- SIG -MESH -1
协议栈 node:成为蓝牙mesh网络中一员的设备被称为节点(Node). 蓝牙mesh规格定义了节点可能拥有的特性.具有这些特性中的一个或多个,即表示节点可以在网络中扮演相应的特殊角色.定义的 ...
- Demo04分解质因数
package 习题集1;import java.util.Scanner;//将一个正整数分解质因数.例如输入90,打印出90=2*3*3*5public class Demo04 { public ...
- 巩固javaweb的第二十二天
巩固内容: 使用表单数据 : 要对用户输入的信息进行验证,需要先获取输入信息.每个表单元素都属于一个 form 表单,要获取信息,需要先获取 form,然后访问表单元素的值. 有两种方式可以获取 fo ...
- Identity Server 4 从入门到落地(七)—— 控制台客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- ALitum技巧
创建异型焊盘的方法 SCH与PCB同步修改后元器件乱跑的解决方法 Altium 在PCB重新编号更新到SCH原理图的方法 同步问题 其他技巧: 当前层亮色,其他层灰色切换:SHIFT+S
- HelloWorldModelMBean
package mbeanTest; import java.lang.reflect.Constructor; import javax.management.Descriptor; import ...
- 'this' pointer in C++
The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is avai ...