- 0
- 0
- 约7.36千字
- 约 22页
- 2026-02-08 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年应用开发中心咨询顾问技术面试题库更新含答案
一、编程语言基础(3题,每题10分)
1.题目:
请用Java编写一个方法,实现将一个字符串中的所有空格替换为%20。要求时间复杂度为O(n)。
答案:
java
publicclassStringReplace{
publicstaticStringreplaceSpaces(Strings){
if(s==null||s.length()==0)returns;
char[]chars=s.toCharArray();
intspaceCount=0;
for(charc:chars){
if(c==)spaceCount++;
}
char[]newChars=newchar[chars.length+spaceCount2];
intj=0;
for(inti=0;ichars.length;i++){
if(chars[i]==){
newChars[j++]=%;
newChars[j++]=2;
newChars[j++]=0;
}else{
newChars[j++]=chars[i];
}
}
returnnewString(newChars,0,j);
}
publicstaticvoidmain(String[]args){
Stringinput=HelloWorld;
System.out.println(replaceSpaces(input));//输出:Hello%20World
}
}
解析:
-首先统计原字符串中空格的数量,以便计算新字符串的长度。
-使用双指针法,一个指针遍历原字符串,另一个指针构建新字符串,空格替换为%20。
-时间复杂度为O(n),空间复杂度为O(n),其中n为原字符串长度。
2.题目:
请用Python实现一个函数,检查一个链表是否为回文链表。假设链表节点定义如下:
python
classListNode:
def__init__(self,val=0,next=None):
self.val=val
self.next=next
答案:
python
classListNode:
def__init__(self,val=0,next=None):
self.val=val
self.next=next
defisPalindrome(head:ListNode)-bool:
ifnotheadornothead.next:
returnTrue
找到链表中间节点
slow=head
fast=head
whilefastandfast.next:
slow=slow.next
fast=fast.next.next
反转后半部分链表
prev=None
whileslow:
next_node=slow.next
slow.next=prev
prev=slow
slow=next_node
比较前后两部分
left,right=head,prev
whileright:#只需比较后半部分是否等于前半部分
ifleft.val!=right.val:
returnFalse
left=left.next
right=right.next
returnTrue
解析:
-使用快慢指针找到链表中间节点,将链表分为前后两部分。
-反转后半部分链表,便于比较。
-逐个比较前后两部分节点的值,若全部相等则为回文链表。
-时间复杂度为O(n),空间复杂度为O(1)。
3.题目:
请用C#编写一个方法,实现快速排序算法。输入一个整数数组,返回排序后的数组。
答案:
csharp
usingSystem;
publicclassQuickSortExample{
publicstaticint[]QuickSort(int[]arr){
if(arr==null||arr.Length=1)returnarr;
QuickSortInternal(arr,0,arr.Length-1);
returnarr;
}
privatestaticvoidQuickSortInternal(int[]arr,intleft,intright){
if(left=right)return;
intpiv
原创力文档

文档评论(0)