leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
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的更多相关文章
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
- leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- 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 ...
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- 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 ...
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
随机推荐
- [NOIP2017(TG/PJ)] 真题选做
[NOIPTG2017] 小凯的疑惑 题意 小凯有两种面值的金币,每种金币有无数个,求在无法准确支付的物品中,最贵的价值是多少金币. 分析 设两种金币面值分别为 $a$ 和 $b \; (a<b ...
- Quartus ii 初学遇到的问题以及解决
第一次下载和运行Quartus后,发现几个问题: 下载时安装易出现问题. 解决:在官网下载后,在开始破解: 运行程序出现警告:RTL波形时出现问题? 解决:testbench程序出错. 问题3:在视频 ...
- sublime添加自己的编译环境_添加一个.app或者.exe文件执行脚本
如何添加一个.app或者.exe文件执行脚本 看了很多简书和博客,还是搞不好,最后参考官方文档搞定了: http://www.sublimetext.com/docs/3/build_systems. ...
- nginx 定义:响应头和请求头
1) 响应头 add_header 例如: add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; a ...
- C. Grid game 思维+问题转化
C. Grid game 思维+问题转化 题意 每当有一行或者一列方格的时候,都可以消气这一行或者这一列,一共有两种形状的方块,一种是横的两个,一种是竖着的两个,按时间顺序放在4*4的格子里面,问怎么 ...
- python 小故事1
def test(a:str,b:int)->str: print(test.__annotations__) return a+str(b) def doc_print(): "&q ...
- C++ split分割字符串函数
将字符串绑定到输入流istringstream,然后使用getline的第三个参数,自定义使用什么符号进行分割就可以了. #include <iostream> #include < ...
- 如何通过源码包的方式在linux安装python36
背景: python34的安装非常简单,直接用yum就可以安装,但是安装最新版的python36通过yum方式是不行的,需要通过源码包进行安装 具体步骤如下: 1.安装openssl静态库[pip3安 ...
- 046_使用Scanner获得键盘输入 047_控制语句介绍 048_控制语句_if单选择结构 049_ifelse双选择结构 050_ifelseifelse多选择结构
046_使用Scanner获得键盘输入 package test_package;import java.util.Scanner;/** * 测试获得键盘输入 * @author * */publi ...
- 【Python】爬虫原理
前言 简单来说互联网是由一个个站点和网络设备组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现我们眼前: 一.爬虫是什 ...