-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1796-Second-largest-digit-in-a-string.cs
More file actions
90 lines (76 loc) · 2.3 KB
/
1796-Second-largest-digit-in-a-string.cs
File metadata and controls
90 lines (76 loc) · 2.3 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Solution._1796.Second_largest_digit_in_a_string
{
public class _1796_Second_largest_digit_in_a_string
{
/// <summary>
/// HashSet Solution
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public int SecondHighest(string s)
{
HashSet<int> set = new HashSet<int>();
for (int i = 0; i < s.Length; i++)
{
if (s[i] >= '0' && s[i] <= '9')
set.Add(s[i] - '0');
}
if (set.Count <= 1) return -1;
set.Remove(set.Max());
return set.Max();
}
/// <summary>
/// Iteration & HashSet
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
//public int SecondHighest(string s)
//{
// HashSet<int> set = new HashSet<int>();
// for (int i = 0; i < s.Length; i++)
// {
// if (s[i] >= '0' && s[i] <= '9')
// set.Add(s[i] - '0');
// }
// if (set.Count <= 1) return -1;
// int first = 0;
// int second = 0;
// foreach (var item in set)
// {
// if (item > first)
// {
// second = first;
// first = item;
// }
// else if (item < first && item > second)
// second = item;
// }
// return second;
//}
/// <summary>
/// Iteration Solution
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
//public int SecondHighest(string s)
//{
// int first = -1;
// int second = -1;
// for (int i = 0; i < s.Length; i++)
// {
// if (char.IsDigit(s[i]) && s[i] - '0' > first)
// {
// second = first;
// first = s[i] - '0';
// }
// else if (char.IsDigit(s[i]) && s[i] - '0' > second && s[i] - '0' < first)
// second = s[i] - '0';
// }
// return second;
//}
}
}