[LeetCode] 565. Array Nesting
565. Array Nesting
这道题目的大概意思是,先选定数组中一个起始的位置,再根据她的值定位到相应的下标,继续下去,直到出现循环为止,最后找出最长的不循环的。
显然需要将数组中每个位置起始的都要计算一遍,所以首先想到的就是dfs。
class Solution(object): def arrayNesting(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0 visited = {} result = 0 for idx in range(0, len(nums)): if idx not in visited: result = max(result, self.helper(nums, idx, visited)) return result def helper(self, nums, start, visited): """ 递归移动到下一个位置,直到和起始位置一样的时候 同时记录移动的个数 :param nums: :param start: :param visited: :return: """ i = start cnt = 0 while i != start or cnt == 0: visited[i] = True i = nums[i] cnt += 1 return cnt
或者是不用函数递归的形式,直接写在一个函数里面,这样还避免了函数递归造成的消耗;
class Solution(object): def arrayNesting(self, nums): """ :type nums: List[int] :rtype: int """ visited = [False] * len(nums) max_cnt = 0 for i in range(len(nums)): cnt = 1 first = i if not visited[i]: next_idx = nums[i] # 下一个位置 # dfs while first != next_idx: visited[next_idx] = True next_idx = nums[next_idx] cnt += 1 max_cnt = max(max_cnt, cnt) return max_cnt
转载于:https://www.cnblogs.com/George1994/p/8976748.html
[LeetCode] 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 ...
- Leetcode——565. Array Nesting
题目原址 https://leetcode.com/problems/array-nesting/description/ 题目描述 A zero-indexed array A of length ...
- 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 ...
最新文章
- cocos dos命令
- mysql的api框架_API接口幂等性框架设计
- [hdu4333]Revolving Digits
- jquery ajax的post、get方式
- POJ - 3694 Network(边双缩点+LCA+并查集优化)
- linux源码安装必备条件,玩转Linux必备的金钥匙之源码安装mysql
- line-height与图片底部间隙的学习整理转述
- 互联网反欺诈体系中的常用方法
- vsftpd增加用户 linux,linux下开通vsftpd 添加新ftp用户
- 读取速度500m/s和1000m/s的硬盘,装系统使用起来有区别吗?
- 4--RESTful应用程序
- 32岁程序员推拿一小时差点丧命!医生说按错这个地方
- Word基础(八)带圈字符
- 英文文本分类(CNN)
- android检测cup温度工具,Android如何实现获取手机CPU的温度?
- 【游戏程序设计】鼠标交互
- 平安云加速器D-day开幕为企业量身打造“加速”之旅
- Eclipse插件(RCP)自定义透视图
- 设计用于环境监测的无线传感网多跳数据采集、传输系统。在PC机上能显示当前测得的温/湿度、光照等环境信息。当光照超过一定阈值时,感知节点通过LED信号灯给以报警。
- 区块链与ICO,了解一下
热门文章
- python下载付费文档教程-付费?是不可能的!处理 PDF 只需几行代码,彻底解放双手!...
- python利器怎么编程-C++和Python混合编程的利器
- python下载安装教程3.8.0-Python3.8下载
- python 数据分析学什么-学好python和数据分析有什么关系?
- 学python是什么专业-小白学python是自学好,还是专业老师培训好?
- python编程入门指南pdf-python编程初学者指南
- python画树叶-Python如何生成树形图案
- 学python要多少钱-学python去培训班要多少钱?
- python画柱状图和折线图-Python读取Excel表格,并同时画折线图和柱状图的方法
- python导入txt为dataframe-Python提取TXT数据转化为DataFrame