2. Two Sum II – Input array is sorted

本文介绍了解决已排序数组中寻找两个数使它们的和等于特定目标值的问题的两种方法。第一种方法通过双指针技巧从前向后遍历数组来找到匹配的元素;第二种方法利用二分查找提高搜索效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question:
Similar to Question [1. Two Sum], except that the input array is already sorted in
ascending order.

solution1
使用两个下标,一个i=0,一个j=nums.size();从后往前计算

//做出来还是挺开心的,问题代码可太复杂了,答案是简化了一波,感觉很优秀了
vector<int> twoSum(vector<int>& nums, int target)
{
	unordered_map<int, int> map;
	vector<int> ret;
	int j = nums.size()-1;
	int len = nums.size()%2;
	if (len == 1)
		len = nums.size() / 2 + 1;
	else
		len = nums.size() / 2;
	for (int i = 0; i < len; i++)
	{
		while (nums[i] + nums[j] > target)
		{
			j--;
		}
		if (nums[i] + nums[j] == target)
		{
			//cout << i << " " << j << endl;
			ret.push_back(i);
			ret.push_back(j);
		}
	}
	return ret;
}
//answer
vector<int> twoSum(vector<int>& nums, int target)
{
	vector<int> ret;
	int j = nums.size() - 1;
	int i = 0;
	while (i < j)
	{
		if (nums[i] + nums[j] == target)
		{
			//cout << i << " " << j << endl;
			ret.push_back(i);
			ret.push_back(j);
			i++; 
		}
		else if (nums[i] + nums[j] > target)
			j--;
		else
			i++;
	}
	return ret;
}

solution 2
对于有序的数组,查找效率想要高一点,考虑二分查找。

// test1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include "pch.h"
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
using namespace std;

int search(vector<int> nums, int i, int target);

vector<int> twoSum(vector<int>& nums, int target)
{
	vector<int> ret;
	for (int i = 0; i < nums.size() / 2; i++)
	{
		int tar = target - nums[i];
		if (search(nums, i,tar) != -1&&search(nums,i,tar)>i)
		{
			cout << i << " " << tar << endl;
			ret.push_back(i);
			ret.push_back(search(nums, i,tar));
		}
	}
	return ret;
}

int search(vector<int> nums, int i,int target)
{
	int len = nums.size()-1;
	while (i<len)
	{
		int mid = (len + i) / 2;
		if (nums[mid] > target)
			len = mid;
		else
			i = mid;
	}	
	return (len = i && nums[len] == target) ? len : -1;
}

int main()
{
	vector<int> nums = {2,3,3,4,5};
	twoSum(nums, 7);
	return 0;
}

又是开心的一天呢。

   There is an empty matrix M of size n×m . Zhongkao examination is over, and Daniel would like to do some puzzle games. He is going to fill in the matrix M using permutations of length m . That is, each row of M must be a permutation of length m† . Define the value of the i -th column in M as vi=MEX(M1,i,M2,i,…,Mn,i)‡ . Since Daniel likes diversity, the beauty of M is s=MEX(v1,v2,⋯,vm) . You have to help Daniel fill in the matrix M and maximize its beauty. † A permutation of length m is an array consisting of m distinct integers from 0 to m−1 in arbitrary order. For example, [1,2,0,4,3] is a permutation, but [0,1,1] is not a permutation (1 appears twice in the array), and [0,1,3] is also not a permutation (m−1=2 but there is 3 in the array). ‡ The MEX of an array is the smallest non-negative integer that does not belong to the array. For example, MEX(2,2,1)=0 because 0 does not belong to the array, and MEX(0,3,1,2)=4 because 0 , 1 , 2 and 3 appear in the array, but 4 does not. DeepL 翻译    有一个大小为 n×m 的空矩阵 M 。 中考结束了,丹尼尔想做一些益智游戏。他打算用长度为 m 的排列填充矩阵 M 。也就是说, M 的每一行都必须是长度为 m† 的排列。 将 M 中 i /th列的值定义为 vi=MEX(M1,i,M2,i,…,Mn,i)‡ 。因为丹尼尔喜欢多样性,所以 M 的美是 s=MEX(v1,v2,⋯,vm) 。 你必须帮助丹尼尔填入矩阵 M ,并最大化它的美感。 † 长度为 m 的排列是由 m 个不同的整数组成的数组,这些整数从 0 到 m−1 按任意顺序排列。例如, [1,2,0,4,3] 是一个排列,但 [0,1,1] 不是一个排列( 1 在数组中出现了两次), [0,1,3] 也不是一个排列( m−1=2 ,但数组中有 3 )。 ‡ 数组的 MEX 是不属于数组的最小非负整数。例如, MEX(2,2,1)=0 是因为 0 不属于数组,而 MEX(0,3,1,2)=4 是因为 0 、 1 、 2 和 3 出现在数组中,但 4 不属于数组。    Input The first line of input contains a single integer t (1≤t≤1000 ) — the number of test cases. The description of test cases follows. The only line of each test case contains two integers n and m (1≤n,m≤2⋅105 ) — the size of the matrix. It is guaranteed that the sum of n⋅m over all test cases does not exceed 2⋅105 . DeepL 翻译    输入 输入的第一行包含一个整数 t ( 1≤t≤1000 ) - 测试用例的数量。测试用例说明如下。 每个测试用例的唯一一行包含两个整数 n 和 m ( 1≤n,m≤2⋅105 ) - 矩阵的大小。 保证所有测试用例
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值