【# 1. 两数之和
## 题目描述
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
“`text
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
“`
## 解题思路
### 方法一:暴力枚举
最容易想到的方法是枚举数组中的每一个数 x,寻找数组中是否存在 target – x。
当我们使用遍历整个数组的方式寻找 target – x 时,需要注意到每一个位于 x 之前的元素都已经和 x 匹配过,因此不需要再进行匹配。而每一个元素不能被使用两次,所以我们只需要在 x 后面的元素中寻找 target – x。
“`python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return []
“`
复杂度分析
– 时间复杂度:O(N^2),其中 N 是数组中的元素数量。最坏情况下数组中任意两个数都要被匹配一次。
– 空间复杂度:O(1)。
### 方法二:哈希表
注意到方法一的时间复杂度较高的原因是寻找 target – x 的时间复杂度过高。因此,我们需要一种更优秀的方法,能够快速寻找数组中是否存在目标元素。如果存在,我们需要找出它的索引。
使用哈希表,可以将寻找 target – x 的时间复杂度降低到 O(1)。
我们创建一个哈希表,对于每一个 x,我们首先查询哈希表中是否存在 target – x,然后将 x 插入到哈希表中,即可保证不会让 x 和自己匹配。
“`python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashtable = dict()
for i, num in enumerate(nums):
if target – num in hashtable:
return [hashtable[target – num], i]
hashtable[num] = i
return []
“`
复杂度分析
– 时间复杂度:O(N),其中 N 是数组中的元素数量。对于每一个元素 x,我们可以 O(1) 地寻找 target – x。
– 空间复杂度:O(N),其中 N 是数组中的元素数量。主要为哈希表的开销。
## 总结
通过使用哈希表,我们可以将时间复杂度从 O(N^2) 降低到 O(N),大大提高了算法的效率。在实际应用中,哈希表是一种非常常用的数据结构,能够有效地解决许多问题。
Giới thiệu Our Favorite 4K Streaming Device Is 38% Off for Black Friday
: Our Favorite 4K Streaming Device Is 38% Off for Black Friday
Hãy viết lại bài viết dài kèm hashtag về việc đánh giá sản phẩm và mua ngay tại Queen Mobile bằng tiếng VIệt: Our Favorite 4K Streaming Device Is 38% Off for Black Friday
Mua ngay sản phẩm tại Việt Nam:
QUEEN MOBILE chuyên cung cấp điện thoại Iphone, máy tính bảng Ipad, đồng hồ Smartwatch và các phụ kiện APPLE và các giải pháp điện tử và nhà thông minh. Queen Mobile rất hân hạnh được phục vụ quý khách….
_____________________________________________________
Mua #Điện_thoại #iphone #ipad #macbook #samsung #xiaomi #poco #oppo #snapdragon giá tốt, hãy ghé [𝑸𝑼𝑬𝑬𝑵 𝑴𝑶𝑩𝑰𝑳𝑬]
✿ 149 Hòa Bình, phường Hiệp Tân, quận Tân Phú, TP HCM
✿ 402B, Hai Bà Trưng, P Tân Định, Q 1, HCM
✿ 287 đường 3/2 P 10, Q 10, HCM
Hotline (miễn phí) 19003190
Thu cũ đổi mới
Rẻ hơn hoàn tiền
Góp 0%
Thời gian làm việc: 9h – 21h.
KẾT LUẬN
Hãy viết đoạn tóm tắt về nội dung bằng tiếng việt kích thích người mua: Our Favorite 4K Streaming Device Is 38% Off for Black Friday
Roku makes the best streaming devices on the market right now, claiming nearly half the spots on our list of the best models for 2023. That includes the top spot overall, which belongs to the Roku Express 4K Plus. Right now you can pick it up for just $25 at Amazon, which is $15 off and just $1 more than the all-time lowest price we’ve seen. Though there’s no set expiration for this early Black Friday deal, we’d recommend getting your order in sooner rather than later if you don’t want to miss out on these savings.
We named the Roku Express 4K Plus the best media streamer of 2023 thanks to its affordable price tag and user-friendly interface that makes it easy to find all your favorite shows and movies. Plus, it has an unbiased platform that doesn’t promote one streaming service over another. It supports 4K content with HDR for vibrant colors and sharp contrast, and it has Apple AirPlay built-in so you can wirelessly stream content from your iPhone, iPad or other Apple devices. It also comes with Roku’s convenient voice remote, which allows you to control your TV’s power and volume, and search for shows and movies completely hands-free.
And if you’re looking for more bargains, you can check out our full roundup of all the best Black Friday Roku deals for even more savings on top-rated streaming devices and smart TVs.
More Black Friday deals from CNET
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=();t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)(0);
s.parentNode.insertBefore(t,s)}(window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘set’, ‘autoConfig’, false, ‘789754228632403’);
fbq(‘init’, ‘789754228632403’);
Xem chi tiết và đăng kýXem chi tiết và đăng ký
Khám phá thêm từ Phụ Kiện Đỉnh
Đăng ký để nhận các bài đăng mới nhất được gửi đến email của bạn.