1. Two Sum - Easy

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], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

用hashmap记录数组元素及其下标 <nums[i], i>

注意返回值要求是下标小的在前面,而从左往右遍历数组, 一般在右半部分才能找到相加为target的数,此时的index应该是较的的那个,应该放在第二位,从map中取得的index应该放在第一位。

时间:O(N),空间:O(N)

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

原文地址:https://www.cnblogs.com/fatttcat/p/10057764.html

时间: 2024-07-30 22:33:09

1. Two Sum - Easy的相关文章

LeetCode--Array--Two sum (Easy)

1.Two sum (Easy) 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

【leetcode】Minimum Path Sum(easy)

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:由于只能向两个方向走,瞬间就没有了路线迂回的烦恼,题目的难度

112. Path Sum - Easy

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. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / 4 8 /

lintcode56 - Two Sum - easy

import java.util.Arrays; public class Solution { /* * @param numbers: An array of Integer * @param target: target = numbers[index1] + numbers[index2] * @return: [index1 + 1, index2 + 1] (index1 < index2) */ public int[] twoSum(int[] numbers, int targ

Two Sum [easy] (Python)

由于题目说了有且只有唯一解,可以考虑两遍扫描求解:第一遍扫描原数组,将所有的数重新存放到一个dict中,该dict以原数组中的值为键,原数组中的下标为值:第二遍扫描原数组,对于每个数nums[i]查看target-nums[i]是否在dict中,若在则可得到结果. 当然,上面两遍扫描是不必要的,一遍即可,详见代码. class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int]

Leetcode 1. Two Sum (Easy)

Description 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

1. Two Sum - Easy - Leetcode

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

LeetCode_112. Path Sum

112. Path Sum Easy 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. Note: A leaf is a node with no children. Example: Given the below binary tree and 

【目录】Leetcode

不知不觉居然已经写了200多篇随笔了,现在查找的时候经常要翻好久,很不方便.故给自己做个目录~ Leetcode 1.动态规划 Palindrome Partitioning II(hard) ☆ Distinct Subsequences(hard) Edit Distance (hard) Interleaving String (hard) Regular Expression Matching (hard) ★ Wildcard Matching(hard) ★ 大神太牛了 Longes