【LeetCode】Two Sum & Two Sum II - Input array is sorted & Two Sum IV - Input is a BST
1. Two Sum
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
1 int* twoSum(int* nums, int numsSize, int target) {
2 int *sValue = (int *)malloc(2 * sizeof(int));
3 for(int i = 1; i < numsSize; i++)
4 {
5 for(int j = 0; j < i; j++)
6 {
7 if(nums[i] + nums[j] == target)
8 {
9 sValue[0] = i;
10 sValue[1] = j;
11 return sValue;
12 }
13 }
14 }
15 return NULL;
16 }
解题思路:
嵌套两层循环:167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
Note:
- Your returned answers (both index1 and index2) are not zero-based.
- You may assume that each input would have exactly one solution and you may not use the same element twice.
Example:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
方法一:
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
*returnSize = 2;
int *Array = NULL;
for(int i = 1; i < numbersSize; i++)
{
for(int j = 0; j < i; j++)
{
if(numbers[i] + numbers[j] == target)
{
Array = (int *)malloc(*returnSize * sizeof(int));
Array[0] = j + 1;
Array[1] = i + 1;
return Array;
}
}
}
return NULL;
}
方法二:
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
*returnSize = 2;
int *Array = NULL;
int iLeft = 0, iRight = numbersSize - 1;
int sum = 0;
while(iLeft < iRight){
sum = numbers[iLeft] + numbers[iRight];
if(sum == target)
{
Array = (int *)malloc(*returnSize * sizeof(int));
Array[0] = iLeft + 1;
Array[1] = iRight + 1;
return Array;
}
else if(sum < target){
iLeft++;
}
else{
iRight--;
}
}
return NULL;
}
C++:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result;
int iLeft = 0;
int iRight = numbers.size() - 1;
auto sum = 0;
while(iLeft < iRight){
sum = numbers[iLeft] + numbers[iRight];
if(sum == target){
// result.push_back(iLeft + 1);
// result.push_back(iRight + 1);
// break;
return {iLeft + 1, iRight + 1};
}
else if(sum < target){
iLeft++;
}
else{
iRight--;
}
}
// return result;
return {};
}
};
因为所给的序列numbers是升序排列,first:numbers的起始标识,last:numbers的最后一位标识
所以当numbers[first] + numbers[last] 的和小于target,first+1取下一位更大的数与numbers相加;当numbers[first] + numbers[last] 的和大于target,将last-1取前一位更小的数与numbers[first]相加
653. Two Sum IV - Input is a BST
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.
Example 1:
Input:
5
/ \
3 6
/ \ \
2 4 7
Target = 9
Output: True
Example 2:
Input:
5
/ \
3 6
/ \ \
2 4 7
Target = 28
Output: False
1 /*
2 struct TreeNode
3 {
4 int val;
5 struct TreeNode *left;
6 struct TreeNode *right;
7 };
8
9 */
10
11 bool findValue(struct TreeNode* root, struct TreeNode* temp, int k)
12 {
13 if(!temp)
14 {
15 return false;
16 }
17 if(temp->val == k && temp != root)
18 {
19 return true;
20 }
21 if(k > temp->val)
22 {
23 return findValue(root, temp->right, k);
24 }
25 else
26 {
27 return findValue(root, temp->left, k);
28 }
29 }
30
31 bool findX(struct TreeNode* root, struct TreeNode* temp, int k)
32 {
33 if(!root)
34 {
35 return false;
36 }
37 if(findValue(root, temp, k - root->val))
38 {
39 return true;
40 }
41 else
42 {
43 return (findX(root->left, temp, k) || findX(root->right, temp, k));
44 }
45 }
46
47 bool findTarget(struct TreeNode* root, int k) {
48 struct TreeNode* pTemp = root;
49 return findX(root, pTemp, k);
50 }
代码37行的k - root->val表示 目标数 减去 二叉树中某一个数剩下的那个数据,如果递归查找树能找到与k - root->val相等的数并且不是同一个节点的数据(第17行有做判断)说明存在两个相加等于目标的数。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通