Array Nesting


题目描述:

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].

题目大意:

给定一个数组nums。题目要我们求出新的一个数组S。求S的规则就是:S[K] = { A[K], A[A[K]], A[A[A[K]]], ... }.最后返回数组S中最大的元素即可。
我们可以直接模拟,提交后发现运行超时。
手动模拟一下给出的示例:

A = [5,4,0,3,1,6,2]S[0]={5,6,2,0};
S[1]={4,1};
S[2]={0,5,2,6};
S[3]={3};
S[4]={1,4};
S[5]={6,2,0,5};
S[6]={2,0,5,6};

我们发现,其实在算出S[0]的长度后,计算S[2],S[5],S[6]都重复了操作。因为nums数组中的数字是在0~n-1之间的(n是nums数组的长度)。所以每一个S元素都会形成一个环。也就是说,如果当前的元素在之前的环中出现过,那么就不必再计算以该元素开头的S序列的长度了。这样势必会减少大量的操作。
这里用vis数组初始化为0表示元素没有在之前的环中出现过,等于1表示已经出现过在之前的环中。


题目代码:

class Solution {
public:int arrayNesting(vector<int>& nums) {int maxSize = INT_MIN;int size = 0;int vis[nums.size()] = {0};for(int i = 0; i < nums.size(); i++){size = 0;int next = i;while(!vis[next]){vis[next] = 1;size++;next = nums[next];}maxSize = max(maxSize, size);}return maxSize;}
};

LeetCode 565. Array Nesting相关推荐

  1. [LeetCode] 565. Array Nesting

    565. Array Nesting 这道题目的大概意思是,先选定数组中一个起始的位置,再根据她的值定位到相应的下标,继续下去,直到出现循环为止,最后找出最长的不循环的. 显然需要将数组中每个位置起始 ...

  2. Leetcode 565. Array Nesting

    题目 链接:https://leetcode.com/problems/array-nesting/ Level: Medium Discription: A zero-indexed array A ...

  3. Leetcode——565. Array Nesting

    题目原址 https://leetcode.com/problems/array-nesting/description/ 题目描述 A zero-indexed array A of length ...

  4. 565.Array Nesting

    题目: A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the long ...

  5. 565. Array Nesting

    把所有的元素都作为第一个遍历一遍?不过,里面应该会有冗余.数组的下标和数组的元素值都在0-N之间,遍历过的置为-1,就不用在计算一遍了 class Solution {public int array ...

  6. leetcode Patching Array

    题意:给定一个数组nums和一个数n,求添加最少的数使得[1,n]中的每个数都可以由数组中元素和组成用known_sum表示已知的连续和为[1,known_sum),有了这个表示那就简单了: nums ...

  7. Array Nesting

    Array Nesting int arrayNesting(vector<int>& nums) {int start = 0;//每次的起始位置int cnt = 0;//某次 ...

  8. [LeetCode]561. Array Partition I (数组分区 1)

    561. Array Partition I Given an array of 2n integers, your task is to group these integers into n pa ...

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

最新文章

  1. mysqldump定时备份数据库
  2. suse mysql root密码忘记_SUSE11.4 找回 mysql root 密码?网上能找到的所有方法都试过了,不行......
  3. python dict update保持顺序_Python OrderedDict不保持元素顺序 - python
  4. ANE的开发需求一般太少,这个静态库如何包含第三方
  5. C++ 通讯录设计(二)
  6. 黑马程序员--里氏转换
  7. Fix Corrupt Blocks on HDFS
  8. 向上传递 java_Java向上转型向下转型
  9. 中公教育python培训讲师面试题_中公教育面试经验
  10. ADS仿真遇到error如何查找原因
  11. 计算机信息技术和网络技术的区别,信息技术计算机技术网络技术通信技术的区别与联系...
  12. GEE学习笔记03(空间类型数据)
  13. 支付宝小程序开发笔记
  14. 使用谷歌地图在 Flutter 应用中添加地图
  15. python 随机生成6位数字+字母的密码
  16. 信号与电源完整性6:EMI电磁干扰分析及降低解决方案概述
  17. 华为鸿蒙文案,机智的华为广告文案,各种“吊 打”三星小朋友!
  18. 嵌入式技术学习总结二
  19. SQL server 2005简体中文版下载
  20. 观察者模式ApplicationListener异步处理事件

热门文章

  1. c# 读取硬件信息并进行加密绑定
  2. KLEE--搜索方法Search Heuristics
  3. 查询选修了全部课程的学生的信息
  4. spfa(链式前向星)+dijkstra(链式前向星)
  5. EasyExcel-排除展示字段-02
  6. sweetalert2不兼容IE和360浏览器
  7. 【java小程序实战】小程序注销功能实现
  8. 仿英语流利说取词放大控件的实现
  9. python是一种编译语言_Python是编译型语言还是解释型语言?
  10. 怎么把文字转成语音播报?安利这几个方法