A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1].

Sets S[K] for 0 <= K < N are defined as follows:

S[K] = { A[K], A[A[K]], A[A[A[K]]], ... }.

Sets S[K] are finite for each K and should NOT contain duplicates.

Write a function that given an array A consisting of N integers, return the size of the largest set S[K] for this array.

Example 1:

Input: A = [5,4,0,3,1,6,2]
Output: 4
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:

  1. N is an integer within the range [1, 20,000].
  2. The elements of A are all distinct.
  3. Each element of array A is an integer within the range [0, N-1].

思路:

感觉类似于求集合的概念。根据给出的例子,可以发现,5 6 2 0这四个数字无论是从0开始还是从2开始,始终是这四个数字为一个集合,于是就可以用一个标记用来表示

是否已经遍历过该数字,比如从0开始,依次找到A[0], A[5], A[6], A[2],将他们依次做标记,就可以避免重复遍历。

int arrayNesting(vector<int>& nums)
{
int n = nums.size();
vector<int>flags(n,false);
int res =;
int num =;
for(int i =;i<n;i++)
{
if(flags[i] == true)continue;
num = ;
for(int j = i;flags[j]==false;)
{
num++;
flags[j] = true;
j = nums[j];
}
res = max(res,num);
}
return res;
}

[leetcode-565-Array Nesting]的更多相关文章

  1. [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 ...

  2. 【leetcode】565. Array Nesting

    You are given an integer array nums of length n where nums is a permutation of the numbers in the ra ...

  3. 【LeetCode】565. Array Nesting 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 565. Array Nesting

    Problem statement: A zero-indexed array A consisting of N different integers is given. The array con ...

  5. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  6. [LeetCode] Array Nesting 数组嵌套

    A zero-indexed array A consisting of N different integers is given. The array contains all integers ...

  7. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  8. [LeetCode] Patching Array 补丁数组

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  9. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  10. LeetCode Patching Array

    原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...

随机推荐

  1. 【转】Java方向如何准备BAT技术面试答案(汇总版)

    原文地址:http://www.jianshu.com/p/1f1d3193d9e3 这个主题的内容之前分三个篇幅分享过,导致网络上传播的比较分散,所以本篇做了一个汇总,同时对部分内容及答案做了修改, ...

  2. PHP 中级内容

    1.面向对象编程(OOP) 2.模板引擎(smarty) 3.MYSQL(中级操作) 数据库抽象层(PDO): 4.Ajax(异步刷新) 5.Js框架(jQuery) Xml+JSON数据       ...

  3. @JsonFormat 日期格式自动格式化

    通常日期格式都是以时间戳的形式存放在数据库里,当前端页面通过接口查询时,我们会将一个对象的某些属性查出来返回给页面. 例如,某个类里面有个属性 Timestamp create_time 给这个对象实 ...

  4. Day5模块-random模块

    random:随机数 >>> import random>>> print(random.random()) #生成随机小数0.6906362176182085 & ...

  5. Ubuntu 挂载硬盘分区

    1.先查看当前硬盘分区状态,命令sudo fdisk -l 大致如下:设备 启动 Start 末尾 扇区 Size Id 类型/dev/sda1 2048 206847 204800 100M 7 H ...

  6. C语言习题1.分别统计一下其中字母,数字,其他字符的个数。将统计的字母,数字,其他字符的个数以柱状图的形式打印

    从键盘上输入字符,(1)分别统计一下其中字母,数字,其他字符的个数, (2)将统计的字母,数字,其他字符的个数以柱状图的形式打印.例如 5 ***** *****     3 *****   **** ...

  7. Ajax01 什么是ajax、获取ajax对象、ajax对象的属性和方法

    1 什么是ajax ajax是一种用来改善用户体验的技术,其本质是利用浏览器提供的一个对象(XMLHttpRequest,也可称之为ajax对象) 向服务器发送异步请求;服务器返回部分数据(不是一个完 ...

  8. [原创]安全系列之端口敲门服务(Port Knocking for Ubuntu 14.04 Server)

    Port Knocking for Ubuntu 14.04 Server OS:ubuntu 14.04 server 原理简单分析: 端口敲门服务,即:knockd服务.该服务通过动态的添加ipt ...

  9. sparklyr包:实现Spark与R的接口

    日前,Rstudio公司发布了sparklyr包.该包具有以下几个功能: 实现R与Spark的连接—sparklyr包提供了一个完整的dplyr后端 筛选并聚合Spark数据集,接着在R中实现分析与可 ...

  10. 项目管理之 SVN 管理软件 CornerStone for Mac

    常用的项目管理有 Git 和 SVN.之前公司一直使用的是 Git,使用的是 SourceTree 客户端,据说 Git 比 SVN 要好,只能说各有特点吧,有兴趣的可以查看下两个的区别. 下面是学习 ...