3324. Find the Sequence of Strings Appeared on the Screen - Medium

前往題目

想法

  • 先把第一個字母當基準點準備好,再來就直接循環建立剩餘的字母

思路

weekly contest 420的題目

完賽後再看一遍其實直接循環建立就好

  1. 循環把字母建立好
  2. 先放入a,然後加到結果,接著比較字母是否和目標的一樣,不是的話ascii + 1再加入結果再比較

Code

class Solution {
    public List<String> stringSequence(String target) {
        List<String> res = new ArrayList();
        StringBuilder sb = new StringBuilder("");

        for (int i = 0; i < target.length(); ++i) {
            char curChar = 'a';
            sb.append(curChar);
            res.add(sb.toString());

            int lastIndex = sb.length() - 1;

            // Build the current last char
            while (sb.charAt(lastIndex) != target.charAt(i)) {
                sb.setCharAt(lastIndex, (char) (sb.charAt(lastIndex) + 1));
                res.add(sb.toString());
            }

        }
        return res;
    }
}

3324. Find the Sequence of Strings Appeared on the Screen - Medium
https://f88083.github.io/2024/10/22/3324-Find-the-Sequence-of-Strings-Appeared-on-the-Screen-Medium/
作者
Simon Lai
發布於
2024年10月22日
許可協議