LeetCode(1678) -- 设计Goal解析器
发布日期:2021-05-10 07:48:52 浏览次数:12 分类:精选文章

本文共 3592 字,大约阅读时间需要 11 分钟。

Goal ���������������������

Goal ������������������������

Goal ��������������������������������������������� command ������������������������������������������������������������ "G"���()`` ���(al)` ���������������������������������������������Goal ���������������������������������������������

  • ������ "G" ������������ "G"���
  • () ������������ "o"���
  • ������ "(al)" ������������ "al"���
  • ������������������������������������������������������������������������������������������������������������

    ������������������

    Goal ������������������������������������ command ���������������������������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������
  • ��������������������������� () ��� (al) ������������������������������������������������������������������������������������������2������������3���������������������������������������������������������������
    • ��������� () ��������������� "o"������������������������������ () ���������
    • ��������� (al) ��������������� "al"������������������������������ a ��� l ���������
  • ������������������������������������������������������������������������������������������������
  • ������������������������

    ���������Goal ���������������������������������������������Java������������

    class Solution {    public String interpret(String command) {        String result = "";        for (int index = 0; index < command.length(); index++) {            if (command.charAt(index) == 'G') {                result += "G";                continue;            } else if (command.charAt(index) == '(' && command.charAt(index + 1) == ')') {                result += "o";                index++;                continue;            } else if (command.charAt(index) == '(' && command.charAt(index + 1) == 'a') {                result += "al";                index += 3;                continue;            }            // ���������������������������������������������������������������            result += command.charAt(index);            index++;        }        return result;    }}

    ���������������

    ������1

    ���������command = "G()(al)"���������"Goal"

    ���������������

    • ������0��������� "G" ��� ��������� "G"���
    • ������1���"(" ���������2���")" ��� ��������� ()������������ "o"���
    • ������3���"("���������4���"a"���������5���"l" ��� ��������� (al)������������ "al"���

    ���������������G + "o" + "al" = "Goal"���

    ������2

    ���������command = "G()()()()(al)"���������"Gooooal"

    ���������������

    • ������ () ��������������� "o"���
    • ������������ (al) ��������� "al"���

    ������������������������"G"+"o"+"o"+"o"+"o"+"al" = "Gooooal"���

    ������3

    ���������command = "(al)G(al)()()G"���������"alGalooG"

    ���������������

    • (al) ��� "al".
    • "G" ��� "G".
    • (al) ��� "al".
    • "()" ��� "o".
    • "()" ��� "o".
    • ��������� "G" ��� "G".

    ���������������"al"+"G"+"al"+"o"+"o"+"G" = "alGalooG"���

    ������������������������

  • ������������������������������������������������������ () ��� (al) ���������������������������������������
  • ������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������
  • ������������

  • ���������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������
  • 上一篇:LeetCode(232)--用栈实现队列
    下一篇:LeetCode(338)--比特位计数

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年04月11日 14时19分58秒