标签:lin nta turn dex for c++ ash nbsp table
Kotlin code:
class Solution { fun twoSum(nums: IntArray, target: Int): IntArray { val v2i: MutableMap<Int,Int> = mutableMapOf() for((index, value) in nums.withIndex()) { val complementary = target-value if(v2i.contains(complementary)) { return intArrayOf(v2i[complementary]!!, index) } v2i += value to index } return intArrayOf() } }
C++
1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) { 4 using Value = int; 5 using Index = int; 6 unordered_map<Value,Index> hash; 7 for(int i=0; i<nums.size(); ++i) { 8 int value = target-nums[i]; 9 if(hash.count(value)) { 10 return vector<int>{ hash[value], i }; 11 } 12 hash[nums[i]] = i; 13 } 14 15 return {}; 16 } 17 };
标签:lin nta turn dex for c++ ash nbsp table
原文地址:https://www.cnblogs.com/BlackYao/p/12094305.html