leetcode刷题两数之和

给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:
给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
两种思路:第一种用暴力法进行解决 嵌套循环 每次遍历 在剩余的部分进行查找 看看剩余的部分是否有元素和该元素的和为目标数 有则进行返回 没有则返回空
package com.lzh.simple;

import java.util.Scanner;

public class TwoSumDemo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next().toString();
String[] arr = str.split(",");
int[] nums = new int[arr.length];
for(int i=0; i<nums.length; i++){
nums[i] = Integer.parseInt(arr[i]);
}
int target = scanner.nextInt();
int[] a = twoSum(nums, target);
for(int val:a){
System.out.print(val+",");
}

}
public static int[] twoSum(int[] nums, int target){
    for(int i=0; i<nums.length; i++){
        for(int j=i+1; j<nums.length; j++){
            if(nums[j]==target-nums[i]){
                return new int[]{i,j};
            }
        }
    }
    return null;
}

}
双重循环 时间复杂度o(n*n) 空间复杂度o(1)

第二种解决方案:采用map结构进行存储 第一次遍历 将所有的数存入map中 第二次遍历的时候 遍历每个数的时候 在map中查找是否存在数和该数之和为目标数 有则进行返回 无则返回null
package com.lzh.simple;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

//给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
//你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
public class TwoSumDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next().toString();
String[] arr = str.split(",");
int[] nums = new int[arr.length];
for(int i=0; i<nums.length; i++){
nums[i] = Integer.parseInt(arr[i]);
}
int target = scanner.nextInt();
int[] pos = getPos(nums, target);
for(int val:pos){
System.out.println(val);
}
}
public static int[] getPos(int[] nums, int target){
int len = nums.length;
Map<Integer, Integer> map = new HashMap<>();
for(int i=0; i<len; i++){
map.put(nums[i], i);
}
for(int i=0; i<len; i++){
int temp = nums[i];
int tmp = target-temp;
if(map.containsKey(tmp)){
return new int[]{i,map.get(tmp)};
}
}
return null;
}
}
这里解释一下map存储重复的元素时会产生元素的覆盖 为什么还能返回正确的结果???

原文地址:https://www.cnblogs.com/phantom576/p/11616833.html

时间: 2024-08-29 07:47:06

leetcode刷题两数之和的相关文章

leetcode刷题--两数之和(简单)

一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然你的基础也要扎实,毕竟在技术面的时候很容易露馅的. 所以奉劝各位还未毕业,在大三或大二的师弟师妹早点刷题,心里也有底气进入求职大军,毕竟大四开始刷题的话时间上有些太紧了,推荐刷题的话就是牛客和leetcode. 回归正题,这次记录的是leetcode刷的第一题--两数之和. 二.审题 审题真的很重要

LeetCode算法题——两数之和(python)

两数之和: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用.例如:给定 nums = [2, 7, 11, 15], target = 9.由nums[0] + nums[1] = 2 + 7 = 9,所以返回 [0, 1] 原文地址:http://blog.51cto.com/13921683/2318829

LeetCode 刷题1---两数之和

/** 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] */ public class demo1 { //普通解法 public static int[] twoSum

LeetCode 第一题 两数之和

题目描述 给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 方法一:暴力法 暴力法很简单,遍历每个元素 xx,并查找是否存在一个值与 target - xtarget?x

LeetCode刷题:第一题 两数之和

从今天开始刷LeetCode 第一题:两数之和 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 代码如下: 1 /** 2 * Note: The retur

LeetCode | No.1 两数之和

题目描述: 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. 给定一个整数数组nums和一个目标值target,请你在该数

leetcode——Two Sum 两数之和(AC)

Given an array of integers, 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. Please note that

LeetCode【167. 两数之和 II - 输入有序数组】

这道题最开始想到的就是,两个for,target是两数之和,就可以target - numbers[i],比较是否有与后面数相等. class Solution { public int[] twoSum(int[] numbers, int target) { int i,j,s; int c = numbers.length; int[] t = new int[2]; for(i = 0;i <= c-1;i++) { s = target - numbers[i]; t[0] = i+1

leetcode中的两数之和(第一题:简单)

描述:给定一个整数数组和一个目标值,找出数组中和为目标值的 两个 数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用.示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 题意:给一个整形的数,这个数在会在给定数组的两个位置数相加的和!这个和是指值的相加,不是数组的索引相加:如果在数组中有这样的两个数,要求返回两个数的索引(也就是数组的下标): 解法一:暴力方法:应用