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 ...
随机推荐
- vue里不同数据的循环,其中的数组对象
用产品的属性数据说明 页面里显示效果为:要把产品的属性显示到页面上,产品属性为后台自主上传产品的属性,产品的属性不同,所以需要把属性和属性值显示到页面上 产品属性数据为: properties: &q ...
- ts中的类
TypeScript 除了实现了所有 ES6 中的类的功能以外,还添加了一些新的用法(部分ES7). 一.ES6中类的主要用法: 1.使用 class 定义类,使用 constructor 定义构造函 ...
- java多线程--死锁
1. Java中导致死锁的原因 Java中死锁最简单的情况是,一个线程T1持有锁L1并且申请获得锁L2,而另一个线程T2持有锁L2并且申请获得锁L1,因为默认的锁申请操作都是阻塞的,所以线程T1和T2 ...
- Docker - 查看容器进程在宿主机的 PID
概述 查看 docker 进程, 在容器外的 pid 背景 docker 中运行的进程, 本质上是运行在 host 上的 这些进程, 在 host 上, 也可以有自己的 pid 如果某种情况下, 连不 ...
- 字符串判空有空格报错:binary operator expected
使用-z或-n对一个变量判空时,需要注意若直接使用[ -n ${ARG} ]这种形式,若${ARG}中有空格将会报错, #!/bin/bash ARG="sd dd" if [ - ...
- DockerFile执行报错解决
错误1: “docker build” requires exactly 1 argument.原因: 之前的命令是这样的: docker build -t nbCentos:1.0.0 , 不仔细看 ...
- es8中对string补白的方式
//允许将空字符串或其他字符串添加到原始字符串的开头或结尾for(let i = 1; i < 32; i++) { if(i < 10) { console.log(`0{i}`) }e ...
- C#堆和栈的入门理解
声明:以下内容从网络整理,非原创,适当待入个人理解. 解释1.栈是编译期间就分配好的内存空间,因此你的代码中必须就栈的大小有明确的定义:堆是程序运行期间动态分配的内存空间,你可以根据程序的运行情况确定 ...
- itest(爱测试) 4.3.1 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
4.3.0 发布后有三个用户强烈要求的更新,所以一周后4.3.1出炉,有点版本帝的味道哈,用户的反馈是我们持续升级的动力...... itest 简介:查看简介 test 开源敏捷测试管理,testO ...
- oracle11g UNDO使用率高,且unexpire过高
1.查看使用率: col USED_PER for a60 SELECT A.TABLESPACE_NAME AS TABLESPACE_NAME, ,) AS TOTAL_GB, ,) AS FRE ...