Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
1 #include <bits/stdc++.h> 2 #include <map> 3 #include <vector> 4 using namespace std; 5 6 class Solution 7 { 8 public: 9 vector<int> twoSum(vector<int> &numbers, int target) 10 { 11 unordered_map<int, int> hash; 12 vector<int> result; 13 for (int i = 0; i < numbers.size(); i++) 14 { 15 int numberToFind = target - numbers[i]; 16 if (hash.find(numberToFind) != hash.end()) 17 { 18 result.push_back(hash[numberToFind] + 1); 19 result.push_back(i + 1); 20 return result; 21 } 22 hash[numbers[i]] = i; 23 } 24 return result; 25 } 26 }; 27 int main() 28 { 29 vector<int> v; 30 int target; 31 int num; 32 while (cin >> num) 33 { 34 if (num == 0) 35 break; 36 v.push_back(num); 37 } 38 cin >> target; 39 Solution sol; 40 vector<int> result; 41 result = sol.twoSum(v, target); 42 cout << result[0] << " " << result[1] << endl; 43 return 0; 44 }
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.Map; 4 import java.util.Scanner; 5 6 class Solution { 7 8 public int[] twoSum(int[] numbers, int target) { 9 int[] result = new int[2]; 10 Map<Integer, Integer> map = new HashMap<>(); //其中用到互加性 11 for (int i = 0; i < numbers.length; i++) { 12 if (map.containsKey(target - numbers[i])) { //通过map检查是否存在符合的值,若存在将索引赋给result[1] 13 result[1] = i; 14 result[0] = map.get(target - numbers[i]); 15 return result; 16 } 17 map.put(numbers[i], i); 18 } 19 return result; 20 } 21 } 22 23 public class TwoSum { 24 public static void main(String[] args) { 25 Scanner input = new Scanner(System.in); 26 ArrayList<Integer> array = new ArrayList<>(); 27 while (!input.hasNext("0")) { 28 array.add(Integer.valueOf(input.next())); 29 } 30 Scanner read = new Scanner(System.in); 31 Integer target = read.nextInt(); 32 int[] d = new int[array.size()]; 33 for (int i = 0; i < array.size(); i++) { 34 d[i] = array.get(i); 35 } 36 Solution sol = new Solution(); 37 int[] result = sol.twoSum(d, target); 38 System.out.printf("%d %d", result[0], result[1]); 39 40 } 41 }
调用了getNode(hash(key), key)
方法,tab[(n - 1) & hash]
执行了如下操作:
1. 指针first指向那一行数组的引用(那一行数组是通过table下标范围n-1和key的hash值计算出来的),若命中,则通过下标访问数组,时间复杂度为O(1)
2. 如果没有直接命中(key进行hash时,产生相同的位运算值),存储方式变为红黑树,那么遍历树的时间复杂度为O(n)。
/** * Implements Map.get and related methods * * @param hash hash for key * @param key the key * @return the node, or null if none */ final Node<K,V> getNode(int hash, Object key) { Node<K,V>[] tab; Node<K,V> first, e; int n; K k; if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) { // 直接命中 if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) return first; // 未命中 if ((e = first.next) != null) { if (first instanceof TreeNode) return ((TreeNode<K,V>)first).getTreeNode(hash, key); do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } while ((e = e.next) != null); } } return null; }