当前位置: 首页 > news >正文

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 encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
The number of ways decoding "12" is 2.


就是给定1个由数字组成的字符串,按照对应关系(1-A,2-B...)完成解码,共有多少种解码方式。
1. 确定问题范围,也就是s中每一个字符s[i]的取值范围
-s[0]不能为0或大于2
-s中不能连续出现两个0


2. 找规律
在满足取值范围的情况下,设a[i]为s中第i个字符的解码可能数,其中i∈[0,n)
a[0] = 1;
a[1] = s[1] != 0 时为  2 否则为 1;
a[2] : 
s[2] 不为0时 a[2] += a[1] , 
s[0]和s[1]可以组成10-26中的一个数时 a[2] += a[0]


因此,对于a[n],  
s[n] != 0 时 ,a[n] += a[n-1] ; 
s[n-1]与s[n]可组成10-26 时, a[n] += a[n-2];


对于长度为n的字符串,最后解为a[n-1].


实现代码:



public class Solution {
 public int NumDecodings(string s) 
 {
     if(string.IsNullOrWhiteSpace(s)){
	 	return 0;
	 }
	 
	 if(s.Length == 1){
	 	return s == "0" ? 0 : 1;
	 }
	 if(s[0] == '0' || s[0] >= '3'&& s[1] == '0'){
	 	return 0;
	 }
	 
	 if(s.Length == 2){
	 	return s[1] == '0' || !ok(s[0],s[1]) ? 1 : 2;
	 }
	 
	 var arr = new int[s.Length];
	 arr[0] = 1;
	  arr[1] = s[1] == '0' || !ok(s[0], s[1]) ? 1 : 2;
	 
	 for(var i = 2;i < s.Length; i++){
	 	if(s[i-1] == '0' && s[i] == '0'){
			return 0;
		}
		
	 	if(s[i] != '0'){
			arr[i] += arr[i-1];
		}
		if(ok(s[i-1], s[i])){
			arr[i] += arr[i-2];
		}
	 }
	 
	 return arr[s.Length-1];
 }
 
 private bool ok(char c1 , char c2){
 if(c1 == '0'){
 return false;
 }
 if (c1 == '1' || c1 == '2' && c2 <= '6'){
 return true;
 }
 return false;
 }
}


相关文章:

  • 嵌入式Linux系统中的GUI系统的研究与移植
  • LeetCode -- Substring with Concatenation of All Words
  • asp.net MVC5 sitemap 的使用
  • CentOS 5.x 預設啟動的服務簡易說明
  • Leet -- Remove Duplicates from Sorted Array
  • LeetCode -- Best Time to Buy and Sell Stock II
  • 海闊天空 信樂團
  • Contains Duplicate III
  • LeetCode -- Combination Sum
  • MySQL添加用户
  • LeetCode -- Candy
  • Leet -- Plus One
  • Leet -- Generate Parentheses
  • LeetCode -- Distinct Subsequences
  • LeetCode -- SpiralOrder
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • CSS进阶篇--用CSS开启硬件加速来提高网站性能
  • DataBase in Android
  • LeetCode算法系列_0891_子序列宽度之和
  • Linux后台研发超实用命令总结
  • Vue 动态创建 component
  • 阿里云Kubernetes容器服务上体验Knative
  • 测试开发系类之接口自动化测试
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 面试总结JavaScript篇
  • 深度解析利用ES6进行Promise封装总结
  • 用jquery写贪吃蛇
  • ​520就是要宠粉,你的心头书我买单
  • ​ArcGIS Pro 如何批量删除字段
  • ​HTTP与HTTPS:网络通信的安全卫士
  • ​业务双活的数据切换思路设计(下)
  • #if 1...#endif
  • (30)数组元素和与数字和的绝对差
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (九)信息融合方式简介
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (六)激光线扫描-三维重建
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (四)JPA - JQPL 实现增删改查
  • (算法)N皇后问题
  • (一)插入排序
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • .equals()到底是什么意思?
  • .NET Core 成都线下面基会拉开序幕
  • .Net MVC + EF搭建学生管理系统
  • .net 逐行读取大文本文件_如何使用 Java 灵活读取 Excel 内容 ?
  • .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
  • .net生成的类,跨工程调用显示注释
  • .Net中的集合
  • .net中我喜欢的两种验证码
  • .stream().map与.stream().flatMap的使用
  • /bin/rm: 参数列表过长"的解决办法