LeetCode[Hash Table]: 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 your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

Brute force

vector<int> twoSum(vector<int> &numbers, int target) {
    vector<int> answer;
    for (int i = 0; i < numbers.size() - 1; i++)
        for (int j = i + 1; j < numbers.size(); j++)
            if (numbers[i] + numbers[j] == target) {
                answer.push_back(i + 1);
                answer.push_back(j + 1);
                return answer;
            }

    return answer;
}

Result: Time Limit Exceeded

Hash table

vector<int> twoSum(vector<int> &numbers, int target) {
    vector<int> answer;
    map<int, int> numMap;
    for (int i = 0; i < numbers.size(); i++)
        if (numMap.find(target - numbers[i]) != numMap.end()) {
            answer.push_back(numMap[target - numbers[i]] + 1);
            answer.push_back(i + 1);
            return answer;
        }
        else numMap[numbers[i]] = i;

    return answer;
}
时间: 2024-11-13 14:31:09

LeetCode[Hash Table]: Two Sum的相关文章

LeetCode[Hash Table]: Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such windo

LeetCode[Hash Table]: Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路:对每一个单词的所有字母按照字典顺序排序,排序结果作为key,所有具有相同key的单词组合在一起成为一个Anagram group.最后返回所有的Anagram group. class Solution { public: vector<string> anag

LeetCode[Hash Table]: Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (partially

[leetcode单元总结]hash table部分easy篇小总结

在difficulty为easy的hash table部分,没有用到常见的哈希算法,更多的是借用数组的下标来实现.对于下标的操作看起来很简单,其实需要细致和耐心,可能一个小错误,比如下标字母弄错用成了上个循环使用的下标(t.t'),结束条件没写对等等就会导致错误. A.在Valid Sudoku 中,判断中拓宽了思维,1.多动脑子,小九宫格中,将每个小个子的下标与第几个联系起来.2.对于一般的含有i,j的双重for循环,比如: for(i=0;i<9;i++) { for(j=0;j<9;j+

[LeetCode] 1. Two Sum_Easy tag: Hash Table

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 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)

Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need to help them find out their common interest with the least list index sum. If there is a choice tie betw

【string】hash table, two pointers, string

利用hash table, two pointers, string的题目. 1.求最长不重复子串的长度 hash table体现在一个数组,下标是字符串中元素的ASCII值,下标对应的元素代表该元素在字符串中出现的位置. two pointers体现在用i一步步向前移去遍历字符串中的元素,作为不重复子串的末尾位置:用j指向不重复字符区间的首字符的位置. 1 /*************************** 2 @date 4.23 3 @description https://leet

C 语言构造hash table 解 LC majority element问题

Leetcode上 majority element这题是 有 时间O(N), 空间O(1)的解的. https://leetcode.com/problems/majority-element/ 用hash table来解则为 时间O(N), 空间O(N). 如果是Java里 用HashMap很方便了. 有位同学问怎么用c语言来构造hash table. 我就随手写了一个: typedef struct Node { int val, count; } Node; typedef struct

leetcode 练习1 two sum

leetcode 练习1  two sum [email protected] 问题描述 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. Example: Given nums = [2, 7, 11, 15