友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

394. Decode String

Given an encoded string, return its decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].

Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

思路分析

主要就是一个栈操作。不过,还需要注意一些细节。

  • 一刷

  • 二刷

 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
/**
 * Runtime: 1 ms, faster than 64.03% of Java online submissions for Decode String.
 * Memory Usage: 37.4 MB, less than 5.68% of Java online submissions for Decode String.
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-01-28 22:08
 */
public String decodeString(String s) {
    if (Objects.isNull(s) || s.length() == 0) {
        return "";
    }
    Deque<Integer> nums = new LinkedList<>();
    Deque<String> strings = new LinkedList<>();
    int num = 0;
    StringBuilder result = new StringBuilder();
    for (char c : s.toCharArray()) {
        if (c == '[') {
            nums.addLast(num);
            strings.addLast(result.toString());
            num = 0;
            result = new StringBuilder();
        } else if (c == ']') {
            StringBuilder sb = new StringBuilder();
            Integer n = nums.removeLast();
            for (int i = 0; i < n; i++) {
                sb.append(result);
            }
            result = new StringBuilder(strings.removeLast() + sb.toString());
        } else if (Character.isDigit(c)) {
            num = num * 10 + (c - '0');
        } else if (Character.isLetter(c)) {
            result.append(c);
        }
    }
    return result.toString();
}
 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
/**
 * @author D瓜哥 · https://www.diguage.com
 * @since 2024-09-18 16:05:13
 */
int idx = 0;
public String decodeString(String s) {
  StringBuilder sb = new StringBuilder();
  while (idx < s.length()) {
    char c = s.charAt(idx);
    if ('0' <= c && c <= '9') {
      sb.append(dfs(s));
    } else {
      sb.append(c);
      idx++;
    }
  }
  return sb.toString();
}

private String dfs(String s) {
  int cnt = 0;
  boolean isEnd = false;
  StringBuilder sb = new StringBuilder();
  while (idx < s.length()) {
    char c = s.charAt(idx);
    if ('0' <= c && c <= '9') {
      if (isEnd) {
        sb.append(dfs(s));
        continue;
      }
      int num = c - '0';
      if (cnt == 0) {
        cnt = num;
      } else {
        cnt = 10 * cnt + num;
      }
      idx++;
    } else if ('a' <= c && c <= 'z') {
      sb.append(c);
      idx++;
    } else if (c == '[') {
      isEnd = true;
      idx++;
    } else if (s.charAt(idx) == ']') {
      idx++;
      break;
    }
  }
  StringBuilder result = new StringBuilder();
  for (int j = 0; j < cnt; j++) {
    result.append(sb);
  }
  return result.toString();
}