英文:
add twoNumber in the array and must equal target
问题
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
int[] result = {i, j};
return result;
}
}
}
return null;
}
英文:
I have this program where the number of elements of the array "n" is entered and then the target is entered "d",
Then I want to add two numbers from the matrix and the addition is equal to target "d",
Then I want to return an array of Indexes of these two numbers
Example:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
I wrote this program, but I could not return the Indexes,
How can I solve the problem?
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
System.out.println("This the number that we want it" + "\n" + i + "\n" + j);
}
}
}
return nums;
}
答案1
得分: 2
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
result[0] = i;
result[1] = j;
System.out.println("这是我们要找的数" + "\n" + i + "\n" + j);
}
}
}
return result;
}
如果您对O(n^2)的复杂度满意,您可以不必继续阅读。但这里是一个O(n)时间复杂度的解决方案:
public int[] twoSum(int[] numbers, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < numbers.length; map.put(numbers[i], ++i)) {
if (map.containsKey(target - numbers[i])) {
return new int[] {map.get(target - numbers[i]), i + 1};
}
}
return new int[]{0,0};
}
英文:
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
res[0] = i;
res[1] = j;
System.out.println("This the number that we want it" + "\n" + i + "\n" + j);
}
}
}
return res;
}
You need to create a new array of size 2 and return that.
If you are happy with O(n^2) complexity, you don't need to read ahead. But here is a solution with O(n) time complexity:
public int[] twoSum(int[] numbers, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < numbers.length; map.put(numbers[i], ++i)) {
if (map.containsKey(target - numbers[i])) {
return new int[] {map.get(target - numbers[i]), i + 1};
}
}
return new int[]{0,0};
}
答案2
得分: 1
看看这些解决方案!
时间复杂度为 O(n^2)
,空间复杂度为 O(1)
public static int[] foo(int[] arr, int d) {
for (int i = 0; i < arr.length - 1; i++)
for (int j = i + 1; j < arr.length; j++)
if (arr[i] + arr[j] == d)
return new int[] { i, j };
return new int[] {-1, -1};
}
时间复杂度为 O(n)
,空间复杂度为 O(n)
public static int[] foo(int[] arr, int d) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
if (map.containsKey(d - arr[i]))
return new int[] { map.get(d - arr[i]), i };
map.put(arr[i], i);
}
return new int[] {-1, -1};
}
英文:
Look at this solutions!
Time complexity O(n^2)
, space complexity O(1)
public static int[] foo(int[] arr, int d) {
for (int i = 0; i < arr.length - 1; i++)
for (int j = i + 1; j < arr.length; j++)
if (arr[i] + arr[j] == d)
return new int[] { i, j };
return new int[] {-1, -1};
}
Time complexity O(n)
, space complexity O(n)
public static int[] foo(int[] arr, int d) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
if (map.containsKey(d - arr[i]))
return new int[] { map.get(d - arr[i]), i };
map.put(arr[i], i);
}
return new int[] {-1, -1};
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论