leetcode刷题之Two Sum(1)

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

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

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

所以返回 [0, 1]

思路

创建一个HashMap,key为数组中的元素值,value为数组下表,遍历数组,直到找到key为target - 数组元素的键值对,时间复杂度O(n)

代码

Java

public class TwoSum {

public static int[] solution(int[] nums,int target){

    Map<Integer,Integer> map=new HashMap<>();

    int[] result=new int[2];

    for(int i=0;i<nums.length;i++){
        if(map.containsKey(target - nums[i])){
            result[0]=map.get(target - nums[i]);
            result[1]=i;
        }
        map.put(nums[i],i);
    }
    return result;
}

}

原文地址:https://www.cnblogs.com/Simon-cat/p/10171405.html

时间: 2024-11-02 13:43:41

leetcode刷题之Two Sum(1)的相关文章

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

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: Given nums = [2, 7, 11, 15]

leetcode刷题记录-Two Sum

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刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

Leetcode刷题录之Two Sum

题意大概是给出一个数列num,和一个目标数target,然后要找出数列中的两个数,使得这两个数之和等于目标数,输出这两个数的下标值(从1开始算). 一个比较暴力的方法是用一个二重循环直接遍历序列,在第一重循环中找到a,在第二重循环中找到b,使得a+b=target,这种做法的时间复杂度是O(n^2), 提交时提示超时. 改进方法,先对数列num复制一个副本,然后对副本进行排序.在一重循环中找到a,接着对这个有序的副本进行二分查找,找到b= target-a,二分查找的 时间复杂度是O(logn)

leetcode 刷题之路 66 Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和数字sum,输出二叉树中从根节点到

LeetCode刷题(一):Two Sum

今天开始在LeetCode刷题,第一题为"两数之和(Two Sum)",整体来讲这一题难度是比较低的,但还是在这个过程中遇到一些问题,以下主要记录出现的一些问题. 这个题目比较容易想到的方法便是穷举了,很暴力但也很直接,需要注意的一点便是向量库Vector,自己也是前一阵子开始学数据结构才知道有这个库(有的也称为容器),定义计算数组长度用的方法是size()而不是length(),官方解答给的是length(),不知道是不是没有注意到还是因为他用的代码是Java(本人不了解Java),

【leetcode刷题笔记】4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending order.

【leetcode刷题笔记】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->