标签:class bsp get java solution for nta res ash
Java :
class Solution { public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(target - nums[i])) { result[1] = i ; result[0] = map.get(target - nums[i]); return result; } map.put(nums[i], i); } return result; } }
C++ :
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { map<int,int> themap; vector<int> result; for(int i=0;i<=nums.size()-1;i++) { if(themap.find(target-nums[i])!=themap.end()) { result.push_back(themap.find(target-nums[i])->second); result.push_back(i); return result; } themap.insert(make_pair(nums[i],i)); } return result; } };
标签:class bsp get java solution for nta res ash
原文地址:http://www.cnblogs.com/Ro0kie/p/8004456.html