leetCode 91.Decode Ways (解码方式) 解题思路和方法

A message containing letters from A-Z is being encoded to numbers using the
following mapping:

‘A‘ -> 1
‘B‘ -> 2
...
‘Z‘ -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,

Given encoded message "12", it could be decoded as "AB" (1
2) or "L" (12).

The number of ways decoding "12" is 2.

思路:本题用递归没有实现,可能是递归没有写好,最后在网上参考了资料写了动态规划的代码。

思想是判断当前判断当前值是否为0,不为0则f[i] = f[i] + f[i-1];

然后当前值和上一个字符能否组成26以下的输,能这与f[i] = f[i] + f[i-2]。

具体代码如下:

public class Solution {
    public int numDecodings(String s) {
    	//动态规划标记
    	int[] f = new int[s.length()];
    	char[] c = s.toCharArray();
    	//边界情况
    	if(c.length == 0){
    		return 0;
    	}
    	//第一个元素
    	f[0] = c[0] > '0' ? 1:0;

    	if(c.length == 1){
    		return f[0];
    	}
    	//f[1]的值是关键,写不好,将会出现各种错误
    	int k = c[0] > '0' && c[1] > '0'? 1:0;
    	f[1] = k + (c[0] == '1' || c[0] == '2' && c[1] <= '6' ? 1:0);

    	//从前往后遍历
    	for(int i = 2; i < c.length; i++){
    		if(c[i] > '0'){//第一个元素大于0,添加情况
    			f[i] += f[i-1];
    		}
    		//在10-26之间则添加两个字母组成一个的情况
    		if(c[i-1] == '1' || (c[i-1] == '2' && c[i] <= '6')){
    			f[i] += f[i-2];
    		}
    	}

    	return f[c.length-1];
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 05:00:16

leetCode 91.Decode Ways (解码方式) 解题思路和方法的相关文章

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给一串包含数字的加密报文,求有多少种解码方式 举个例子,已知报文"12",它可以解码为AB(1 2),也可以是L (12) 所以解码方式有2种. 测试样例 Input: "0" "121212" "1010

[LeetCode] 91. Decode Ways 解码方法

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given encoded

[LeetCode] decode ways 解码方式

A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given encoded me

[LeetCode]91.Decode Ways

题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 - 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encode

[LeetCode] 91. Decode Ways Java

题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given enco

19.2.23 [LeetCode 91] Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a non-empty string containing only digits, determine the total number of ways to decode it. Example 1: Input: &qu

leetCode 27.Remove Element (删除元素) 解题思路和方法

Remove Element Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路:此题和26题一脉相承,算法上不难,具体如代码所示: public class

leetCode 51.N-Queens (n皇后问题) 解题思路和方法

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur

leetCode 57.Insert Interval (插入区间) 解题思路和方法

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], i