面试算法十问(中英文)

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) - 1
    complement = target - nums[i]
    if complement in hash_table
        return [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 string
    if char is left parenthesis
        stack.push(char)
    else if stack is not empty and top of stack matches char
        stack.pop()
    else
        return 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 null
    if list1.val < list2.val
        current.next = list1
        list1 = list1.next
    else
        current.next = list2
        list2 = list2.next
    current = current.next
if list1 is not null
    current.next = list1
else
    current.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 < right
    width = right - left
    max_water = max(max_water, min(height[left], height[right]) * width)
    if height[left] < height[right]
        left++
    else
        right--
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) - 1
    while string[i] in char_set
        char_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) - 3
    if i > 0 and nums[i] == nums[i - 1]
        continue
    left = i + 1
    right = len(nums) - 1
    while left < right
        sum = nums[i] + nums[left] + nums[right]
        if sum == 0
            result.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 < 0
            left++
        else
            right--
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) - 3
    if i > 0 and nums[i] == nums[i - 1]
        continue
    left = i + 1
    right = len(nums) - 1
    while left < right
        sum = nums[i] + nums[left] + nums[right]
        if abs(sum - target) < abs(closest_sum - target)
            closest_sum = sum
        if sum < target
            left++
        else if sum > target
            right--
        else
            return 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 n
    first = first.next
while first.next is not null
    first = first.next
    second = 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 < right
    if str_num[left] != str_num[right]
        return false
    left++
    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 != 0
    pop = num % 10
    num /= 10
    if rev > INT_MAX/10 or (rev == INT_MAX / 10 and pop > 7)
        return 0
    if rev < INT_MIN/10 or (rev == INT_MIN / 10 and pop < -8)
        return 0
    rev = rev * 10 + pop
return rev

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

OpenHarmony语言基础类库【@ohos.url (URL字符串解析)】

说明&#xff1a; 本模块首批接口从API version 7开始支持。后续版本的新增接口&#xff0c;采用上角标单独标记接口的起始版本。 导入模块 import Url from ohos.url URLParams9 URLParams接口定义了一些处理URL查询字符串的实用方法。 constructor9 constructor(init?…

基于Spring Boot的家具销售电商平台设计与实现

基于Spring Boot的家具销售电商平台设计与实现 开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/idea 系统部分展示 系统功能界面图&#xff0c;在系统首页可以查看首页…

代码随想录第44天|动态规划:完全背包理论基础 518.零钱兑换II 377. 组合总和 Ⅳ

动态规划&#xff1a;完全背包理论基础 代码随想录 (programmercarl.com) 动态规划之完全背包&#xff0c;装满背包有多少种方法&#xff1f;组合与排列有讲究&#xff01;| LeetCode&#xff1a;518.零钱兑换II_哔哩哔哩_bilibili 完全背包和01背包问题唯一不同的地方就是&…

xilinx Mailbox 中的ipi message地址计算方式

适用于openAmp mailbox ipi id对应的ipi message地址计算方式 官方openamp硬件配置解析 OpenAMP Base Hardware Configurations - Xilinx Wiki - Confluence openamp官方设备树 meta-openamp/meta-xilinx-tools/recipes-bsp/device-tree/files/zynqmp-openamp.dtsi at rel-v2…

C++:构造函数与析构函数

目录 构造函数 构造函数的概念 析构函数的作用 自定义构造函数与默认构造函数 自定义构造函数 默认构造函数 调用自定义构造函数 析构函 自定义析构函数和默认构造函数 自定义构造函数 默认析构函数 构造函数 构造函数的概念 我们通常的函数是都需要有返回值的,但…

共享单车数据分析与需求预测项目

注意&#xff1a;本文引用自专业人工智能社区Venus AI 更多AI知识请参考原站 &#xff08;[www.aideeplearning.cn]&#xff09; 项目背景 自动自行车共享系统是传统自行车租赁的新一代&#xff0c;整个会员、租赁和归还过程都变得自动化。通过这些系统&#xff0c;用户可以…

Jupyter的下载与安装

1.下载&#xff1a; 在anaconda的指定环境中 conda install nb_conda_kernels 2.打开 在anaconda指定环境中使用命令&#xff1a; jupyter notebook 3.输入指令后&#xff0c;会显示如下&#xff0c;根据显示地址打开 3. 在右边的new按钮处&#xff0c;选择相应环境&…

DTU如何用VPN

在工业物联网的应用中&#xff0c;数据传输单元&#xff08;DTU&#xff09;作为关键的通信设备&#xff0c;承担着现场设备与远程服务器之间的数据传输任务。然而&#xff0c;在某些情况下&#xff0c;由于网络环境的限制或安全需求&#xff0c;我们需要通过虚拟私人网络&…

