[leetcode]565. Array Nesting

来源:互联网 发布:java购物商城源码 编辑:程序博客网 时间:2024/06/05 23:46

题目链接:https://leetcode.com/problems/array-nesting/#/description


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: 4Explanation: 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].


class Solution {public:    int arrayNesting(vector<int>& nums) {        int res=0;        for(int i=0;i<nums.size();i++)        {            if(nums[i]!=INT32_MAX)            {                int start=nums[i],count=0;                while(nums[start]!=INT32_MAX)                {                    int temp=start;                    start=nums[start];                    count++;                    nums[temp]=INT32_MAX;                }                res=max(res,count);            }        }        return res;    }};

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 储蓄卡被冻结怎么办 百度云qq登录怎么办 微信发红包忘记密码怎么办 电脑wps表格打不开怎么办 电脑wps文字打不开怎么办 电脑的wps打不开怎么办 qq锁异地登录怎么办 手机玩游游戏太热怎么办 运动秀注册不了怎么办 应用卸载不掉怎么办 aweme卸载不掉怎么办 win10系统安装失败怎么办 英雄联盟自动更新失败怎么办 英雄联盟老出错怎么办? lol下载0kb怎么办 手机md5校验失败怎么办 王者荣耀占内存怎么办 信用卡被暂停使用怎么办 宝宝爱涂鸦怎么办呢 大良老板欠薪怎么办 投影仪流明很低怎么办 户外涂鸦没有墙怎么办 word突然未响应怎么办 文档突然未响应怎么办 pr不自己播放怎么办 笔记本电脑空格键打字失灵怎么办 小孩受凉打喷嚏流鼻涕怎么办 头疼打喷嚏流鼻涕鼻塞怎么办 头疼打喷嚏想吐怎么办 感冒鼻塞头疼打喷嚏怎么办 孕期打喷嚏漏尿怎么办 孕妇打喷嚏漏尿怎么办 打喷嚏停不下来怎么办 鼻炎不停的打喷嚏怎么办 感冒了鼻子痒怎么办 怀孕鼻炎严重了怎么办 鼻炎不停打喷嚏流鼻涕怎么办 鼻子里面干痒怎么办 有鼻炎老打喷嚏怎么办 鼻子喷嚏打不停怎么办 鼻子痒想打喷嚏怎么办