力扣01两数之和Java版

class Solution {  

    public int[] twoSum(int[] nums, int target) {  

        HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();  

        int[] res = new int[2];  

        for (int i = 0; i < nums.length; ++i) {  

            if (m.containsKey(target - nums[i])) {  

                res[0] = m.get(target - nums[i]);  

                res[1] = i;  

                break;  

            }  

            m.put(nums[i], i);  

扫描二维码关注公众号,回复: 7696809 查看本文章

        }  

        return res;  

    }  

}  

1.   class Solution {  

2.      public int[] twoSum(int[] nums, int target) {  

3.          HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();  

4.          int[] res = new int[2];  

5.          for (int i = 0; i < nums.length; ++i) {  

6.              if (m.containsKey(target - nums[i])) {  

7.                  res[0] = m.get(target - nums[i]);  

8.                  res[1] = i;  

9.                  break;  

10.             }  

11.             m.put(nums[i], i);  

12.         }  

13.         return res;  

14.     }  

15. }  

猜你喜欢

转载自www.cnblogs.com/codeleader/p/11634139.html