软件工程师面试题及技术要点详解.docxVIP

  • 1
  • 0
  • 约8.73千字
  • 约 26页
  • 2026-02-19 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年软件工程师面试题及技术要点详解

一、编程基础与数据结构(共5题,每题10分,总分50分)

题目1(10分)

题目:请实现一个函数,判断一个字符串是否是有效的括号组合。例如,输入()[]{}应返回true,输入(]应返回false。要求时间复杂度为O(n)。

答案:

java

publicbooleanisValid(Strings){

if(s==null||s.length()==0)returntrue;

StackCharacterstack=newStack();

MapCharacter,Charactermap=newHashMap();

map.put(),();

map.put(},{);

map.put(],[);

for(charc:s.toCharArray()){

if(map.containsKey(c)){

if(stack.isEmpty()||stack.pop()!=map.get(c)){

returnfalse;

}

}else{

stack.push(c);

}

}

returnstack.isEmpty();

}

解析:

1.使用栈结构来存储未匹配的左括号

2.创建一个映射表来快速查找每个右括号对应的左括号

3.遍历字符串,遇到右括号时检查栈顶元素是否匹配

4.如果栈为空或栈顶元素不匹配则返回false

5.最终检查栈是否为空,为空则匹配成功

题目2(10分)

题目:给定一个整数数组,请找到其中最长连续递增子序列的长度。例如,在数组[10,9,2,5,3,7,101,18]中,最长连续递增子序列为[2,5,3,7,101],长度为5。

答案:

java

publicintfindLengthOfLCIS(int[]nums){

if(nums==null||nums.length==0)return0;

intmaxLen=1,currentLen=1;

for(inti=1;inums.length;i++){

if(nums[i]nums[i-1]){

currentLen++;

maxLen=Math.max(maxLen,currentLen);

}else{

currentLen=1;

}

}

returnmaxLen;

}

解析:

1.初始化最大长度和当前长度都为1

2.从第二个元素开始遍历

3.如果当前元素大于前一个元素,则当前长度增加

4.更新最大长度

5.如果不满足递增条件,重置当前长度为1

6.最终返回最大长度

题目3(10分)

题目:请实现一个LRU(最近最少使用)缓存机制,支持get和put操作。缓存容量为固定的,超出容量时需要淘汰最久未使用的元素。

答案:

java

classLRUCache{

privateintcapacity;

privateMapInteger,Nodecache;

privateNodehead,tail;

classNode{

intkey;

intvalue;

Nodeprev;

Nodenext;

Node(intkey,intvalue){

this.key=key;

this.value=value;

}

}

publicLRUCache(intcapacity){

this.capacity=capacity;

cache=newHashMap();

head=newNode(0,0);

tail=newNode(0,0);

head.next=tail;

tail.prev=head;

}

publicintget(intkey){

Nodenode=cache.get(key);

if(node==null)return-1;

moveToHead(node);

returnnode.value;

}

publicvoidput(intkey,intvalue){

Nodenode=cache.get(key);

if(node!=null){

node.value=value;

moveToHead(node);

}else{

NodenewNode=newNode(key,value);

cache.put(key,newNode);

addToHead(newNode);

if(cache.size

文档评论(0)

1亿VIP精品文档

相关文档