zoukankan      html  css  js  c++  java
  • leetcode--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.

     1 public class Solution {
     2     public int numDecodings(String s) {
     3        int num = 0;
     4         if(s.length() > 0){
     5             int[] decodeways  = new int[s.length()];
     6             if(Integer.parseInt(s.substring(0, 1)) == 0)
     7                 return 0;
     8             else
     9                 decodeways[0] = 1;            
    10             if(s.length()> 1){
    11                 if(Integer.parseInt(s.substring(1, 2)) != 0){
    12                     if(Integer.parseInt(s.substring(0, 2)) > 26)
    13                         decodeways[1] = 1;
    14                     else{
    15                         if(Integer.parseInt(s.substring(0, 1)) == 0)
    16                             return 0;
    17                         else
    18                             decodeways[1] = 2;                            
    19                     }                
    20                 }                
    21                 else{
    22                     if(Integer.parseInt(s.substring(0, 1)) > 2)
    23                         return 0;
    24                     else
    25                         decodeways[1] = 1;                        
    26                 }
    27                 for(int i = 2; i < s.length(); ++i){
    28                     if(Integer.parseInt(s.substring(i, i + 1)) != 0){
    29                         if(Integer.parseInt(s.substring(i - 1, i + 1)) > 26 || 
    30                             Integer.parseInt(s.substring(i - 1, i)) == 0)
    31                             decodeways[i] = decodeways[i - 1];
    32                         else
    33                             decodeways[i] = decodeways[i - 1] + decodeways[i - 2];
    34                     }
    35                     else{
    36                         if(Integer.parseInt(s.substring(i - 1, i)) == 0 || 
    37                             Integer.parseInt(s.substring(i - 1, i)) > 2){
    38                             num = 0;
    39                             break;
    40                         }
    41                         else
    42                             decodeways[i] = decodeways[i - 2];
    43                     }                    
    44                 }
    45             }
    46             num = decodeways[s.length() - 1];
    47         }
    48         return num;    
    49     }
    50 }

    code 2, the same algorithm as above

     1 public class Solution {
     2     public int numDecodings(String s) {
     3       int num = 0;
     4         if(s.length() == 0 || s.charAt(0) == '0')
     5             return num;
     6           else{
     7             int[] ways = new int[s.length() + 1];
     8             ways[0] = ways[1] = 1;
     9             for(int i = 2; i < s.length() + 1; ++i){
    10                 int cur = Integer.parseInt(s.substring(i - 1, i));
    11                 int pre = Integer.parseInt(s.substring(i - 2, i - 1));
    12                 if(cur != 0){
    13                     if(pre != 0){
    14                         ways[i] = (Integer.parseInt(s.substring(i - 2, i)) < 27) ? ways[i - 1] + ways[i - 2] : ways[i - 1];
    15                     }
    16                     else
    17                         ways[i] = ways[i - 2];                        
    18                 }
    19                 else{
    20                     if(pre > 2 || pre == 0)
    21                         return num;
    22                     else
    23                         ways[i] = ways[i - 2];
    24                 }    
    25             }
    26             num = ways[s.length()];    
    27         }
    28         return num;
    29     }
    30 }

    The third code:

    public class Solution {
        public int numDecodings(String s) {
           int result = 0;
    		int length = s.length();
    		if(length > 0){
    			int[] ways = new int[length + 1];
    			ways[length] = 1;
    			ways[length - 1] = (s.charAt(length - 1) - '0' == 0) ? 0 : 1;
    			for(int i = length - 2; i > -1; --i){
    				if(s.charAt(i)-'0' == 0)
    					ways[i] = 0;
    				else if((s.charAt(i) - '0' == 1)){
    					if(s.charAt(i + 1) - '0' == 0)
    						ways[i] = ways[i + 2];
    					else
    						ways[i] = ways[i + 2] + ways[i + 1];
    				}
    				else if(s.charAt(i) -'0' == 2){
    					if(s.charAt(i + 1) - '0' == 0)
    						ways[i] = ways[i + 2];
    					else if(s.charAt(i + 1) - '0' != 0 && s.charAt(i + 1) - '0' < 7)
    						ways[i] = ways[i + 1] + ways[i + 2];
    					else
    						ways[i] = ways[i + 1];					
    				}
    				else{
    					if(s.charAt(i + 1) - '0' == 0)
    						ways[i] = 0;
    					else
    						ways[i] = ways[i + 1];
    				}
    			}
    			result = ways[0];
    		}
    		return result;
        }
    }
    

      

  • 相关阅读:
    编程语言学哪个比较好?
    C#一定要避免程序中很多的依靠
    EXPIREAT
    EXISTS
    DUMP
    Python之sys模块
    Python的OS模块
    CentOS 7上安装gitlab-runner
    PyCharm激活方法
    Linux03 文件的相关操作(touch、rm、mv、cat)
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3536707.html
Copyright © 2011-2022 走看看