Leetcode——565. Array Nesting
题目原址
https://leetcode.com/problems/array-nesting/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:
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:
- 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].
解题思路
给一个长度N的数组,里面的元素值为 1 到 N-1,要求找到最长的子数组,返回其长度。
子数组其实是嵌套的数组,题目中解释了什么是嵌套数组,即元素值变为坐标,再讲坐标变为值。
因为数组中可能存在多个循环子数组,那就一一找到,最后返回一个长度最大的即可。
- 每个元素都要找循环子数组。将元素存入set集合中,如果集合中不存在该元素,说明没有构成循环,则将元素存到set集合中,并将下标更改为当前元素的值。将循环子串长度的计数元素值+1。
- 每个元素都如上述步骤进行计算,最后找到数组中最长的子数组的长度。
AC代码
class Solution {Set<Integer> set = new HashSet<Integer>();public int arrayNesting(int[] nums) {int ret = 0;for(int i: nums) {ret = Math.max(ret, valid(nums,i));}return ret;}//返回循环的子串中元素的个数public int valid(int[] nums,int i) {int ret = 0;while(!set.contains(i)) {set.add(i);i = nums[i];ret++;}return ret;}
}
Leetcode——565. Array Nesting相关推荐
- [LeetCode] 565. Array Nesting
565. Array Nesting 这道题目的大概意思是,先选定数组中一个起始的位置,再根据她的值定位到相应的下标,继续下去,直到出现循环为止,最后找出最长的不循环的. 显然需要将数组中每个位置起始 ...
- LeetCode 565. Array Nesting
Array Nesting 题目描述: A zero-indexed array A consisting of N different integers is given. The array co ...
- Leetcode 565. Array Nesting
题目 链接:https://leetcode.com/problems/array-nesting/ Level: Medium Discription: A zero-indexed array A ...
- 565.Array Nesting
题目: A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the long ...
- 565. Array Nesting
把所有的元素都作为第一个遍历一遍?不过,里面应该会有冗余.数组的下标和数组的元素值都在0-N之间,遍历过的置为-1,就不用在计算一遍了 class Solution {public int array ...
- leetcode Patching Array
题意:给定一个数组nums和一个数n,求添加最少的数使得[1,n]中的每个数都可以由数组中元素和组成用known_sum表示已知的连续和为[1,known_sum),有了这个表示那就简单了: nums ...
- Array Nesting
Array Nesting int arrayNesting(vector<int>& nums) {int start = 0;//每次的起始位置int cnt = 0;//某次 ...
- [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 ...
- 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 ...
最新文章
- 2、创建视图(CREATE VIEW)
- 计算机知识产权结合,计算机知识产权.doc
- 小白开学Asp.Net Core 《五》
- hdu2648 Shopping-map容器
- js前端——滑稽官网的亮瞎眼系列滑稽大法
- 笨办法学 Python · 续 练习 2:创造力
- 万条留言!这本Python凭啥火爆程序员的朋友圈?
- tomcat连接oracle非常慢,关于myEclipse中tomcat 6.0启动慢的有关问题
- c++11 多线程编程(一)------初始
- 在Linux下怎样安装rarlinux?
- 大麦盒子无线服务器连接超时,【当贝市场】大麦盒子无线设置问题汇总
- 启动react项目报找不到文件的错误
- 家用路由器改造成交换机教程
- suest:跨模型比较与广义豪斯曼检验
- 抖音微信消息推送情侣告白浪漫(简易版)
- android 模拟器输入中文
- 刚子扯谈:广告向东 客户向西
- ##求助!Python编程从入门到实践 外星人飞船问题
- swift TextView
- 根据系统的业务需求,数据库设计可以设置三张表,分别为客户信息表、账号表、交易日志记录表,其表的具体设置如下: