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

本文深入探讨了LeetCode上的一道经典题目“数组嵌套”,分享了一个解决方案,并对一个特定测试案例的预期结果进行了讨论。文章指出,对于数组[0,2,1],正确结果应为2,而非1,解释了为何会出现此误解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值