001 两数之和

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. Map<Integer, Integer> hashTable = new HashMap<Integer, Integer>();
  4. int[] ans = new int[2];
  5. for (int i = 0; i < nums.length; ++i) {
  6. if (hashTable.containsKey(target - nums[i])) {
  7. ans[0] = i;
  8. ans[1] = hashTable.get(target - nums[i]);
  9. }
  10. hashTable.put(nums[i], i);
  11. } // for
  12. return ans;
  13. }
  14. }