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. asp.net使用wsdl文件调用接口,以及调用SSL接口报错“根据验证过程 远程证书无效”的处理

    1.调用wsdl接口,首先需要将wsdl文件转换为cs文件: 进入VS 开发人员命令提示行,输入如下命令: c:/Program Files/Microsoft Visual Studio 8/VC& ...

  2. selenium webdriver 登录百度

    public class BaiduTest { private WebDriver driver; private String baseUrl; private StringBuffer veri ...

  3. 三维数据曲面图 season绘图 panda绘图

    三维数据曲面图 season绘图 panda绘图 待办 enter description here 转化成网格坐标 season可以让绘图更美观 panda绘图可以直接根据panda数据绘制图直接p ...

  4. [lua]紫猫lua教程-命令宝典-L1-01-07. table表

    L1[table]01. table表的定义与赋值 小知识:声明表的例子 xx={}--创建一个空表xx --给这表的元素赋值 test="a" xx[test]="a& ...

  5. eli和字符串 (牛客假期训练)

    链接:https://ac.nowcoder.com/acm/contest/3002/G来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言5242 ...

  6. mediasoup-demo安装记录

    环境CentOS 7 64位 VMWare12虚拟机(win10主机),安装好NodeJS 10.13(大于8.9就可以) 已按照GitHub说明拉下来代码,配置好Node环境,开始执行npm sta ...

  7. python的类定义与实例化

    理解类属性和实例属性: 直接在类里面定义的变量叫类属性,类属性是公有的,每个类实例化就自动拥有类的属性,且实例化对象的这个属性的初始地址指向类属性的地址 如果直接给实例化对象的属性赋值这样会改变该属性 ...

  8. 在远程连接mysql数据库出现问题怎么办

    远程连接mysql数据库报“Communications link failure...”错误 今天在用myEclipse连接时提示:Communications link failure,Last ...

  9. 6_14 Abbott的复仇(UVa816)<图的最短路BFS>

    1999次世界总决赛的比赛包括一个骰子迷宫问题.在这个问题被写的时候,法官们无法发现骰子迷宫概念的原始来源.不久之后的比赛,但是,罗伯特先生雅培,无数的迷宫和对作者的创造者主题,联系大赛评委,自称是骰 ...

  10. Logarithmic-Trigonometric积分系列(二)

    \[\Large\displaystyle \int_0^{\pi/2}\ln^2(\sin x)\ln(\cos x)\tan x \,{\rm d}x\] \(\Large\mathbf{Solu ...