1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0 || s.startsWith("0")) return 0;
if (s.length() == 1) return 1;
// define dp state
int dp[] = new int[s.length() + 1];
// init
if (s.charAt(0) != '0') dp[0] = 1;
if (s.charAt(1) != '0') dp[1] = 1;
for (int i = 2; i < s.length() + 1; i++) {
int t1 = Integer.parseInt(s.substring(i - 1, i));
if (1 <= t1 && t1 <= 9) dp[i] += dp[i - 1];
int t2 = Integer.parseInt(s.substring(i - 2, i));
if (10 <= t2 && t2 <= 26) dp[i] += dp[i - 2];
}
return dp[s.length()];
}
}
|
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
| public class KahanSummation {
private static float kahanSum(float... fa) {
float sum = 0.0f;
float c = 0.0f;
for (float f : fa) {
float y = f - c;
float t = sum + y;
c = (t - sum) - y;
sum = t;
}
return sum;
}
private static float epsilon() {
float eps = 1.0f;
while (1.0f + eps != 1.0f) eps /= 2.0f;
return eps;
}
public static void main(String[] args) {
float a = 1.0f;
float b = epsilon();
float c = -b;
System.out.println("Epsilon = " + b);
System.out.println("(a + b) + c = " + ((a + b) + c));
System.out.println("Kahan sum = " + kahanSum(a, b, c));
}
}
|