【2024华为OD-E卷-200分-评论转换输出】(题目+思路+Java&C++&Python解析)

题目

评论转换输出

给定一个评论字符串数组 comments 和一个整数 maxLength。对于每个评论,如果评论的长度大于 maxLength,则需要将其进行截断处理,并在截断处添加省略号 "..."。如果评论长度小于或等于 maxLength,则直接输出原评论。

要求:

  1. 截断位置应在单词边界上,即不能在一个单词的中间截断。
  2. 如果在截断位置前恰好是空格,该空格也需要保留。

输入

  • comments:字符串数组,每个字符串代表一条评论。
  • maxLength:整数,表示最大长度限制。

输出

  • 字符串数组,每个字符串是处理后的评论。

示例

输入:

comments = ["This is a sample sentence.", "Another longer comment here to test.", "Short comment."]
maxLength = 15

输出:

["This is a...", "Another longer...", "Short comment."]

思路

  1. 遍历每个评论字符串。
  2. 如果评论长度小于或等于 maxLength,直接添加到结果数组中。
  3. 如果评论长度大于 maxLength,则需要进行截断处理:
    • 从后向前遍历评论字符串,找到第一个空格的位置(且该位置之前的子串长度小于等于 maxLength - 3,以留出空间添加省略号)。
    • 如果找到这样的空格,则在该空格处截断并添加省略号。
    • 如果没有找到空格,则在 maxLength - 3 处截断并添加省略号。
  4. 将处理后的评论添加到结果数组中。

Java 代码解析

import java.util.ArrayList;
import java.util.List;

public class CommentConverter {
    public static String[] convertComments(String[] comments, int maxLength) {
        List result = new ArrayList<>();
        for (String comment : comments) {
            if (comment.length() <= maxLength) {
                result.add(comment);
            } else {
                int truncateIndex = maxLength - 3;
                while (truncateIndex > 0 && comment.charAt(truncateIndex) != ' ') {
                    truncateIndex--;
                }
                if (truncateIndex == 0 && comment.charAt(truncateIndex) != ' ') {
                    truncateIndex = maxLength - 3;
                }
                String truncatedComment = comment.substring(0, truncateIndex >= 0 ? truncateIndex : 0) + "...";
                result.add(truncatedComment);
            }
        }
        return result.toArray(new String[0]);
    }

    public static void main(String[] args) {
        String[] comments = {"This is a sample sentence.", "Another longer comment here to test.", "Short comment."};
        int maxLength = 15;
        String[] result = convertComments(comments, maxLength);
        for (String comment : result) {
            System.out.println(comment);
        }
    }
}

C++ 代码解析

#include
#include
#include

std::vector convertComments(const std::vector& comments, int maxLength) {
    std::vector result;
    for (const auto& comment : comments) {
        if (comment.length() <= maxLength) {
            result.push_back(comment);
        } else {
            int truncateIndex = maxLength - 3;
            while (truncateIndex > 0 && comment[truncateIndex] != ' ') {
                truncateIndex--;
            }
            if (truncateIndex == 0 && comment[truncateIndex] != ' ') {
                truncateIndex = maxLength - 3;
            }
            std::string truncatedComment = comment.substr(0, truncateIndex >= 0 ? truncateIndex : 0) + "...";
            result.push_back(truncatedComment);
        }
    }
    return result;
}

int main() {
    std::vector comments = {"This is a sample sentence.", "Another longer comment here to test.", "Short comment."};
    int maxLength = 15;
    std::vector result = convertComments(comments, maxLength);
    for (const auto& comment : result) {
        std::cout << comment << std::endl;
    }
    return 0;
}

Python 代码解析

def convert_comments(comments, maxLength):
    result = []
    for comment in comments:
        if len(comment) <= maxLength:
            result.append(comment)
        else:
            truncate_index = maxLength - 3
            while truncate_index > 0 and comment[truncate_index] != ' ':
                truncate_index -= 1
            if truncate_index == 0 and comment[truncate_index] != ' ':
                truncate_index = maxLength - 3
            truncated_comment = comment[:truncate_index if truncate_index >= 0 else 0] + "..."
            result.append(truncated_comment)
    return result

# 测试
comments = ["This is a sample sentence.", "Another longer comment here to test.", "Short comment."]
maxLength = 15
result = convert_comments(comments, maxLength)
for comment in result:
    print(comment)

你可能感兴趣的:(2024华为OD-E卷,java,华为od,c++,算法)