Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df90bd6

Browse files
authored
Problem# 271 and 128
1 parent 03ec11a commit df90bd6

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Solution12
2+
{
3+
public int LongestConsecutive(int[] nums)
4+
{
5+
var numSet = new HashSet<int>();
6+
var longest = 0;
7+
8+
foreach(var num in nums)
9+
{
10+
numSet.Add(num);
11+
}
12+
13+
foreach(var num in nums)
14+
{
15+
var length = 0;
16+
if (!numSet.Contains(num - 1))
17+
{
18+
while (numSet.Contains(num + length))
19+
{
20+
length += 1;
21+
}
22+
23+
longest = Math.Max(longest, length);
24+
}
25+
}
26+
return longest;
27+
}
28+
}

csharp/271-Encode-Decode-Strings.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
namespace AlgoPractice
3+
{
4+
public class Solution
5+
{
6+
7+
// Encodes a list of strings to a single string.
8+
public string Encode(IList<string> strs)
9+
{
10+
var result = string.Empty;
11+
foreach(var str in strs)
12+
{
13+
result += $"{str.Length}#{str}";
14+
}
15+
return result;
16+
}
17+
18+
// Decodes a single string to a list of strings.
19+
public IList<string> Decode(string s)
20+
{
21+
IList<string> result = new List<string>();
22+
var i = 0;
23+
while(i < s.Length)
24+
{
25+
var j = i;
26+
while(s[j] != '#')
27+
{
28+
j++;
29+
}
30+
31+
var numStr = s.Substring(i, j - i);
32+
var num = int.Parse(numStr);
33+
34+
var word = s.Substring(j+1, num);
35+
result.Add(word);
36+
37+
i = j + 1 + num;
38+
}
39+
return result;
40+
}
41+
42+
}
43+
}
44+

0 commit comments

Comments
 (0)