Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53f9a9b

Browse files
feat(problem #85): add summary-ranges
1 parent f930b73 commit 53f9a9b

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎Easy/summary-ranges.cs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+

2+
internal class Program
3+
{
4+
private static string _GetRange(int first, int last)
5+
{
6+
return (Convert.ToInt32(first) != last) ? $"{first}->{last}" : $"{first}";
7+
}
8+
9+
public static IList<string> SummaryRanges(int[] nums)
10+
{
11+
if (nums == null || nums.Length == 0)
12+
{
13+
return [];
14+
}
15+
16+
IList<string> result = new List<string>();
17+
string firstItemInRange = string.Empty;
18+
19+
for (int i = 0; i < nums.Length; i++)
20+
{
21+
if (string.IsNullOrWhiteSpace(firstItemInRange))
22+
{
23+
firstItemInRange = $"{nums[i]}";
24+
}
25+
26+
if (nums[i] < int.MaxValue && nums.Contains(nums[i] + 1))
27+
{
28+
continue;
29+
}
30+
31+
string range = _GetRange(Convert.ToInt32(firstItemInRange), nums[i]);
32+
33+
result.Add(range);
34+
firstItemInRange = string.Empty;
35+
}
36+
37+
return result;
38+
}
39+
40+
private static void Main(string[] args)
41+
{
42+
int[] nums = [-2147483648, -2147483647, 2147483647];
43+
44+
Console.WriteLine(string.Join(',', SummaryRanges(nums)));
45+
46+
Console.ReadKey();
47+
}
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /