File tree 2 files changed +28
-0
lines changed
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change 81
81
| 88 | [ Merge Sorted Array] ( https://leetcode.com/problems/merge-sorted-array ) | [ ![ Java] ( assets/java.png )] ( src/MergeSortedArray.java ) [ ![ Python] ( assets/python.png )] ( python/merge_sorted_array.py ) | |
82
82
| 89 | [ Gray Code] ( https://leetcode.com/problems/gray-code ) | [ ![ Java] ( assets/java.png )] ( src/GrayCode.java ) | |
83
83
| 90 | [ Subsets II] ( https://leetcode.com/problems/subsets-ii ) | [ ![ Java] ( assets/java.png )] ( src/SubsetsII.java ) | |
84
+ | 91 | [ Decode Ways] ( https://leetcode.com/problems/decode-ways ) | [ ![ Java] ( assets/java.png )] ( src/DecodeWays.java ) | |
84
85
| 94 | [ Binary Tree Inorder Traversal] ( https://leetcode.com/problems/binary-tree-inorder-traversal/ ) | [ ![ Java] ( assets/java.png )] ( src/BinaryTreeInorderTraversal.java ) [ ![ Python] ( assets/python.png )] ( python/binary_tree_inorder_traversal.py ) | |
85
86
| 100 | [ Same Tree] ( https://leetcode.com/problems/same-tree ) | [ ![ Java] ( assets/java.png )] ( src/SameTree.java ) [ ![ Python] ( assets/python.png )] ( python/same_tree.py ) | |
86
87
| 101 | [ Symmetric Tree] ( https://leetcode.com/problems/symmetric-tree ) | [ ![ Java] ( assets/java.png )] ( src/SymmetricTree.java ) [ ![ Python] ( assets/python.png )] ( python/symmetric_tree.py ) | |
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/decode-ways
2
+ // T: O(n)
3
+ // S: O(1)
4
+
5
+ public class DecodeWays {
6
+ private static final char ZERO = '0' ;
7
+
8
+ public int numDecodings (String s ) {
9
+ int previous = 1 , current , temp ;
10
+ current = s .charAt (s .length () - 1 ) > ZERO ? 1 : 0 ;
11
+ for (int i = s .length () - 2 ; i >= 0 ; i --) {
12
+ if (s .charAt (i ) == ZERO ) {
13
+ previous = current ;
14
+ current = 0 ;
15
+ } else {
16
+ temp = current ;
17
+ current += isValidAlphabet (s .charAt (i ), s .charAt (i + 1 )) ? previous : 0 ;
18
+ previous = temp ;
19
+ }
20
+ }
21
+ return current ;
22
+ }
23
+
24
+ private boolean isValidAlphabet (char a , char b ) {
25
+ return (a - ZERO ) * 10 + (b - ZERO ) <= 26 ;
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments