测试工程师社招-算法题

(太难了对于我,我只看了一小部分,侧开的话考的比较多)

LC1:两数之和,找一个数组中目标值等于val的两数下标

#暴力法
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        result = []
        for i in range(0, len(nums)):
            for j in range(i+1, len(nums)):
                total = nums[i] + nums[j]
                if total == target:
                    result.append(i);
                    result.append(j);                    
        return result
#哈希法
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        result = []
        mapping = {}
        for i in range(0, len(nums)):
            mapping[nums[i]] = i
        for j in range(0, len(nums)):
            diff = target - nums[j]
            if (diff in mapping and mapping[diff] != j):
                result.append(j);
                result.append(mapping[diff]);
                return result

LC2:两数相加,两个非空链表,两个非负整数,每个结点只能存储一位数字

9---9---9

9---9

8(进1)---9(进1)---0(进1)---1

要点:1、空链表接收result 2、依次遍历两个链表 l1.val+l2.val 3、next1 是否进1 4、添加剩余部分

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        total = 0
        next1 = 0
        res = ListNode()
        cur = res
        while (l1 != None and l2 != None):
            total = l1.val + l2.val + next1
            cur.next = ListNode(total % 10)
            next1 = total // 10
            cur = cur.next
            l1 = l1.next
            l2 = l2.next
        
        while l1 != None:
            total = l1.val + next1
            cur.next = ListNode(total % 10)
            next1 = total // 10
            cur = cur.next
            l1 = l1.next
        
        while l2 != None:
            total = l2.val + next1
            cur.next = ListNode(total % 10)
            next1 = total // 10
            cur = cur.next
            l2 = l2.next
        
        if next1 != 0:
            cur.next = ListNode(next1)
        
        return res.next

LC20:有效括号,只包括()[] {},判断字符串是否有效

要点:1、字符串为空,有效 2、左括号压栈 3、右括号,看是否匹配 4、剩余是否空栈

class Solution:
    def isValid(self, s: str) -> bool:
        if len(s) == 0:
            return True
        stack = []
        for c in s:
            if c=="(" or c=="[" or c=="{":
                stack.append(c)
            else:
                if len(stack) == 0:
                    return False
                else:
                    temp = stack.pop()
                    if c == ")":
                        if temp != "(":
                            return False
                    elif c == "]":
                        if temp != "[":
                            return False
                    elif c == "}":
                        if temp != "{":
                            return False
        return True if len(stack)==0 else False

LC21:合并两个有序链表

1---2---4

1---4---5

1---1---2---4---4---5

要点:递归法1、新建一个链表接收数据 2、比较两个数,谁小谁进

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        if l1 == None or l2 == None:
            return l1 or l2
        if l1.val <= l2.val:
            l1.next = self.mergeTwoLists(l1.next,l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1,l2.next)
            return l2

LC22:括号生成,回溯算法

要点:1、左括号的数量<=右括号的数量 2、左括号的数量<右括号的数量 (不可)3、左右括号的数量相等且等于N

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        result = []
        self.backtracking(n,result,0,0,"")
        return result
    def backtracking(self,n,result,left,right,s):
        if right>left:
            return
        if (left == right ==n):
            result.append(s)
            return
        if left<n:
            self.backtracking(n,result,left+1,right,s+"(")
        if right<left:
            self.backtracking(n,result,left,right+1,s+")")

LC24:两两交换链表中的节点

res - 1(head)--2(nxt)---3(tmp)---4

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head:ListNode) -> ListNode:
        if head == None or head.next==None
            return head
        res = ListNode()
        res.next= head
        cur = res
        while cur.next!=None and cur.next.next != None:
            nxt = head.next
            tmp = nxt.next
            cur.next = nxt
            nxt.next = head
            head.next = tmp
            cur = head
            head = head.next
        return res.next    

LC27:移除元素

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        if nums== None or len(nums) == 0:
            return 0
        l,r = 0,len(nums)-1
        while l<r:
            while l<r and nums[l]!=val:
                l+=1
            while l<r and nums[r]==val:
                r-=1
            nums[l],nums[r] =nums[r],nums[l]
        return l if nums[l]==val else l+1 

全部评论
佬 太强了!向佬学习
点赞 回复 分享
发布于 2025-08-26 17:31 北京
这个有没有完整的题库,可以发来看看不
点赞 回复 分享
发布于 2024-01-29 15:28 四川
项目该怎么说呢 不会总结
点赞 回复 分享
发布于 2024-01-21 23:03 北京

相关推荐

03-01 19:30
已编辑
南京大学 Java
点赞 评论 收藏
分享
行云流水1971:这份实习简历的优化建议: 结构清晰化:拆分 “校园经历”“实习经历” 板块(当前内容混杂),按 “实习→校园→技能” 逻辑排版,求职意向明确为具体岗位(如 “市场 / 运营实习生”)。 经历具象化:现有描述偏流程,需补充 “动作 + 数据”,比如校园活动 “负责宣传” 可加 “运营公众号发布 5 篇推文,阅读量超 2000+,带动 300 + 人参与”;实习内容补充 “协助完成 XX 任务,效率提升 X%”。 岗位匹配度:锚定目标岗位能力,比如申请运营岗,突出 “内容编辑、活动执行” 相关动作;申请市场岗,强化 “资源对接、数据统计” 细节。 信息精简:删减冗余表述(如重复的 “负责”),用短句分点,比如 “策划校园招聘会:联系 10 + 企业,组织 200 + 学生参与,到场率达 85%”。 技能落地:将 “Office、PS” 绑定经历,比如 “用 Excel 整理活动数据,输出 3 份分析表;用 PS 设计 2 张活动海报”,避免技能单独罗列。 优化后需强化 “经历 - 能力 - 岗位需求” 的关联,让实习 / 校园经历的价值更直观。 若需要进一步优化服务,私信
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
2
8
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务