File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments