154.Decode Ways

本文介绍了一种使用动态规划解决消息解码问题的方法。对于由数字组成的编码消息,要计算其可能的解码方式总数。例如,“12”可以被解码为“AB”或“L”。文章提供了一个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 encoded message "12",it could be decoded as "AB" (1 2) or"L" (12).

The number of ways decoding "12" is 2.



public class Solution {
    /**
	 * 采用动态规划的思想做。
	 * 题目类似于上台阶的问题。
	 * 对于本题目来说,设定一个数组num,num[i]表示s.subString(0,i+1)字符串的编码个数。
	 * num[i] = num[i-1]+num[i-2];
	 * 但是num[i]是否每次都可以拆分成num[i-1]与num[i-2]有各自的取决条件。
	 * 包含num[i-1]的条件是n!=0
	 * 包含num[i-2]的条件是倒数第二位不为0,且最后两位在[1,26]之间。
	 * 这样分析就巧妙的转化了条件判断,纯粹的判断num[i]分别拆分成num[i-1]的条件和num[i-2]的条件。
	 * @param s
	 * @return
	 */
    public int numDecodings(String s) {
    	int len = s.length();
    	if(len <=0 || s.charAt(0)=='0'){
    		return 0;
    	}
    	else{
    	    
    		int num[] = new int[len+1];
    		num[0] = 1;
    		num[1] = 1;
    		/*依次读取后面的每个字符*/
    		for(int i=2;i<=len;i++){
    			int n = s.charAt(i-1)-'0'; 
    		    int digit = Integer.parseInt(s.substring(i-2,i));//读到的后两位,将其转化为整数
    		    
    		    if(n!=0){
    		        num[i] = num[i-1];
    		    }
    		    
    		    if(s.charAt(i-2) != '0' && digit>=1 && digit <=26){
    		        num[i] =num[i] + num[i-2];
    		    }
    		    
    		}
    		return num[len];
    	}
    	
        
    }
}


You are to retrieve the following document using the HTTP protocol in a way that you can examine the HTTP Response headers. http://data.pr4e.org/intro-short.txt There are three ways that you might retrieve this web page and look at the response headers: Preferred: Modify the socket1.py program to retrieve the above URL and print out the headers and data. Make sure to change the code to retrieve the above URL - the values are different for each URL. Open the URL in a web browser with a developer console or FireBug and manually examine the headers that are returned. Use the telnet program as shown in lecture to retrieve the headers and content. Enter the header values in each of the fields below and press "Submit". Last-Modified: ETag: Content-Length: Cache-Control: Content-Type: socket1.py的内容:注意不能命名为socket,不然和lib重名了。 import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect(('data.pr4e.org', 80)) cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode() mysock.send(cmd) while True: data = mysock.recv(512) if len(data) < 1: break print(data.decode(),end='') mysock.close() AI写代码 html 我的解法1(老师推荐的): import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect(('data.pr4e.org',80)) cmd = 'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n'.encode() mysock.send(cmd) while True: data = mysock.recv(512) if(len(data) < 1): break print(data.decode()) mysock.close() 我的解法2: 进入网站:http://data.pr4e.org/intro-short.txt,点F12,点Network,Ctrl+R,点击intro-short.txt的Headers寻找答案。 可能存在的解法3: ———————————————— 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 原文链接:https://blog.csdn.net/zjt1027/article/details/104213562
09-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值