LeetCode算法(1)_兩數之和
import java.util.HashMap; import java.util.Map; /* * @lc app=leetcode.cn id=1 lang=java * * [1] 兩數之和 */ // @lc code=start class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> map = new HashMap<>(); for(int i = 0,w = nums.length;i<w;i++){ int num = nums[i]; int exceptNum = target-num; if(map.containsKey(exceptNum)){ return new int[]{i,map.get(exceptNum)}; } map.put(num, i); } return null; } } // @lc code=end
LeetCode算法(1)_兩數之和
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.