栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

leetcode 722. Remove Comments | 722. 删除注释(Java)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode 722. Remove Comments | 722. 删除注释(Java)

题目

题解

测试用例太恶心了,受不鸟啊

class Solution {
    public static final char NO_NEW_LINE = ''';

    public List removeComments(String[] source) {
        boolean pending = false;
        List result = new ArrayList<>();
        for (int i = 0; i < source.length; i++) {
            if (!pending) { // 左注释符已闭合
                int i1 = source[i].indexOf("//");
                if (i1 < 0) i1 = source[i].length();

                int i2 = source[i].indexOf("");
                if (i2 >= 0) {
                    source[i] = source[i].substring(i2 + 2);
                    pending = false;
                    i--;
                }
            }
        }
        return result;
    }

    public void add(List result, String s) {
        if (!result.isEmpty()) {
            String tail = result.get(result.size() - 1);
            int len = tail.length();
            if (tail.charAt(len - 1) == NO_NEW_LINE) { // implicit newline characters can be deleted by block comments
                result.set(result.size() - 1, tail.substring(0, len - 1) + s);
                return;
            }
        }
        if (s.length() > 0) result.add(s);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号