首页 > 网易笔试 网易秋招 网易笔试题 1012
头像
林小白zii
发布于 昨天 02:53 上海
+ 关注

网易笔试 网易秋招 网易笔试题 1012

笔试时间:2025年10月12日

往年笔试合集:

2023春招秋招笔试合集

2024春招秋招笔试合集

第一题:简单模板引擎校验

假设需要设计一个模板渲染引擎,给定一个字符串,判断该输入的模板字符串是否合法有效。模板引擎采用双花括号匹配方式,用于替换中间的变量。

有效字符串需满足:

  1. 必须采用相邻双花括号方式进行匹配,并且要求是闭合的,例如 {{a}} 合法, {a} 不合法
  2. 双花括号之间必须有有效字符,不可以只包含空格,例如 {{ }} 不合法
  3. 在双花括号之间存在有效字符的情况下,允许存在空格,例如 {{ a }} 合法
  4. 允许出现多个模板匹配,但不允许嵌套,例如 {{a}} {{b}} 合法, {{{a}} b}} 不合法, {{a {{b}}}} 不合法

备注:

  • 每次仅仅输入一个模板字符串
  • 如果不包含任何双花括号,认定为有效模板

输入描述

输入为需要判断有效性的字符串模板

输出描述

输出是否为有效字符串,布尔值,有效返回 true,无效返回 false

样例输入

this is a {{question}}

样例输出

true

双花括号匹配,符合要求。

参考题解

解题思路:

  1. 逐个字符遍历输入字符串
  2. 寻找起始标记{{,如果找到单个{则判定无效
  3. 找到{{后继续查找对应的}},期间检查是否有嵌套
  4. 提取双花括号之间的内容,检查是否包含至少一个非空格字符
  5. 处理不成对的括号情况

C++:

#include <iostream>
#include <string>
using namespace std;

bool isValidTemplate(string template_str) {
    int strLen = template_str.length();
    int index = 0;
    
    while (index < strLen) {
        if (template_str[index] == '{') {
            if (index + 1 >= strLen || template_str[index + 1] != '{') {
                return false;
            }
            index += 2;
            int contentStartIdx = index;
            bool foundClosingBrackets = false;
            
            while (index < strLen - 1) {
                if (template_str[index] == '{' && template_str[index + 1] == '{') {
                    return false;
                }
                if (template_str[index] == '}' && template_str[index + 1] == '}') {
                    int contentEndIdx = index - 1;
                    bool hasNonSpaceChar = false;
                    for (int contentIdx = contentStartIdx; contentIdx <= contentEndIdx; contentIdx++) {
                        if (template_str[contentIdx] != ' ') {
                            hasNonSpaceChar = true;
                            break;
                        }
                    }
                    if (!hasNonSpaceChar) {
                        return false;
                    }
                    index += 2;
                    foundClosingBrackets = true;
                    break;
                }
                index++;
            }
            if (!foundClosingBrackets) {
                return false;
            }
        } else if (template_str[index] == '}') {
            return false;
        } else {
            index++;
        }
    }
    return true;
}

int main() {
    string templateStr;
    getline(cin, templateStr);
    cout << (isValidTemplate(templateStr) ? "true" : "false") << endl;
    return 0;
}

Java:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String templateStr = scanner.nextLine();
        System.out.println(isValidTemplate(templateStr));
        scanner.close();
    }
    
    private static boolean isValidTemplate(String template) {
        int strLen = template.length();
        int index = 0;
        
        while (index < strLen) {
            if (template.charAt(index) == '{') {
                if (index + 1 >= strLen || template.charAt(index + 1) != '{') {
                    return false;
                }
                index += 2;
                int contentStartIdx = index;
                boolean foundClosingBrackets = false;
                
                while (index < strLen - 1) {
                    if (template.charAt(index) == '{' && template.charAt(index + 1) == '{') {
                        return false;
                    }
                    if (template.charAt(index) == '}' && template.charAt(index + 1) == '}') {
                        int contentEndIdx = index - 1;
                        boolean hasNonSpaceChar = false;
                        for (int contentIdx = contentStartIdx; contentIdx <= contentEndIdx; contentIdx++) {
                            if (template.charAt(contentIdx) != ' ') {
                                hasNonSpaceChar = true;
                                break;
                            }
                        }
                        if (!hasNonSpaceChar) {
                            return false;
                        }
                        index += 2;
                        foundClosingBrackets = true;
                        break;
                    }
                    index++;
                }
                if (!foundClosingBrackets) {
                    return false;
                }
            } else if (template.charAt(index) == '}') {
                return false;
            } else {
                index++;
            }
        }
        return true;
    }
}

Python:

def is_valid_template(template):
    str_len = len(template)
    index = 0
    
    while index < str_len:
        if template[index] == '{':
            if index + 1 >= str_len or template[index + 1] != '{':
                return False
            index += 2
            content_start_idx = index
            found_closing_brackets = False
            
            while index < str_len - 1:
                if template[index] == '{' and template[index + 1] == '{':
                    return False
                if template[index] == '}' and template[index + 1] == '}':
                    content_end_idx = index - 1
                    has_non_space_char = False
                    for content_idx in range(content_start_idx, content_end_idx + 1):
                        if template[content_idx] != ' ':
                            has_non_space_char = True
                            break
                    if not has_non_space_char:
                        return False
                    index += 2
                    found_closing_brackets = True
                    break
                index += 1
            
            if not found_closing_brackets:
                return False
        elif template[index] == '}':
            return False
        else:
            index += 1
    
    return True

template_str = input()
print("true" if is_valid_template(template_str) else "false")

第二题:最长公共子序列

给定两个字符串 s1 和 s2,返回这两个字符串的最长公共子序列的长度。如果不存在公共子序列,返回 0。

一个字符串的子序列是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。

输入描述

两个字符串,s1 和 s2

输出描述

返回这两个字符串的最长公共子序列的长度

样例输入

abcde

ace

样例输出

3

参考题解

解题思路:使用动态规划解决:

  1. 定义状态:dp[i][j]表示字符串s1的前i个字符和字符串s2的前j个字符的最长公共子序列长度
  2. 状态转移: 如果s1[i-1] == s2[j-1],则dp[i][j] = dp[i-1][j-1] + 1否则,dp[i][j] = max(dp[i-1][j], dp[i][j-1])

C++:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    string text1, text2

全部评论

(1) 回帖
加载中...
话题 回帖
历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

热门推荐