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. 关于jquery改变onclick方法,最保险的做法

    function a(){ alert("a"); } function b(){ alert("b"); } <input type="but ...

  2. Python | 面向对象中的名词总结

    一.变量名称 (最前的序号表示将来用到的频繁程度) 5 全局变量: 1. 文件中出现的变量,不归属于函数及类:2.在函数用用global声明的变量 2 局部变量: 1.类的方法中没有self.,只能在 ...

  3. 用git无法连接github的解决方法

    如果要從 GitHub 存取 Git 儲存庫,建議還是多採用 SSH 與 HTTPS 通訊協定最為穩定可靠,因此我的替代方案就是設定 Git 的全域設定值 ( –global ),預設將所有 git: ...

  4. Linux上后台运行node和springboot服务

    环境:Ubuntu18.04 阿里云云服务器 尝试全局安装forever和pm2均失败,最后以linux自带的nohub启动,以前同样用nohub启动springboot 命令: nohup npm ...

  5. 牛客腾讯2020校园招聘-后台&综合-第一次笔试

    第一题 Q: 小Q想要给他的朋友发送一个神秘字符串,但是他发现字符串的过于长了,于是小Q发明了一种压缩算法对字符串中重复的部分进行了压缩,对于字符串中连续的m个相同字符串S将会压缩为m|S,例如字符串 ...

  6. css3之渐变背景色(linear-gradient)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 【Thinkphp】记录一次分页的实现

    thinkphp分页非常简单 1,控制器渲染数据: $studentList = StudentDb::paginate(5); $this->view->assign('list',$s ...

  8. 计算几何-多边形内核判定-HPI-poj3335

    This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 先解决一个问题, ...

  9. Swagger与OAuth 手动搭建WebApi 操作笔记

    1.创建一个空的Web应用程序 2.通过nuget 安装以下插件清单,有部分会在安装其他插件时候自动安装: 3.安装完Swagger 会生成一个目录App_Start,在这个目录中增加文件ApiCon ...

  10. Chrome - 使用 开发者工具 对页面截图

    概述 使用 开发者工具 对页面截图 背景 经常需要截图 常用的截图模式有这些 窗口截图 区域截图 gif 问题 Chrome 如何截长图 firefox 好像有插件 1. 解决: 使用 Chrome ...