Skip to content

Commit 7908474

Browse files
Accepted
1 parent b217f8f commit 7908474

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

problems/src/ClimbingStairs.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Created by gouthamvidyapradhan on 01/04/2017.
3+
*/
4+
public class ClimbingStairs
5+
{
6+
/**
7+
* Main method
8+
* @param args
9+
* @throws Exception
10+
*/
11+
public static void main(String[] args) throws Exception
12+
{
13+
14+
}
15+
16+
public int climbStairs(int n)
17+
{
18+
if(n == 0 || n == 1) return 1;
19+
int[] A = new int[n + 1];
20+
A[n] = 1;
21+
A[n - 1] = 1;
22+
for(int i = n - 2; i >= 0; i --)
23+
A[i] = A[i + 1] + A[i + 2];
24+
return A[0];
25+
}
26+
27+
}

problems/src/DecodeWays.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.PriorityQueue;
2+
import java.util.Queue;
3+
4+
/**
5+
* Created by gouthamvidyapradhan on 01/04/2017.
6+
* Accepted
7+
*/
8+
public class DecodeWays
9+
{
10+
/**
11+
* Main method
12+
* @param args
13+
* @throws Exception
14+
*/
15+
public static void main(String[] args) throws Exception
16+
{
17+
System.out.println(new DecodeWays().numDecodings("3120"));
18+
}
19+
20+
public int numDecodings(String s)
21+
{
22+
if(s == null || s.isEmpty()) return 0;
23+
int[] dp = new int[s.length() + 2];
24+
dp[s.length()] = 1;
25+
dp[s.length() + 1] = 1;
26+
for(int i = s.length() - 1; i >= 0; i --)
27+
{
28+
for(int j = i + 1; j < i + 3; j ++)
29+
{
30+
if(j <= s.length())
31+
{
32+
String subStr = s.substring(i, j);
33+
if(!subStr.startsWith("0"))
34+
{
35+
int intVal = Integer.parseInt(subStr);
36+
if(intVal <= 26)
37+
{
38+
dp[i] += dp[j];
39+
}
40+
}
41+
}
42+
}
43+
}
44+
return dp[0];
45+
}
46+
}

0 commit comments

Comments
 (0)