# Leetcode 1.Two Sum # HashMap、Map > https://www.runoob.com/java/java-hashmap.html > https://www.delftstack.com/zh-tw/howto/java/difference-between-hashmap-and-map-in-java/ faster than 99.96% 時間複雜度: O(n) 空間複雜度: O(n) ``` java= class Solution { public int[] twoSum(int[] nums, int target) { // HashMap=>key、value去對應,藉此快速找出符合的條件 // key type , value type Map<Integer,Integer> map= new HashMap<>(); for(int i=0;i<nums.length;i++){ if(map.get(target-nums[i])!=null){ return new int[]{map.get(target-nums[i]),i}; } map.put(nums[i],i); } // 因型態是int[],故回傳null,不能回傳0(int) return null; } } ``` ###### tags: `Hash Table`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up