1. 程式人生 > >LeetCode:用HashMap解決問題

LeetCode:用HashMap解決問題

integer pub span 問題 ash length col .get int

LeetCode:用HashMap解決問題

Find Anagram Mappings

 1 class Solution {
 2     public int[] anagramMappings(int[] A, int[] B) {
 3         Map<Integer, Integer> D = new HashMap();
 4         for (int i = 0; i < B.length; ++i)
 5             D.put(B[i], i);
 6 
 7         int[] ans = new int[A.length];
8 int t = 0; 9 for (int x: A) 10 ans[t++] = D.get(x); 11 return ans; 12 } 13 }

思考:用字典來解決問題,巧妙將數組編號轉換為頁碼,然後快速查表

LeetCode:用HashMap解決問題