面试算法十问(中英文)

1.两数之和 (Two Sum)
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回它们的数组下标。
Given an array of integers nums and a target value target, find the two integers in the array that sum up to the target value and return their array indices.

解释:可以使用哈希表来存储已经遍历过的数字及其索引,这样可以在 O(1) 时间内查找 target - nums[i] 是否存在。
Explanation: You can use a hash table to store the numbers and their indices that have been traversed, so you can find if target - nums[i] exists in O(1) time.

伪代码:

hash_table = {}
for i = 0 to len(nums) - 1complement = target - nums[i]if complement in hash_tablereturn [hash_table[complement], i]hash_table[nums[i]] = i
return []

2.有效的括号 (Valid Parentheses)
给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串,判断字符串是否有效。
Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

解释:使用栈来跟踪括号的匹配,左括号入栈,右括号时检查栈顶元素是否匹配。
Explanation: Use a stack to track the matching of parentheses. Push left parentheses onto the stack, and when encountering a right parenthesis, check if the top element of the stack matches.

伪代码:

stack = []
for each char in stringif char is left parenthesisstack.push(char)else if stack is not empty and top of stack matches charstack.pop()elsereturn false
return stack is empty

3.合并两个有序链表 (Merge Two Sorted Lists)
将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
Merge two sorted linked lists into a new sorted linked list and return it. The new list is formed by splicing together the nodes of the given two lists.

解释:可以使用一个新的链表来逐个比较两个链表的节点,选择较小的节点添加到新链表中。
Explanation: You can use a new linked list to compare the nodes of the two lists one by one, and add the smaller node to the new list.

伪代码:

dummy = new Node(0)
current = dummy
while list1 is not null and list2 is not nullif list1.val < list2.valcurrent.next = list1list1 = list1.nextelsecurrent.next = list2list2 = list2.nextcurrent = current.next
if list1 is not nullcurrent.next = list1
elsecurrent.next = list2
return dummy.next

4.盛最多水的容器 (Container With Most Water)
给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai)。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). Draw n vertical lines such that the two endpoints of line i are at (i, ai) and (i, 0). Find two lines, which together with the x-axis form a container, such that the container contains the most water.

解释:使用两个指针分别指向数组的开始和结束,计算当前的容量,并逐步向中间移动较短的线,以寻找可能的更大容量。
Explanation: Use two pointers pointing to the start and end of the array, calculate the current capacity, and gradually move the shorter line towards the middle to find a potentially larger capacity.

伪代码:

left = 0
right = len(height) - 1
max_water = 0
while left < rightwidth = right - leftmax_water = max(max_water, min(height[left], height[right]) * width)if height[left] < height[right]left++elseright--
return max_water

5.无重复字符的最长子串 (Longest Substring Without Repeating Characters)
给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
Given a string, find the length of the longest substring without repeating characters.

解释:可以使用滑动窗口的方法,用一个哈希集合来记录窗口内的字符,当遇到重复字符时,移动窗口的开始位置。
Explanation: You can use the sliding window method and a hash set to record the characters within the window. When a repeated character is encountered, move the start position of the window.

伪代码:

start = 0
max_length = 0
char_set = set()
for i = 0 to len(string) - 1while string[i] in char_setchar_set.remove(string[start])start++char_set.add(string[i])max_length = max(max_length, i - start + 1)
return max_length

6.三数之和 (3Sum)
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0?请你找出所有满足条件且不重复的三元组。
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

解释:可以先对数组排序,然后使用一个固定的指针遍历数组,对于每个元素,使用两个指针在剩余部分进行搜索,找到满足条件的三元组。
Explanation: You can first sort the array and then use a fixed pointer to traverse the array. For each element, use two pointers to search in the remaining part to find triplets that meet the condition.

伪代码:

