学习时间:
2023年1月29日
题目描述:
![](https://img-blog.csdnimg.cn/06a881f2d9df4671908a5ec3dbb45585.png)
题解分享:
/**
* @ Author 繁华倾夏
* @ Date 2023年01月28日
*/
// 力扣(LeetCode):1309. 解码字母到整数映射
public class Solution {
public static String freqAlphabets(String s) { // 调用函数
int len=s.length(); // 统计字符串长度
StringBuilder news = new StringBuilder(); // 初始化一个新的可变长度的字符串作为返回字符串
int i;
for(i=0;i<len;i++){ // 遍历字符串
if(i+2<len&&s.charAt(i+2)=='#'){ // 当有长度为3并且第三个位置为#时
news.append((char)((s.charAt(i)-'0')*10+(s.charAt(i+1)-'1')+'a')); // 将字符串转变后返回news,需要进行强制类型转换
i+=2; // 先转换为int相加,再转换为字符串
}else{
news.append((char)(s.charAt(i)-'1'+'a')); // 先转换为int相加,再转换为字符串
}
}
return news.toString(); // 返回字符串
}
// 测试用例
// 输入 s = "10#11#12"
// 输出 "jkab"
public static void main(String[] args) {
String s ="10#11#12";
String re=freqAlphabets(s);
System.out.println(re);
}
}
【繁华倾夏】【每日力扣题解分享】【Day15】