1.原题:

https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/

Given a string s formed by digits ('0' - '9') and '#' . We want to map s to English lowercase characters as follows:

  • Characters ('a' to 'i') are represented by ('1' to '9') respectively.
  • Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.

Return the string formed after mapping.

It's guaranteed that a unique mapping will always exist.

翻译:

给一个string s,由一串数组和‘#’组成,我们需要把其解码成英语小字母。

Input: s = "10#11#12"

Output: "jkab"

解释: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".

2.解题思路:

这道题考验的是你对string,char的理解,其实用java写这个题很麻烦,因为Java的string类实在是很麻烦,但是我就当顺便练手了。实际上推荐用C类语言去弄,因为C的string本身就是char的array。。。(当然了你可以声明char array)

class Solution {
public String freqAlphabets(String s)
{
String res =""; //初始化一个string类
for (int i = 0; i < s.length(); i++)
{
if ((i+2) < s.length() && s.charAt(i + 2) == '#')//向前搜索2个位置找#标志
{
String tmp_1 = String.valueOf(s.charAt(i))+ String.valueOf(s.charAt(i+1));
int tmp_2 = Integer.parseInt(tmp_1);
String tmp_3 = String.valueOf((char)('j' + tmp_2 - 10));
res += tmp_3; //#的数字肯定是大于10的,而且是从j开始,如果是10的话,
i += 2; //10-10=0,这时候就是J。如果是11, 11-10=1,为j之后的k。
}
else
{
char tmp_4 = (char)((s.charAt(i) - '1') + 'a');//从0开始为a,以此类推。
String tmp_3 = String.valueOf(tmp_4);
res += tmp_3;
}
}
return res;
}
}

参考阅读:

1.Java 如何将String转化为Int:https://blog.csdn.net/a772304419/article/details/79723249

2.Java charAt() 方法:https://www.runoob.com/java/java-string-charat.html

3.Java中char和String的相互转换:https://www.cnblogs.com/rrttp/p/7922202.html

4.Char类型的加减法:https://blog.csdn.net/wz568780720/article/details/81612467

leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping的更多相关文章

  1. leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段

    1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...

  2. leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法

    1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...

  3. leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石

    1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...

  4. leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字

    1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...

  5. leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST

    1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...

  6. leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字

    1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...

  7. leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)

    Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...

  8. leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero

    1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...

  9. leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点

    1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...

随机推荐

  1. 解决VS2013报错fopen、sprintf等函数安全的问题

    VS2013中使用fopen.sprintf等函数是会出现安全问题: error C4996: 'fopen': This function or variable may be unsafe. Co ...

  2. 第四十四篇 入门机器学习——matplotlib基础——实现数据可视化

    No.1. 绘制一条正弦曲线 No.2. 在一张图中绘制多条曲线 No.3. 可以为曲线指定颜色.线条样式 No.4. 可以指定横纵坐标轴的范围 也可以使用: No.6. 可以为每条曲线添加图示 No ...

  3. jvm(1):内存结构

    JVM内存结构 JVM内存的运行时数据区: 线程私有(在线程启动时创建) 程序计数器Program Counter Register 一块较小的内存空间,可以看作是当前线程所执行的字节码的行号指示器, ...

  4. FP side-effects

    https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076be ...

  5. eclipse快速创建一个Spring Boot应用

    1,创建一个空的maven项目 2,添加parent <parent> <groupId>org.springframework.boot</groupId> &l ...

  6. Python的DataFrame遍历_转CSDN_J小白Y

    转CSDN_J小白Y:https://blog.csdn.net/Jarry_cm/article/details/99683788 1.DataFrame.iterrows() 返回{索引,Seri ...

  7. ansible笔记(1):ansible基本概念

    一.基础概念 1.ansible是什么? ansible是一个配置管理工具,是一个自动化运维工具. 2.ansible能做什么? 它可以完成一组批量化的工作任务,或者经常重复性的工作任务.例如:a.在 ...

  8. 劳动人民万岁(拒绝惰性)------- 浅谈迭代对象(Iterable) 迭代器(Iterator)

    一.前戏 问题:如果一次抓取所有城市天气 再显示,显示第一个城市气温时有很高的延时,并且很浪费储存空间 解决方案:以“用时访问”策略,并且能把说有城市气温封装到一个对象里,可用for一句进行迭代 二. ...

  9. Windows server 2012文件服务器配置

    文件服务器的管理   Windows server 2012提供了易于使用的管理工具,让系统管理员更有效的管理服务器的资源. 安装文件服务器管理工具 添加角色-安装管理器 安装完成后直接可以在工具中打 ...

  10. 计算几何-BZOJ2618-凸包的交-HPI

    This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. bzoj2618 ...