-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString1309.java
More file actions
60 lines (59 loc) · 1.83 KB
/
String1309.java
File metadata and controls
60 lines (59 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package string;
/**
* @ProjectName: leetcode
* @Package: string
* @ClassName: String1309
* @Author: markey
* @Description:
* 给你一个字符串 s,它由数字('0' - '9')和 '#' 组成。我们希望按下述规则将 s 映射为一些小写英文字符:
*
* 字符('a' - 'i')分别用('1' - '9')表示。
* 字符('j' - 'z')分别用('10#' - '26#')表示。
* 返回映射之后形成的新字符串。
*
* 题目数据保证映射始终唯一。
*
*
*
* 示例 1:
*
* 输入:s = "10#11#12"
* 输出:"jkab"
* 解释:"j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".
* 示例 2:
*
* 输入:s = "1326#"
* 输出:"acz"
* 示例 3:
*
* 输入:s = "25#"
* 输出:"y"
* 示例 4:
*
* 输入:s = "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#"
* 输出:"abcdefghijklmnopqrstuvwxyz"
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/decrypt-string-from-alphabet-to-integer-mapping
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/2/8 21:26
* @Version: 1.0
*/
public class String1309 {
public static void main(String[] args) {
System.out.println(freqAlphabets( "1326#"));
}
public static String freqAlphabets(String s) {
StringBuilder sb = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == '#') {
sb.insert(0, (char)('j' + Integer.parseInt(s.substring(i-2, i)) - 10));
i -= 2;
} else {
System.out.println((char)('a' + Integer.parseInt(s.substring(i, i+1)) - 1));
sb.insert(0, (char)('a' + Integer.parseInt(s.substring(i, i+1)) - 1));
}
}
return sb.toString();
}
}