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 c223c7e

Browse files
Second Largest Digit in a String
1 parent 12200b1 commit c223c7e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Link: https://leetcode.com/problems/second-largest-digit-in-a-string/
2+
Language: C#
3+
4+
5+
6+
7+
8+
9+
public class Solution {
10+
public int SecondHighest(string s)
11+
{
12+
bool[] digits = new bool[10];
13+
14+
for (int i = 0; i < s.Length; i++)
15+
{
16+
if (char.IsDigit(s[i]))
17+
digits[s[i] - '0'] = true;
18+
}
19+
20+
bool largestNumber = false;
21+
22+
for (int i = 9; i >= 0; i--)
23+
{
24+
if (digits[i] && !largestNumber)
25+
largestNumber = true;
26+
else if (digits[i])
27+
return i;
28+
}
29+
30+
return -1;
31+
}
32+
}

0 commit comments

Comments
(0)

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