c# Leetcode 565. 数组嵌套(待提交-中等)

https://leetcode-cn.com/problems/array-nesting/submissions/

我想我终于把这道题给做出来了:

public class Solution {
    public int ArrayNesting(int[] nums) {
        int res = 0;
			if (nums.Length == 1) return 1;
			var set = new HashSet<int>();
			int c = 0;
			while (true)
			{
				c= count(nums, c);
				bool b = set.Add(nums[c]);
				if (b)
				{
					res++;
				}
				else
				{
					break;
				}
			} 
			return res;
    }
    		public static int count(int[] nums,int count)
		{
			return nums[count]; 
		}
}

可我不明白为什么会有这样的这是用例:

解答错误

输入

[0,2,1]

输出

1

预期结果

2

难道 【0,2,1】的测试结果不是1么?

猜你喜欢

转载自blog.csdn.net/us2019/article/details/86562672
565
今日推荐