剑指 Offer 57. 和为s的两个数字bahttps://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof/
| 2023-8-7
0  |  阅读时长 0 分钟
Date
Aug 3, 2023
need_review
need_review
type
剑指 Offer(第 2 版)
undo
undo
难度
简单
输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。
示例 1:
示例 2:
限制:
  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^6

解法1
  1. 遍历选取一个小于target的数
  1. 二分查找下一个数
时间复杂度
notion image
解法2
使用双指针向中心逼近
时间复杂度
notion image
  • Giscus
目录