Skip to content

Commit 6482543

Browse files
committed
add decode ways problem
1 parent 546aa53 commit 6482543

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

decode_ways.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# problem: https://leetcode.com/problems/decode-ways/description/
2+
# @param {String} s
3+
# @return {Integer}
4+
def num_decodings(s)
5+
return 0 if s.empty? or s.start_with? '0'
6+
close = far = 1
7+
8+
(2..s.size).each do |i|
9+
cur = 0
10+
cur = close if s[i-1] != '0'
11+
cur += far if s[i-2] == '1' or (s[i-2] == '2' and s[i-1].to_i <= 6)
12+
far = close
13+
close = cur
14+
end
15+
return close
16+
end

0 commit comments

Comments
 (0)