柠檬微趣笔试 柠檬微趣笔试题 0727
笔试时间:2025年7月27日
往年笔试合集:
第一题
给定一个单链表,两两交换相邻的节点,并返回链表的头指针。
注意: 不允许借助空节点交换链表,借助空节点的答案将作为无效答案。不允许使用递归,使用递归的答案将作为无效答案。链表创建完成后,只允许更改节点的指向,而不可以更改节点的值,更改节点值的答案将作为无效答案。
输入描述
给定每一个链表的值(以-1作为结尾),笔试者需要手动创建链表并完成节点的交换,并正确进行内存管理。
输出描述
链表的每一个节点值。
样例输入
1 2 3 4 -1
样例输出
2 1 4 3
参考题解
翻转链表
C++:
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct Node
{
int value;
Node* next;
Node(int val, Node* next) :value(val), next(next) {}
};
Node* reverse(Node* head)
{
if (head == nullptr || head->next == nullptr)
return head;
Node* root = new Node(0, head);
Node* pre = root;
while (pre->next != nullptr && pre->next->next != nullptr && pre->next->value != -1) {
Node* now = pre->next;
Node* nxt = now->next;
now->next = nxt->next;
nxt->next = now;
pre->next = nxt;
pre = now;
}
Node* ans = root->next;
delete root;
return ans;
}
signed main() {
int cnt;
Node* pre = new Node(0, nullptr);
Node* head = pre;
while (cin >> cnt && cnt != -1) {
Node* node = new Node(cnt, nullptr);
pre->next = node;
pre = node;
}
Node* ans = reverse(head->next);
delete head;
while (ans != nullptr)
{
Node* temp = ans;
cout << ans->value << " ";
ans = ans->next;
delete temp;
}
return 0;
}
// 64 位输出请用 printf("%lld")
Java:
import java.io.*;
import java.util.*;
public class Main {
static class Node {
long value;
Node next;
Node(long v, Node n) { value = v; next = n; }
}
// Swap adjacent nodes in pairs: 1 2 3 4 5 -> 2 1 4 3 5
static Node swapPairs(Node head) {
Node dummy = new Node(0L, head);
Node pre = dummy;
while (pre.next != null && pre.next.next != null) {
Node now = pre.next;
Node nxt = now.next;
// perform local swap
now.next = nxt.next;
nxt.next = now;
pre.next = nxt;
// advance
pre = now;
}
return dummy.next;
}
public static void main(String[] args) throws Exception {
// Read all integers from stdin until -1
FastScanner fs = new FastScanner(System.in);
Node dummy = new Node(0L, null);
Node tail = dummy;
while (true) {
Long v = fs.nextLongOrNull();
if (v == null) break;
if (v == -1L) break;
tail.next = new Node(v, null);
tail = tail.next;
}
Node ans = swapPairs(dummy.next);
StringBuilder out = new StringBuilder();
for (Node p = ans; p != null; p = p.next) {
out.append(p.value).append(' ');
}
// Print exactly like the C++ program (trailing space is fine)
System.out.print(out.toString());
}
// Lightweight fast scanner
static class FastScanner {
private final InputStream in;
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
FastScanner(InputStream is) { in = is; }
private int read() throws IOException {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0) return -1;
}
return buffer[ptr++];
}
Long nextLongOrNull() throws IOException {
int c;
do {
c = read();
if (c == -1) return null;
} while (c <= ' ');
int sign = 1;
if (c == '-') { sign = -1; c = read(); }
long val = 0;
while (c > ' ') {
val = val * 10 + (c - '0');
c = read();
}
return sign == 1 ? val : -val;
}
}
}
Python:
# Swap adjacent nodes in pairs; read integers until -1, then print the list.
import sys
class Node:
__slots__ = ("value", "next")
def __init__(self, value, next=None):
self.value = value
self.next = next
def swap_pairs(head: Node) -> Node:
dummy = Node(0, head)
pre = dummy
while pre.next is not None and pre.next.next is not None:
now = pre.next
nxt = now.next
# local swap
now.next = nxt.next
nxt.next = now
pre.next = nxt
# advance
pre = now
return dummy.next
def main():
data = sys.stdin.read().strip().split()
# Build list until -1
dummy = Node(0, None)
tail = dummy
for tok in data:
try:
v = int(tok)
except ValueError:
continue
if v == -1:
break
tail.next = Node(v, None)
tail = tail.next
ans = swap_pairs(dummy.next)
# Print like the C++ code: values separated by a trailing space, no newline required
p = ans
out = []
while p is not None:
out.append(str(p.value))
p = p.next
if out:
sys.stdout.write(" ".join(out) + " ")
if __name__ == "__main__":
main()
第二题
给定一个字符串S,判断S是否为有效字符申。有效的标准如下:字符串 a bc有效。对于一个有效字符串S,S=X+Y(+号为连接符,X,Y可有一个为空),那么T=X+abc+Y 也为有效字符串。如果字符串有效,返国true,否则返回 false。
输入描述
输入字符串S。
输出描述
字符串是否有效。
样例输入
aabcbc
样例输出
true
参考题解
我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 "abc",所以每次插入操作之后,字符串的长度都会增加 3。如果字符串 s 有效,那么它的长度一定是 3 的倍数。因此,我们先对字符串 s 的长度进行判断,如果不是 3 的倍数,那么 s 一定无效,可以直接返回 false。接下来我们遍历字符串 s 的每个字符 c,我们先将字符 c 压入栈 t 中。如果此时栈 t 的长度大于等于 3,并且栈顶的三个元素组成了字符串 "abc",那么我们就将栈顶的三个元素弹出。然后继续遍历字符串 s 的下一个字符。遍历结束之后,如果栈 t 为空,那么说明字符串 s 有效,返回 true,否则返回 false。
C++:
class Solution {
public:
bool isValid(string s) {
if (s.size() % 3) {
returnfalse;
}
string t;
for (char c : s) {
t.push_back(c);
if (t.size() >= 3 && t.substr(t.size() - 3, 3) == "abc") {
t.erase(t.end() - 3, t.end());
}
}
return t.empty();
}
};
Java:
class Solution {
public boolean isValid(String s) {
if (s.length() % 3 != 0) return false;
StringBuilder t = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
t.append(c);
int n = t.length();
if (n >= 3 && t.charAt(n - 3) == 'a' && t.charAt(n - 2) == 'b' && t.charAt(n - 1) == 'c') {
t.setLength(n - 3);
}
}
return t.length() == 0;
}
}
Pyt
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南
