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 target) {
        // write your code here
        // mergesort- nlogn
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

        // for loop with binary search -nlog
        for(int i = 0; i < numbers.length; i++){
            if(map.get(numbers[i]) != null){
                int[] twoIdx = {map.get(numbers[i]) +1 , i + 1};
                return twoIdx;
            }
            map.put(target - numbers[i], i);
        }
        int [] twoIdx = {};
        return twoIdx;
    }

}

降低时间复杂度用的HashMap方法!
不能用那个binarySearch,因为这里面要你返回的是index,binarysearch使用前要求你一定要sort过,不然他那个折半最后返回的值不会对的。而如果你sort过,那你返回的找到的那个index也和原始数字里所需数字的index不一样了!

时间: 2024-11-06 07:39:38

lintcode56 - 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 /

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

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]

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