两数之和

给定整数数组,如果有两个数之和是给定的数,那么返回两个数的下标。

每组输入只有一个解,同一个数不能用两次

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

因为nums[0]+nums[1]=9

返回[0,1]

我的答案:

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        for(int i = 0;i<nums.length;i++){
            for(int j = 0;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    if(i==j)continue;
                    res[0] = i;
                    res[1] = j;
                    return res;
                }
            }
        }
        return res;
    }
}

看了别人优秀的答案:

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                result[1] = i;
                result[0] = map.get(target - nums[i])-1;
                return result;
            }
        map.put(nums[i], i+1);
        }
    return result;
    }
}

使用HashMap,使的效率更高。

时间: 2024-07-31 14:28:13

两数之和的相关文章

给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的位置,如果sum=x则返回true, 3,找到位置后,保持i不变,从k处向前遍历,直到找到A[k]+A[i]等于x,并返回TRUE,如果找不到,则返回false. 论证步骤3:当前找到的位置恰好A[k]+A[i]>x,且前一位置的sum<x: 所以A[i]前面的数(不包括A[i])无论取哪两个数都

JavaScript-3.1--获取用户的输入,输出用户输入的两数之和---ShinePans

提示用户输入两个数,然后输出用户输入的两数之和 第一次输入 ,输入处为空 第二个输入,输入处为默认27  (这里强调语句的使用) <html> <head> <meta http-equiv="content-type" content="text/html;charset=GB2312"/> <title> 3.1 让用户输入两个数字,然后输出相加的结果 </title> </head> &l

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] 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 Targe

编程题:求两数之和

#include<stdio.h>       /*包含输入输出头文件*/ main()                            /*定义主函数*/ {  int a,b,sum;                /*定义整数变量a.b.sum*/ a=123;                        /*给a赋值*/ b=456;                        /*给b赋值*/ sum=a+b;                  /*令sum=a+b*/ p

LeetCode 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 m

找出数组中两数之和为指定值的所有整数对

一,问题描述 给定一个整型数组(数组中的元素可重复),以及一个指定的值.打印出数组中两数之和为指定值的 所有整数对 二,算法分析 一共有两种方法来求解.方法一借助排序,方法二采用HashSet 方法一: 先将整型数组排序,排序之后定义两个指针left和right.left指向已排序数组中的第一个元素,right指向已排序数组中的最后一个元素 将 arr[left]+arr[right]与 给定的元素比较,若前者大,right--:若前者小,left++:若相等,则找到了一对整数之和为指定值的元素

算法练习:两数之和

题目:给定一个整型数组,是否能找出两个数使其和为指定的某个值?注:整型数组中不存在相同的数. 一.解题方法 1.暴力破解法(时间复杂度O(n^2) ) 这是最容易想到的一种方法,即使用两层循环,从数组里取出一个数,然后在此数之后部分找出另外一个数,计算两数之和,判断是否等于指定值.如下: //直观的办法,使用两个循环 bool IsExistSumOfTwoNum( int nArray[], int nCount, int nSum ) { bool bRet = false; for ( i

【LeetCode】 两数之和 twoSum

两数之和 (简单) 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数: 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 例如: 给定 nums = [2,7,11,15] ,target = 9 因为 nums[0] + nums[1] = 9: 因此返回 [0,1]: v1.0代码如下: 正数.0或重复值通过测试: 异常用例: [-1, -2, -3, -4, -5] -8; 输出 [] /** * @param {number[]} nums * @par

LeetCode——两数之和

最近在家拧三阶魔方,把初级的玩法掌握了,也就是可以还原六个面了,速度不快,但是也很兴奋.三阶魔方的初级玩法按照套路拧就可以了,每一步需要完成的任务,该步骤转动的方法基本都是固定的,而且变化也并不是特别多.只要按照套路多练习,不考虑速度的情况下还原一个三阶魔方还是很容易的. 编程入门和还原魔方其实差不多,最初也是掌握套路后反复的练习,先从一个生手变成一个熟手,然后再去提高.分享一个段子,在知乎上看到的,如下: 陈康肃公尧咨善射,当世无双,公亦以此自矜.尝射于家圃,有卖油翁释担而立,睨之,久而不去.