SpringCloud系列(13)--Eureka服务名称修改和服务IP显示

前言&#xff1a;在上一章节中我们把服务提供者做成了集群&#xff0c;而本章节则是一些关于服务信息的配置&#xff0c;这部分知识对集群整体影响不大&#xff0c;不过最好还是掌握&#xff0c;毕竟万一有用到的地方呢 1、修改服务的名称 有时候我们想要修改服务的名称&#…

【深度学习】DDoS-Detection-Challenge aitrans2024 入侵检测,基于机器学习(深度学习)判断网络入侵

当了次教练&#xff0c;做了个比赛的Stage1&#xff0c;https://github.com/AItransCompetition/DDoS-Detection-Challenge&#xff0c;得了100分。 一些记录&#xff1a; 1、提交的flowid不能重复&#xff0c;提交的是非入侵的数量和数据flowid,看check.cpp可知。 2、Stage…

redis底层数据结构之ziplist

目录 一、概述二、ziplist结构三、Entry结构四、为什么ZipList特别省内存五、ziplist的缺点 redis底层数据结构已完结&#x1f44f;&#x1f44f;&#x1f44f;&#xff1a; ☑️redis底层数据结构之SDS☑️redis底层数据结构之ziplist☑️redis底层数据结构之quicklist☑️red…

Docker的资源控制管理——Cgroups

目录 引言&#xff1a; 一、CPU资源控制 1、简介 2、cgroup的四大功能&#xff1a; ①资源限制&#xff1a; ②优先级分配&#xff1a; ③资源统计&#xff1a; ④任务控制&#xff1a; 3、设置cpu使用率上限 4、查看CPU默认配置&#xff1a; 5、CPU压力测试 6、设…

H264编码标准中游程编码应用介绍

H264编码标准 H.264编码标准&#xff0c;也被称作MPEG-4 AVC&#xff08;Advanced Video Coding&#xff09;&#xff0c;是一种被广泛使用的数字视频压缩标准。它由国际电信联盟&#xff08;ITU-T&#xff09;和国际标准化组织&#xff08;ISO&#xff09;共同开发&#xff0…

C++核心编程——4.5 运算符重载

4.5.0 运算符重载概念 对已有的运算符重新进行定义&#xff0c;赋予其另一种功能&#xff0c;以适应不同的数据类型 4.5.1 加号运算符重载 作用&#xff1a;实现两个自定义数据类型相加的运算 class Person { public:Person() {};Person(int a, int b){this->m_A a;this…

Bayes判别:统计学中的经典分类方法

在统计和机器学习领域&#xff0c;Bayes判别是一个基于概率理论的强大工具&#xff0c;用于解决分类问题。它基于Bayes定理&#xff0c;通过计算和比较后验概率来进行决策。这种方法在处理不确定性和不完整数据时表现尤为出色&#xff0c;因此在医学诊断、邮件过滤、语音识别等…

《十》Qt各种对话框之QFontDialog

QFontDialog 在介绍 QFontDialog 对话框之前&#xff0c;我们先简单介绍一下 QFont 字体类。QFont 主要用于控制文本显示的字体&#xff0c;字体主要有四大属性&#xff1a;①字体家族 family 决定字体外观家族&#xff0c;比如宋体、楷体等&#xff1b; ②字号 pointSize &am…

css文字和span在一行对不齐

1.需求背景 父盒子中有两个span&#xff0c;但是span中的文字对不齐。如下图&#xff0c;明显右边的文字偏高 处理后的效果&#xff08;已经对齐&#xff0c;图中标记的是基本的div结构&#xff09;&#xff1a; 2.该问题出现的原因&#xff1a; span1设置的高度比span2内…

thsi指针用法总结

1 c类对象中的变量和函数是分开存储的 2 所以对象共用一份成员函数&#xff0c;类的大小是指非静态的成员变量&#xff1b; this 完成链式操作 const 修饰成员函数

【Java 解析全国详细地址】Java 利用正则表达式完美解析全国省市区地址

这里写自定义目录标题 Java使用正则解析省市区/县 具体地址问题场景上demo运行结果 Java使用正则解析省市区/县 具体地址 问题场景 OCR识别营业执照 获取详细地址并拆分 上demo import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import j…

使用API有效率地管理Dynadot域名,自查账户信息

关于Dynadot Dynadot是通过ICANN认证的域名注册商&#xff0c;自2002年成立以来&#xff0c;服务于全球108个国家和地区的客户&#xff0c;为数以万计的客户提供简洁&#xff0c;优惠&#xff0c;安全的域名注册以及管理服务。 Dynadot平台操作教程索引&#xff08;包括域名邮…
最新文章