From 652b36c69033fbef360104fe26d21a34343274b1 Mon Sep 17 00:00:00 2001 From: jasper Date: Sun, 21 Jun 2020 23:14:52 -0400 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0twoSum=20I=20=E5=92=8CII=20C+?= =?UTF-8?q?+=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\345\277\203\346\200\235\346\203\263.md" | 101 +++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" index 00ba39d..1fd3b72 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" @@ -157,9 +157,108 @@ int[] twoSum(int[] nums, int target) { ![labuladong](../pictures/labuladong.jpg) +[labuladong](https://github.com/labuladong) 提供TwoSum I JAVA解法代码: + +```JAVA +int[] twoSum(int[] nums, int target) { + int n = nums.length; + index index = new HashMap<>(); + // 构造一个哈希表:元素映射到相应的索引 + for (int i = 0; i < n; i++) + index.put(nums[i], i); + + for (int i = 0; i < n; i++) { + int other = target - nums[i]; + // 如果 other 存在且不是 nums[i] 本身 + if (index.containsKey(other) && index.get(other) != i) + return new int[] {i, index.get(other)}; + } + + return new int[] {-1, -1}; +} +``` + +[Jinglun Zhou](https://github.com/Jasper-Joe) 提供TwoSum I C++解法代码: + +```CPP +class Solution { +public: + vector twoSum(vector& nums, int target) { + int n=nums.size(); + unordered_map index; + // 构造一个哈希表: 元素映射到相应的索引 + for(int i=0;i freq = new HashMap<>(); + + public void add(int number) { + // 记录 number 出现的次数 + freq.put(number, freq.getOrDefault(number, 0) + 1); + } + + public boolean find(int value) { + for (Integer key : freq.keySet()) { + int other = value - key; + // 情况一 + if (other == key && freq.get(key) > 1) + return true; + // 情况二 + if (other != key && freq.containsKey(other)) + return true; + } + return false; + } +} +``` +[Jinglun Zhou](https://github.com/Jasper-Joe) 提供TwoSum II C++解法代码: + +```CPP +class TwoSum { +public: + unordered_map freq; // key为当前加入的元素,value为当前加入元素一共出现的频率 + TwoSum() {} // constructor + + void add(int number) { + // 记录number出现的次数 + freq[number]++; + } + + bool find(int value) { + for(auto& cur:freq) + { + int other=value-cur.first; + // 情况一: other和当前这个元素一样大,所以需要两个这个的元素才能构成value + if(other==cur.first && cur.second>1) + return true; + // 情况二: other和当前这个元素不一样,other在freq中需要至少出现一次,与twoSum I道理一样 + if(other!=cur.first && freq.count(other)) + return true; + } + return false; + } +}; +``` + [上一篇:滑动窗口技巧](../算法思维系列/滑动窗口技巧.md) [下一篇:常用的位操作](../算法思维系列/常用的位操作.md) -[目录](../README.md#目录) \ No newline at end of file +[目录](../README.md#目录) -- GitLab