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 2c31537

Browse files
committed
Added SummaryRanges
1 parent 86374fa commit 2c31537

File tree

1 file changed

+37
-2
lines changed
  • CodingInterview/CodingInterview

1 file changed

+37
-2
lines changed

‎CodingInterview/CodingInterview/Tests.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,44 @@ int[] GetMinimumSizeSubarraySum(int[] inputArray, int n)
110110
return null;
111111
}
112112

113-
List<string> GetSummaryRanges(int [] inputArray)
113+
string[] GetSummaryRanges(int [] inputArray)
114114
{
115-
return null;
115+
if (inputArray == null || inputArray.Length == 0)
116+
throw new ArgumentNullException("inputArray needs to have elements");
117+
118+
var listOfAnswers = new List<string>();
119+
120+
for(int index = 0; index < inputArray.Length; index++)
121+
{
122+
var firstString = string.Empty;
123+
bool found = false;
124+
for(int insideIndex = index; insideIndex < inputArray.Length; insideIndex++)
125+
{
126+
if(insideIndex + 1 < inputArray.Length && (inputArray[insideIndex + 1] - inputArray[insideIndex] == 1))
127+
{
128+
if(!found)
129+
{
130+
firstString = $"{inputArray[insideIndex]}";
131+
}
132+
found = true;
133+
}
134+
else
135+
{
136+
if(found)
137+
{
138+
string endString = inputArray[insideIndex].ToString();
139+
listOfAnswers.Add($"{firstString}->{endString}");
140+
index = insideIndex;
141+
break;
142+
}
143+
else
144+
{
145+
listOfAnswers.Add($"{inputArray[insideIndex]}");
146+
}
147+
}
148+
}
149+
}
150+
return listOfAnswers.ToArray();
116151
}
117152

118153
[Test]

0 commit comments

Comments
(0)

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