算法分析与设计作业题
Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:

public static int[] twoSum(int[] nums, int target) {
int[] answer = new int[2];
A:for (int i = 0; i < nums.length; ++i){
answer[0] = i;
int b = target - nums[i];
for (int j = i + 1; j < nums.length; ++j){
if (nums[j] == b){
answer[1] = j;
break A;
}
}
}
return answer;
}
本文深入探讨了TwoSum问题的算法实现,这是一种常见的编程面试题目,要求在整数数组中找到两个数,使它们的和等于特定的目标值。文章提供了一个Java实现的例子,使用双重循环来寻找符合条件的两个数,并返回它们的下标。
1708

被折叠的 条评论
为什么被折叠?



