-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
52 lines (50 loc) · 1.53 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
namespace CircularArrayLoop
{
class Program
{
static void Main(string[] args)
{
int[] nums = { -1};
Console.WriteLine(CircularArrayLoop(nums));
Console.WriteLine(-7 % 5);
}
static bool CircularArrayLoop(int[] nums)
{
if (nums.Length == 1) return false;
for (int i = 0; i < nums.Length; i++)
{
var index = new List<int>();
var set = new HashSet<int>();
var cur = i;
var sameDirection = true;
var found = false;
while (true)
{
if (set.Contains(cur))
{
if (cur != index[^1])
found = true;
break;
}
index.Add(cur);
set.Add(cur);
var next = cur + nums[cur];
if (next >= nums.Length)
next %= nums.Length;
else if (next < 0)
next = nums.Length - (-next % nums.Length);
if (nums[next] * nums[i] <= 0)
{
sameDirection = false;
break;
}
cur = next;
}
if (sameDirection && found) return true;
}
return false;
}
}
}