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 b05e36d

Browse files
ADD Decode-Ways problem #61
1 parent 6aecce8 commit b05e36d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

‎Medium/decode-ways.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Linq;
3+
4+
public class Solution
5+
{
6+
public static int NumDecodings(string s)
7+
{
8+
if (string.IsNullOrWhiteSpace(s) || s[0] == '0')
9+
{
10+
return 0;
11+
}
12+
13+
if (s.Length == 1)
14+
{
15+
if (s[0] == '0')
16+
{
17+
return 0;
18+
}
19+
else
20+
{
21+
return 1;
22+
}
23+
}
24+
25+
int[] dp = Enumerable.Repeat(1, s.Length + 1).ToArray();
26+
27+
for (int i = s.Length - 1; i >= 0; i--)
28+
{
29+
if (s[i] == '0')
30+
{
31+
dp[i] = 0;
32+
}
33+
else
34+
{
35+
dp[i] = dp[i + 1];
36+
}
37+
38+
if ((i + 1) < s.Length &&
39+
((s[i] == '1') || (s[i] == '2' && "0123456".Contains(s[i + 1])))
40+
)
41+
{
42+
dp[i] += dp[i + 2];
43+
}
44+
}
45+
46+
return dp[0];
47+
}
48+
49+
public static void Main(string[] args)
50+
{
51+
Console.WriteLine(NumDecodings("11106"));
52+
53+
Console.ReadKey();
54+
}
55+
}
56+

0 commit comments

Comments
(0)

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