leetcode/tests/two_sum_test.cpp
2023-05-18 00:12:05 +08:00

62 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <gtest/gtest.h>
#include "map_vec.h"
/**
* 2 <= nums.length <= 104
* -109 <= nums[i] <= 109
* -109 <= target <= 109
* 只会存在一个有效答案
*
* 来源力扣LeetCode
* 链接https://leetcode.cn/problems/two-sum
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
TEST(TwoSumTest, BasicAssertions) {
std::vector<int> output, input;
int target;
/**
* 示例 1
* 输入nums = [2,7,11,15], target = 9
* 输出:[0,1]
* 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
*/
input.resize(4);
input = {2, 7, 11, 15};
target = 9;
output = yaha::two_sum(input, target);
ASSERT_EQ(output.size(), 2);
ASSERT_EQ(output[0], 0);
ASSERT_EQ(output[1], 1);
/**
* 示例 2
* 输入nums = [3,2,4], target = 6
* 输出:[1,2]
*/
input.resize(3);
input = {3, 2, 4};
target = 6;
output = yaha::two_sum(input, target);
ASSERT_EQ(output.size(), 2);
ASSERT_EQ(output[0], 1);
ASSERT_EQ(output[1], 2);
/**
* 示例 3
* 输入nums = [3,3], target = 6
* 输出:[0,1]
*/
input.resize(2);
input = {3, 3};
target = 6;
output = yaha::two_sum(input, target);
ASSERT_EQ(output.size(), 2);
ASSERT_EQ(output[0], 0);
ASSERT_EQ(output[1], 1);
}