sort(nums)
result = []
for i = 0 to len(nums) - 3if i > 0 and nums[i] == nums[i - 1]continueleft = i + 1right = len(nums) - 1while left < rightsum = nums[i] + nums[left] + nums[right]if sum == 0result.add([nums[i], nums[left], nums[right]])while left < right and nums[left] == nums[left + 1]left++while left < right and nums[right] == nums[right - 1]right--left++right--else if sum < 0left++elseright--
return result

7.最接近的三数之和 (3Sum Closest)
给定一个包括 n 个整数的数组 nums 和一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。
Given an array nums of n integers and a target value target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers.

解释:和三数之和类似,先对数组排序,然后使用一个固定的指针遍历数组,对于每个元素,使用两个指针在剩余部分进行搜索,记录最接近的和。
Explanation: Similar to 3Sum, first sort the array, then use a fixed pointer to traverse the array. For each element, use two pointers to search in the remaining part and record the closest sum.

伪代码:

sort(nums)
closest_sum = inf
for i = 0 to len(nums) - 3if i > 0 and nums[i] == nums[i - 1]continueleft = i + 1right = len(nums) - 1while left < rightsum = nums[i] + nums[left] + nums[right]if abs(sum - target) < abs(closest_sum - target)closest_sum = sumif sum < targetleft++else if sum > targetright--elsereturn sum
return closest_sum
​```

8.删除链表的倒数第N个节点 (Remove Nth Node From End of List)
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
Given a linked list, remove the nth node from the end of the list and return its head.

解释:可以使用两个指针,第一个指针先移动 n 步,然后两个指针同时移动直到第一个指针到达末尾,这时第二个指针指向的就是需要删除的节点的前一个节点。
Explanation: You can use two pointers, move the first pointer n steps first, then move both pointers at the same time until the first pointer reaches the end, at which point the second pointer points to the node before the one to be deleted.

伪代码:

dummy = new Node(0)
dummy.next = head
first = dummy
second = dummy
for i = 0 to nfirst = first.next
while first.next is not nullfirst = first.nextsecond = second.next
second.next = second.next.next
return dummy.next

9.回文数 (Palindrome Number)
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

解释:可以将整数转换为字符串,然后使用双指针法从两端向中间比较字符是否相同。
Explanation: You can convert the integer to a string and then use the two-pointer technique to compare characters from both ends towards the middle to see if they are the same.

伪代码:

str_num = str(num)
left = 0
right = len(str_num) - 1
while left < rightif str_num[left] != str_num[right]return falseleft++right--
return true

10.整数反转 (Reverse Integer)
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
Given a 32-bit signed integer, reverse digits of an integer.

解释:可以通过循环取出整数的最后一位数字,然后添加到新整数的末尾,注意处理整数溢出的情况。
Explanation: You can reverse the digits of an integer by repeatedly popping the last digit of the integer and appending it to the end of a new integer, being careful to handle integer overflow.

伪代码:

rev = 0
while num != 0pop = num % 10num /= 10if rev > INT_MAX/10 or (rev == INT_MAX / 10 and pop > 7)return 0if rev < INT_MIN/10 or (rev == INT_MIN / 10 and pop < -8)return 0rev = rev * 10 + pop
return rev

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/828288.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Godot4自学手册】第三十八节给游戏添加音效

今天&#xff0c;我的主要任务就是给游戏添加音效。在添加音效前&#xff0c;我们需要了解一个东西&#xff1a;音频总线。这个东西或许有些枯燥&#xff0c;如果你只为添加一个音效没必要了解太多&#xff0c;但如果你以后将要经常与音频播放打交道&#xff0c;还是要了解一下…

政安晨:【深度学习神经网络基础】(十三)—— 卷积神经网络

目录 概述 LeNet-5 卷积层 最大池层 稠密层 针对MNIST数据集的卷积神经网络 总之 政安晨的个人主页&#xff1a;政安晨 欢迎 &#x1f44d;点赞✍评论⭐收藏 收录专栏: 政安晨的机器学习笔记 希望政安晨的博客能够对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎…

ReentrantLock 源码解析

ReentrantLock 源码解析 文章目录 ReentrantLock 源码解析前言一、字段分析二、内部类分析1、Sync2、FairSync3、NonfairSync 三、方法分析1、构造方法2、其他方法 总结 前言 ReentrantLock 实现了 Lock 接口&#xff0c;内部基于 AQS 实现。所以想要弄懂 ReentrantLock &#…

vue 实现左侧导航栏,右侧锚点定位滚动到指定位置(超简单方法)

项目截图&#xff1a; 实现方法&#xff1a; 点击左侧菜单根据元素id定位到可视内容区域。 浏览器原生提供了一种方法scrollIntoView 。 通过scrollIntoView方法可以把元素滚动到可视区域内。 behavior: "smooth"是指定滚动方式为平滑效果。 具体代码如下&#xf…

使用 PhpMyAdmin 安装 LAMP 服务器

使用 PhpMyAdmin 安装 LAMP 服务器非常简单。按照下面所示的步骤&#xff0c;我们将拥有一个完全可运行的 LAMP 服务器&#xff08;Linux、Apache、MySQL/MariaDB 和 PHP&#xff09;。 什么是 LAMP 服务器&#xff1f; LAMP 代表 Linux、Apache、MySQL 和 PHP。它们共同提供…

智能化安全防护:AI防火墙的原理与应用

随着人工智能技术的迅猛发展&#xff0c;其在各个领域的应用也日益广泛。作为引领数字化转型的重要力量&#xff0c;AI技术为我们的生活和工作带来了前所未有的便利与效率。在通信领域&#xff0c;人工智能的应用同样展现出了巨大的潜力和价值&#xff0c;特别是在网络安全防护…

HTTP/1.1,HTTP/2.0和HTTP/3.0 各版本协议的详解(2024-04-24)

1、HTTP介绍 HTTP 协议有多个版本&#xff0c;目前广泛使用的是 HTTP/1.1 和 HTTP/2&#xff0c;以及正在逐步推广的 HTTP/3。 HTTP/1.1&#xff1a;支持持久连接&#xff0c;允许多个请求/响应通过同一个 TCP 连接传输&#xff0c;减少了建立和关闭连接的消耗。 HTTP/2&#…

基于PaddlePaddle平台训练物体分类——猫狗分类

学习目标&#xff1a; 在百度的PaddlePaddle平台训练自己需要的模型&#xff0c;以训练一个猫狗分类模型为例 PaddlePaddle平台&#xff1a; 飞桨&#xff08;PaddlePaddle&#xff09;是百度开发的深度学习平台&#xff0c;具有动静统一框架、端到端开发套件等特性&#xf…

node.js 解析post请求 方法一

前提&#xff1a;依旧以前面发的node.js服务器动态资源处理代码 具体见 http://t.csdnimg.cn/TSNW9为模板&#xff0c;在这基础上进行修改。与动态资源处理代码不同的是&#xff0c;这次的用户信息我们借用表单来实现。post请求解析来获取和展示用户表单填写信息 1》代码难点&…

【项目实战】基于高并发服务器的搜索引擎

【项目实战】基于高并发服务器的搜索引擎 目录 【项目实战】基于高并发服务器的搜索引擎搜索引擎部分代码index.htmlindex.hpplog.hppparser.cc&#xff08;用于对网页的html文件切分且存储索引关系&#xff09;searcher.hpputil.hpphttp_server.cc&#xff08;用于启动服务器和…

WPForms Pro插件下载:简化您的在线表单构建,提升用户互动

在当今的数字化世界中&#xff0c;表单是网站与用户互动的关键。无论是收集信息、处理订单还是进行调查&#xff0c;一个好的表单可以极大地提升用户体验和转化率。WPForms Pro插件&#xff0c;作为一款专业的WordPress表单构建工具&#xff0c;旨在帮助您轻松创建美观、功能强…

深度学习基础:循环神经网络中的Dropout

深度学习基础&#xff1a;循环神经网络中的Dropout 在深度学习中&#xff0c;过拟合是一个常见的问题&#xff0c;特别是在循环神经网络&#xff08;RNN&#xff09;等复杂模型中。为了应对过拟合问题&#xff0c;研究者们提出了许多方法&#xff0c;其中一种被广泛应用的方法…

TensorFlow进阶一(张量的范数、最值、均值、和函数、张量的比较)

⚠申明&#xff1a; 未经许可&#xff0c;禁止以任何形式转载&#xff0c;若要引用&#xff0c;请标注链接地址。 全文共计3077字&#xff0c;阅读大概需要3分钟 &#x1f308;更多学习内容&#xff0c; 欢迎&#x1f44f;关注&#x1f440;【文末】我的个人微信公众号&#xf…

短视频评论ID批量爬虫提取获客软件|视频评论下载采集工具

短视频评论批量抓取软件&#xff1a;智能拓客&#xff0c;精准抓取用户反馈 主要功能一览 1. 智能抓取任务创建&#xff1a; 软件提供了任务创建功能&#xff0c;用户只需输入任务名称、搜索关键词以及评论监控词&#xff0c;即可开始智能抓取。不仅能够搜索关键词匹配的视频…

Gradio 最快创建Web 界面部署到服务器并演示机器学习模型,本文提供教学案例以及部署方法,避免使用繁琐的django

最近学习hugging face里面的物体检测模型&#xff0c;发现一个方便快捷的工具&#xff01; Gradio 是通过友好的 Web 界面演示机器学习模型的最快方式&#xff0c;以便任何人都可以在任何地方使用它&#xff01; 一、核心优势&#xff1a; 使用这个开发这种演示机器学习模型的…

就业班 第三阶段(负载均衡) 2401--4.19 day3

二、企业 keepalived 高可用项目实战 1、Keepalived VRRP 介绍 keepalived是什么keepalived是集群管理中保证集群高可用的一个服务软件&#xff0c;用来防止单点故障。 ​ keepalived工作原理keepalived是以VRRP协议为实现基础的&#xff0c;VRRP全称Virtual Router Redundan…

前端开发攻略---封装calendar日历组件,实现日期多选。可根据您的需求任意调整,可玩性强。

1、演示 2、简介 1、该日历组件是纯手搓出来的&#xff0c;没依赖任何组件库&#xff0c;因此您可以随意又轻松的改变代码&#xff0c;以实现您的需求。 2、代码清爽干净&#xff0c;逻辑精妙&#xff0c;您可以好好品尝。 3、好戏开场。 3、代码&#xff08;Vue3写法&#xff…

探索Web3:去中心化的互联网新时代

引言 在过去的几十年里&#xff0c;互联网已经改变了我们的生活方式、商业模式以及社交互动方式。然而&#xff0c;一个新的技术浪潮——Web3正在崭露头角&#xff0c;预示着一个去中心化的互联网新时代的来临。本文将深入探讨Web3技术的定义、特点以及其对未来互联网发展的影…

【数据结构-图】

目录 1 图2 图的定义和基本概念&#xff08;在简单图范围内&#xff09;3 图的类型定义4 图的存储结构4.1 邻接矩阵 表示法4.2 邻接表 表示法4.3 十字链表 表示法4.4 邻接多重表 表示法 5 图的遍历5.1 深度优先搜索-DFS 及 广度优先遍历-BFS 6 图的应用6.1 最小生成树6.1.1 克鲁…

vue cli3开发自己的插件发布到npm

具体流程如下&#xff1a; 1、创建一个vue项目 vue create project 2、编写组件 &#xff08;1&#xff09;新建一个plugins文件夹&#xff08;可自行创建&#xff09; &#xff08;2&#xff09;新建Button组件 &#xff08;3&#xff09;组件挂载&#xff0c;为组件提供 in…