c++ 26ms simple solution


  • 0
    C
    class Solution {
    public:
        
        int arrayNesting(vector<int>& nums) {
            int res=0,nsize=nums.size();
            vector<int> record(nsize,0);
            for(int i=0;i<nsize;i++){
                helper(nums,nums[i],record);
                res=max(res, record[nums[i]]);
            }
            return res;
        }
        void helper(vector<int>& nums,int n,vector<int>& record){
            int cnt=0;
            while(!record[n]){
                cnt++;
                record[n]=cnt;
                n=nums[n];
            }
        }
        
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.